From 4de09978fdb4ff37a6c481ccfbca7adc996380eb Mon Sep 17 00:00:00 2001 From: Yukine Date: Mon, 12 Aug 2024 13:46:43 +0200 Subject: [PATCH] ci(docker): fix multiplatform build on multiple runners overriding see https://github.com/docker/build-push-action/issues/671 --- .github/workflows/docker-build.yml | 76 ++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 9 deletions(-) diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index 0748c50..57a3a89 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -15,8 +15,11 @@ on: - Dockerfile - .github/workflows/docker-build.yml +env: + REGISTRY_IMAGE: ghcr.io/retrorealm/playmatch + jobs: - docker: + build: strategy: matrix: platform: [ ubuntu-latest, linux-arm64 ] @@ -30,7 +33,7 @@ jobs: id: meta uses: docker/metadata-action@v5 with: - images: ghcr.io/retrorealm/playmatch + images: ${{ env.REGISTRY_IMAGE }} tags: | type=ref,event=branch type=ref,event=pr @@ -50,9 +53,9 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push - id: docker_build - uses: docker/build-push-action@v5 + - name: Build and push by digest + id: build + uses: docker/build-push-action@v6 with: context: . file: Dockerfile @@ -60,8 +63,63 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} annotations: ${{ steps.meta.outputs.annotations }} - cache-from: type=gha - cache-to: type=gha,mode=max + outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true + cache-from: type=gha,scope=build-${{ matrix.platform }} + cache-to: type=gha,scope=build-${{ matrix.platform }} + + - name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v4 + with: + name: digests-${{ env.PLATFORM_PAIR }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge: + runs-on: ubuntu-latest + needs: + - build + steps: + - 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: Docker metadata extraction + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY_IMAGE }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=ref,event=tag + type=sha + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} + - name: Inspect image + run: | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}