ci: Harden the image build process #2
Workflow file for this run
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: Dev Image CI Build | |
| # Any change in triggers needs to be reflected in the concurrency group. | |
| on: | |
| pull_request: | |
| types: | |
| - opened | |
| - synchronize | |
| - reopened | |
| push: | |
| branches: | |
| - main | |
| permissions: | |
| contents: read | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.after }} | |
| cancel-in-progress: true | |
| jobs: | |
| build: | |
| if: ${{ github.repository == 'cilium/cilium-cli' }} | |
| runs-on: ubuntu-24.04 | |
| permissions: | |
| contents: read | |
| outputs: | |
| tag: ${{ steps.tag.outputs.tag }} | |
| repo_tags: ${{ steps.tag.outputs.repo_tags }} | |
| digest: ${{ steps.docker_build.outputs.digest }} | |
| steps: | |
| - name: Getting image tag | |
| id: tag | |
| run: | | |
| if [ ${{ github.event.pull_request.head.sha }} != "" ]; then | |
| echo "tag=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT | |
| echo "repo_tags=quay.io/${{ github.repository_owner }}/cilium-cli-ci:${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT | |
| else | |
| echo "tag=${{ github.sha }}" >> $GITHUB_OUTPUT | |
| echo "repo_tags=quay.io/${{ github.repository_owner }}/cilium-cli-ci:latest,quay.io/${{ github.repository_owner }}/cilium-cli-ci:${{ github.sha }}" >> $GITHUB_OUTPUT | |
| fi | |
| # SECURITY: Checking out untrusted code from pull_request_target | |
| # This workflow uses pull_request_target which has write access to the base repository. | |
| # The code being checked out may come from a forked repository and should be treated as untrusted. | |
| # Build steps run in this job should not have access to any secrets or credentials. | |
| - name: Checkout Untrusted Source Code | |
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | |
| with: | |
| ref: ${{ steps.tag.outputs.tag }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 | |
| - name: Docker Build | |
| uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 | |
| id: docker_build | |
| with: | |
| context: . | |
| platforms: linux/arm64,linux/amd64 | |
| push: false | |
| tags: ${{ steps.tag.outputs.repo_tags }} | |
| outputs: type=oci,dest=/tmp/image.tar | |
| - name: Upload image artifact | |
| uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # v4.5.0 | |
| with: | |
| name: docker-image | |
| path: /tmp/image.tar | |
| retention-days: 1 | |
| push: | |
| needs: build | |
| if: ${{ github.repository == 'cilium/cilium-cli' }} | |
| runs-on: ubuntu-24.04 | |
| environment: ci | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Download image artifact | |
| uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 | |
| with: | |
| name: docker-image | |
| path: /tmp | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 | |
| - name: Login to quay.io for CI | |
| uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 | |
| with: | |
| registry: quay.io | |
| username: ${{ secrets.QUAY_CI_USERNAME }} | |
| password: ${{ secrets.QUAY_CI_TOKEN }} | |
| - name: Load and push image | |
| run: | | |
| for tag in $(echo "${{ needs.build.outputs.repo_tags }}" | tr ',' '\n'); do | |
| docker buildx imagetools create --tag "$tag" oci-archive:/tmp/image.tar | |
| done |