diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3377487..9524d54 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,13 +22,21 @@ jobs: - run: mix test docker: - name: Docker - runs-on: ubuntu-24.04 + name: Docker (${{ matrix.platform }}) + runs-on: ${{ matrix.runner }} permissions: contents: 'read' id-token: 'write' + strategy: + matrix: + include: + - platform: linux/amd64 + runner: ubuntu-24.04 + - platform: linux/arm64 + runner: ubuntu-24.04-arm + env: IMAGE_NAME: 'diff' PROJECT_ID: 'hexpm-prod' @@ -38,12 +46,6 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Set short git commit SHA - id: vars - run: | - calculatedSha=$(git rev-parse --short ${{ github.sha }}) - echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV - - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -66,10 +68,72 @@ jobs: username: 'oauth2accesstoken' password: '${{ steps.auth.outputs.access_token }}' - - name: Build and push + - name: Build and push by digest + id: build uses: docker/build-push-action@v6 with: - tags: gcr.io/${{ env.PROJECT_ID }}/${{ env.IMAGE_NAME }}:${{ env.COMMIT_SHORT_SHA }} - push: ${{ github.event_name != 'pull_request' }} - cache-from: type=gha - cache-to: type=gha,mode=max + platforms: ${{ matrix.platform }} + outputs: type=image,name=gcr.io/${{ env.PROJECT_ID }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} + cache-from: type=gha,scope=${{ matrix.runner }} + cache-to: type=gha,scope=${{ matrix.runner }},mode=max + + - name: Export digest + if: ${{ github.event_name != 'pull_request' }} + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + if: ${{ github.event_name != 'pull_request' }} + uses: actions/upload-artifact@v4 + with: + name: digests-${{ matrix.runner }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + docker-merge: + name: Docker Merge + runs-on: ubuntu-24.04 + if: ${{ github.event_name != 'pull_request' }} + needs: docker + permissions: + contents: 'read' + id-token: 'write' + env: + IMAGE_NAME: 'diff' + PROJECT_ID: 'hexpm-prod' + SERVICE_ACCOUNT: ${{ secrets.GCLOUD_SERVICE_ACCOUNT }} + WORKLOAD_IDENTITY_PROVIDER: ${{ secrets.GCLOUD_WORKFLOW_IDENTITY_POOL_PROVIDER }} + steps: + - name: Set short git commit SHA + run: echo "COMMIT_SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV + - name: Download digests + uses: actions/download-artifact@v4 + with: + path: /tmp/digests + pattern: digests-* + merge-multiple: true + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Google auth + id: auth + uses: 'google-github-actions/auth@v2' + with: + token_format: 'access_token' + project_id: ${{ env.PROJECT_ID }} + service_account: ${{ env.SERVICE_ACCOUNT }} + workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }} + - name: Docker Auth + uses: 'docker/login-action@v3' + with: + registry: gcr.io + username: 'oauth2accesstoken' + password: '${{ steps.auth.outputs.access_token }}' + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create \ + -t gcr.io/${{ env.PROJECT_ID }}/${{ env.IMAGE_NAME }}:${{ env.COMMIT_SHORT_SHA }} \ + $(printf 'gcr.io/${{ env.PROJECT_ID }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)