diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 4185b95..c13e79b 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -10,19 +10,23 @@ on: env: REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} + REGISTRY_IMAGE: ghcr.io/${{ github.repository }} jobs: - build-and-push-image: + build-image: if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} - runs-on: ubuntu-latest + name: Build image (${{ matrix.os }}/${{ matrix.arch }}) + runs-on: ${{ matrix.runner }} strategy: fail-fast: false matrix: include: - - module: metaproxy - dockerfile: Dockerfile - image: ghcr.io/${{ github.repository }} + - os: linux + arch: amd64 + runner: ubuntu-24.04 + - os: linux + arch: arm64 + runner: ubuntu-24.04-arm permissions: contents: read packages: write @@ -44,7 +48,7 @@ jobs: id: meta uses: docker/metadata-action@v5 with: - images: ${{ matrix.image }} + images: ${{ env.REGISTRY_IMAGE }} tags: | type=schedule type=ref,event=branch @@ -52,28 +56,81 @@ jobs: type=ref,event=pr type=sha - - name: Cache Docker layers - uses: actions/cache@v4 - with: - path: /tmp/.buildx-cache - key: ${{ runner.os }}-buildx-${{ hashFiles(matrix.dockerfile) }} - restore-keys: | - ${{ runner.os }}-buildx- - - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Build and push Docker image + - name: Build and push Docker image by digest + id: build uses: docker/build-push-action@v6 with: context: . - file: ${{ matrix.dockerfile }} - platforms: linux/amd64,linux/arm64 - push: true - tags: ${{ steps.meta.outputs.tags }} + file: Dockerfile + platforms: ${{ matrix.os }}/${{ matrix.arch }} labels: ${{ steps.meta.outputs.labels }} - cache-from: type=local,src=/tmp/.buildx-cache - cache-to: type=local,dest=/tmp/.buildx-cache,mode=max + outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true + cache-from: type=gha,scope=metaproxy-${{ matrix.os }}-${{ matrix.arch }} + cache-to: type=gha,scope=metaproxy-${{ matrix.os }}-${{ matrix.arch }},mode=max + + - 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-${{ matrix.os }}-${{ matrix.arch }} + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge-image: + if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} + name: Merge image manifests + runs-on: ubuntu-24.04 + needs: + - build-image + permissions: + contents: read + packages: write + + steps: + - name: Download digests + uses: actions/download-artifact@v4 + with: + path: /tmp/digests + pattern: digests-* + merge-multiple: true + + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY_IMAGE }} + tags: | + type=schedule + type=ref,event=branch + type=ref,event=tag + type=ref,event=pr + type=sha + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - 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: Inspect image + run: docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}