Repository navigation
Build and Push mimir-build-image #43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Push mimir-build-image | |
| # A note on permissions: | |
| # We don't explicitly check that the PR author is authorized to trigger a build: | |
| # fork PRs receive a read-only GITHUB_TOKEN with no secrets, so the image push and | |
| # Makefile commit steps fail naturally with permission denied, while PRs from | |
| # internal branches already require repo write access. Relying on GitHub's | |
| # permission model also lets us drop the org-members read scope the | |
| # mimir-github-bot App previously needed. | |
| on: | |
| pull_request: | |
| permissions: {} | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number }} | |
| cancel-in-progress: true | |
| jobs: | |
| prepare: | |
| name: Prepare | |
| runs-on: ubuntu-x64 | |
| permissions: | |
| contents: read | |
| pull-requests: write # So we can post comments on the PR. | |
| steps: | |
| # We only want for this workflow to do anything when anything inside mimir-build-image/ is modified, | |
| # as well as when this workflow is modified. | |
| # | |
| # However, we want for the workflow to be able to run for all PRs, just so it can be made a required check | |
| # (i.e. so PRs can't be merged until it's done). | |
| # | |
| # We list changed files via the paginated PR Files API rather than `gh pr diff --name-only` | |
| # because the latter caps at 300 files and returns HTTP 406 on larger PRs, in which case the | |
| # script would silently fall into the "not modified" branch even when the Dockerfile was | |
| # actually modified. | |
| - name: Check if files modified | |
| id: check_if_files_modified | |
| run: | | |
| set -euo pipefail | |
| files=$(gh api --paginate \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" \ | |
| --jq '.[].filename') | |
| if echo "$files" | grep -qxE '(mimir-build-image/.*|\.github/workflows/push-mimir-build-image\.yml)'; then | |
| echo "modified=true" >> "$GITHUB_OUTPUT" | |
| echo "Dockerfile or build workflow was modified" | |
| else | |
| echo "modified=false" >> "$GITHUB_OUTPUT" | |
| echo "Dockerfile or build workflow were not modified" | |
| fi | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Checkout repository | |
| if: steps.check_if_files_modified.outputs.modified == 'true' | |
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7 | |
| with: | |
| persist-credentials: false | |
| ref: ${{ github.event.pull_request.head.ref }} # Check out the PR branch itself, not the temporary merge commit | |
| - name: Compute variables | |
| if: steps.check_if_files_modified.outputs.modified == 'true' | |
| id: compute_variables | |
| run: | | |
| main_build_image=$(make print-build-image) | |
| main_image_tag=$(echo $main_build_image | cut -d ':' -f 2-) # Get everything after the first : (ie. the image tag and digest, if present) | |
| image_name=$(echo $main_build_image | cut -d ':' -f 1) | |
| echo "image_name=$image_name" >> $GITHUB_OUTPUT | |
| echo "main_image_tag=$main_image_tag" >> $GITHUB_OUTPUT | |
| # Compute a hash of the entire build image directory and this workflow, so that any changes to the Dockerfile or other files trigger a new build. | |
| # We use tar so that this also captures changes to permissions. | |
| new_image_hash=$(tar -c -f - --exclude .uptodate --mtime='1900-01-01 00:00Z' --sort=name mimir-build-image .github/workflows/push-mimir-build-image.yml | md5sum | awk '{print substr($1, 0, 10)}') | |
| echo "New image hash is $new_image_hash" | |
| new_image_tag="pr${{ github.event.pull_request.number }}-$new_image_hash" | |
| echo "new_image_tag=$new_image_tag" >> $GITHUB_OUTPUT | |
| { | |
| echo "build_args<<EOF" | |
| make print-build-image-build-args | |
| echo "EOF" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Check if image is already built | |
| if: steps.check_if_files_modified.outputs.modified == 'true' | |
| id: check_if_image_is_built | |
| # Make sure to run skopeo ignoring any credential helpers and auth since the build image | |
| # must be able to be pulled anonymously. This also avoid permissions errors in the Grafana | |
| # runner when the `ubuntu` user tries to load Docker credentials owned by `root`. | |
| run: | | |
| echo "Checking if image has already been built" | |
| if skopeo inspect --no-creds --raw "docker://${{ steps.compute_variables.outputs.image_name }}:${{ steps.compute_variables.outputs.new_image_tag }}"; then | |
| echo "Tag ${{ steps.compute_variables.outputs.new_image_tag }} exists" | |
| echo "need_to_build=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "Tag ${{ steps.compute_variables.outputs.new_image_tag }} does not exist" | |
| echo "need_to_build=true" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Add comment to the PR | |
| if: steps.check_if_files_modified.outputs.modified == 'true' | |
| id: notification | |
| run: | | |
| if [ ${{ steps.check_if_image_is_built.outputs.need_to_build }} == 'true' ]; then | |
| gh pr comment $PR_NUMBER --body "**Building new version of mimir-build-image**. After image is built and pushed to the registry, \ | |
| a new commit will automatically be added to this PR with new image version \`$IMAGE:$NEW_IMAGE_TAG\`. This can take a few minutes." | |
| else | |
| echo "This PR will not trigger a build of mimir-build-image" | |
| gh pr comment $PR_NUMBER --body "**Not building new version of mimir-build-image**. This PR modifies the build image or the build image build workflow, but the image \`$IMAGE:$NEW_IMAGE_TAG\` already exists." | |
| fi | |
| env: | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} | |
| NEW_IMAGE_TAG: ${{ steps.compute_variables.outputs.new_image_tag }} | |
| IMAGE: ${{ steps.compute_variables.outputs.image_name }} | |
| outputs: | |
| need_to_build: ${{ steps.check_if_image_is_built.outputs.need_to_build }} | |
| new_image_tag: ${{ steps.compute_variables.outputs.new_image_tag }} | |
| main_image_tag: ${{ steps.compute_variables.outputs.main_image_tag }} | |
| build_args: ${{ steps.compute_variables.outputs.build_args }} | |
| build-push-multiarch: | |
| name: Build image | |
| needs: | |
| - prepare | |
| if: needs.prepare.outputs.need_to_build == 'true' | |
| permissions: | |
| contents: read | |
| id-token: write | |
| uses: grafana/shared-workflows/.github/workflows/docker-build-push-multiarch.yml@638f24848a49edb0bd14f771b6dd37b4455f1591 # main | |
| with: | |
| context: mimir-build-image | |
| platforms: linux/arm64,linux/amd64 | |
| tags: ${{ needs.prepare.outputs.new_image_tag }} | |
| build-args: ${{ needs.prepare.outputs.build_args }} | |
| push: true | |
| registries: gar | |
| gar-environment: dev | |
| gar-repository: docker-mimir-build-image | |
| gar-image: mimir-build-image | |
| generate-summary: true | |
| runner-type-x64: ubuntu-x64 | |
| runner-type-arm64: ubuntu-arm64 | |
| # This name is completely misleading, but a previous version of this workflow used this name and it's used as a required PR check in GitHub. | |
| # Renaming it would mean backporting a large number of changes to old release branches, so we're keeping it as-is for now. | |
| # Once all active release branches use this workflow, it'll be much easier to rename it and backport just that rename. | |
| build_and_push: | |
| runs-on: ubuntu-x64 | |
| needs: | |
| - prepare | |
| - build-push-multiarch | |
| if: always() # Even if the build-push-multiarch job is skipped, we still want to run this job and mark it as successful, so that we can make this job a required check. | |
| permissions: | |
| contents: read | |
| id-token: write # Required to generate GitHub app token to push commit. | |
| steps: | |
| # We always run this job even if the image build is skipped, so that we can make this job a required check. | |
| # So if the image build failed, then we want this job to fail too. | |
| - name: Fail if image build failed | |
| if: needs.prepare.result != 'success' || (needs.prepare.outputs.need_to_build == 'true' && needs.build-push-multiarch.result != 'success') | |
| run: | | |
| echo "Image build failed, stopping." | |
| exit 1 | |
| - name: Checkout repository | |
| if: needs.prepare.outputs.need_to_build == 'true' | |
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7 | |
| with: | |
| persist-credentials: false | |
| ref: ${{ github.event.pull_request.head.ref }} # Check out the PR branch itself, not the temporary merge commit | |
| # Compare the just-built tag against the one currently referenced in the Makefile. | |
| # If they differ, update the Makefile and signal the next step to commit; otherwise | |
| # `changed=false` and no commit will be made. | |
| - name: Check whether Makefile needs updating, and update it | |
| id: update_makefile | |
| if: needs.prepare.outputs.need_to_build == 'true' | |
| run: | | |
| set -euo pipefail | |
| NEW_IMAGE_TAG=$(echo $NEW_IMAGE | cut -d ':' -f 2-) | |
| echo "Current build image tag is $MAIN_IMAGE_TAG" | |
| echo "Built image is $NEW_IMAGE, new tag is $NEW_IMAGE_TAG" | |
| if [ "$MAIN_IMAGE_TAG" = "$NEW_IMAGE_TAG" ]; then | |
| echo "Build image tag is already up to date." | |
| echo "changed=false" >> "$GITHUB_OUTPUT" | |
| else | |
| sed -i "s/$MAIN_IMAGE_TAG/$NEW_IMAGE_TAG/g" Makefile | |
| echo "changed=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| env: | |
| NEW_IMAGE: ${{ needs.build-push-multiarch.outputs.image-digests }} | |
| MAIN_IMAGE_TAG: ${{ needs.prepare.outputs.main_image_tag }} | |
| # Generate the app token here (rather than earlier in the job) so we only mint one | |
| # when we actually need to commit. | |
| - name: Generate GitHub App Token | |
| if: steps.update_makefile.outputs.changed == 'true' | |
| id: app-token-for-commit | |
| uses: grafana/shared-workflows/actions/create-github-app-token@46f48da11e78ebdba7a8747ae456b11062fac83e # create-github-app-token/v0.3.1 | |
| with: | |
| github_app: mimir-github-bot | |
| # Commit via the GraphQL createCommitOnBranch mutation so that the resulting commit | |
| # is signed by GitHub's web-flow key (shows as Verified in the PR). We use the | |
| # "Mimir bot" App token rather than secrets.GITHUB_TOKEN because events triggered | |
| # by the latter don't spawn GitHub Actions runs. | |
| # Refer to https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow | |
| - name: Commit Makefile update to PR branch | |
| if: steps.update_makefile.outputs.changed == 'true' | |
| uses: planetscale/ghcommit-action@a6b150b81dca5dd027baa898604418eec9e11465 # v0.2.22 | |
| with: | |
| commit_message: "Update build image version to ${{ needs.prepare.outputs.new_image_tag }}" | |
| repo: ${{ github.event.pull_request.head.repo.full_name }} | |
| branch: ${{ github.head_ref }} | |
| file_pattern: Makefile | |
| env: | |
| GITHUB_TOKEN: ${{ steps.app-token-for-commit.outputs.token }} |