build-and-publish-release.yml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. name: Build and Publish Release
  2. on:
  3. push:
  4. tags:
  5. - 'v[0-9]+.[0-9]+.[0-9]+'
  6. workflow_dispatch:
  7. inputs:
  8. release_version:
  9. description: "Version of the release"
  10. required: true
  11. concurrency:
  12. group: build-opencost
  13. cancel-in-progress: true
  14. env:
  15. # Use docker.io for Docker Hub if empty
  16. REGISTRY: ghcr.io
  17. # github.repository as <account>/<repo>
  18. IMAGE_NAME: ${{ github.repository }}
  19. jobs:
  20. build-and-publish-opencost:
  21. runs-on: ubuntu-latest
  22. permissions:
  23. contents: read
  24. packages: write
  25. steps:
  26. - name: Get Version From Tag
  27. id: tag
  28. if: ${{ github.event_name }} == 'push'
  29. run: |
  30. echo "TRIGGERED_TAG=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
  31. - name: Determine Version Number
  32. id: version_number
  33. run: |
  34. if [ -z "${TRIGGERED_TAG}" ];
  35. then
  36. version=${{ inputs.release_version }}
  37. else
  38. version=$TRIGGERED_TAG
  39. fi
  40. if [[ ${version:0:1} == "v" ]];
  41. then
  42. echo "RELEASE_VERSION=${version:1}" >> $GITHUB_OUTPUT
  43. else
  44. echo "RELEASE_VERSION=$version" >> $GITHUB_OUTPUT
  45. fi
  46. - name: Show Input Values
  47. run: |
  48. echo "release version: ${{ inputs.release_version }}"
  49. - name: Make Branch Name
  50. id: branch
  51. run: |
  52. VERSION_NUMBER=${{ steps.version_number.outputs.RELEASE_VERSION }}
  53. echo "BRANCH_NAME=v${VERSION_NUMBER%.*}" >> $GITHUB_ENV
  54. - name: Checkout Repo
  55. uses: actions/checkout@v4
  56. with:
  57. repository: 'opencost/opencost'
  58. ref: '${{ steps.branch.outputs.BRANCH_NAME }}'
  59. path: ./opencost
  60. - name: Set SHA
  61. id: sha
  62. run: |
  63. pushd ./opencost
  64. echo "OC_SHORTHASH=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
  65. popd
  66. # Login against a Docker registry except on PR
  67. # https://github.com/docker/login-action
  68. - name: Log into registry ${{ env.REGISTRY }}
  69. uses: docker/login-action@v3
  70. with:
  71. registry: ${{ env.REGISTRY }}
  72. username: ${{ github.actor }}
  73. password: ${{ secrets.GITHUB_TOKEN }}
  74. - name: Set OpenCost Image Tags
  75. id: tags
  76. run: |
  77. echo "IMAGE_TAG=ghcr.io/opencost/opencost:${{ steps.sha.outputs.OC_SHORTHASH }}" >> $GITHUB_OUTPUT
  78. echo "IMAGE_TAG_LATEST=ghcr.io/opencost/opencost:latest" >> $GITHUB_OUTPUT
  79. echo "IMAGE_TAG_VERSION=ghcr.io/opencost/opencost:${{ steps.version_number.outputs.RELEASE_VERSION }}" >> $GITHUB_OUTPUT
  80. - name: Set up Docker Buildx
  81. uses: docker/setup-buildx-action@v3
  82. with:
  83. buildkitd-flags: --debug
  84. - name: Install Go
  85. uses: actions/setup-go@v5
  86. with:
  87. go-version: 'stable'
  88. - name: Set up just
  89. uses: extractions/setup-just@v2
  90. - name: Install crane
  91. uses: imjasonh/setup-crane@v0.4
  92. ## Install manifest-tool, which is required to combine multi-arch images
  93. ## https://github.com/estesp/manifest-tool
  94. - name: Install manifest-tool
  95. run: |
  96. mkdir -p manifest-tool
  97. pushd manifest-tool
  98. wget -q https://github.com/estesp/manifest-tool/releases/download/v2.0.8/binaries-manifest-tool-2.0.8.tar.gz
  99. tar -xzf binaries-manifest-tool-2.0.8.tar.gz
  100. cp manifest-tool-linux-amd64 manifest-tool
  101. echo "$(pwd)" >> $GITHUB_PATH
  102. - name: Build and push (multiarch) OpenCost
  103. working-directory: ./opencost
  104. run: |
  105. just build '${{ steps.tags.outputs.IMAGE_TAG }}' '${{ steps.version_number.outputs.RELEASE_VERSION }}'
  106. crane copy '${{ steps.tags.outputs.IMAGE_TAG }}' '${{ steps.tags.outputs.IMAGE_TAG_LATEST }}'
  107. crane copy '${{ steps.tags.outputs.IMAGE_TAG }}' '${{ steps.tags.outputs.IMAGE_TAG_VERSION }}'