Build multiarch image correctly #2
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: Publish | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| branches: | |
| - main | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build: | |
| strategy: | |
| matrix: | |
| arch: [amd64, arm64] | |
| include: | |
| - arch: amd64 | |
| runner: ubuntu-latest | |
| platform: linux/amd64 | |
| - arch: arm64 | |
| runner: ubuntu-24.04-arm | |
| platform: linux/arm64 | |
| runs-on: ${{ matrix.runner }} | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Setup Docker buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log into registry ${{ env.REGISTRY }} | |
| if: github.event_name != 'pull_request' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| id: build-and-push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ github.ref }}-${{ matrix.arch }} | |
| platforms: matrix.platform | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| merge_manifests: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create and push multi-arch manifest | |
| run: | | |
| docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref }} \ | |
| --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ github.ref }}-amd64 \ | |
| --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ github.ref }}-arm64 | |
| docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref }} | |
| # Optionally tag 'latest' on main | |
| if [[ "${GITHUB_REF}" == "refs/heads/main" ]]; then | |
| docker manifest create ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \ | |
| --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref }}-amd64 \ | |
| --amend ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref }}-arm64 | |
| docker manifest push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest | |
| fi |