···11-name: Docker image
22-33-on:
44- workflow_dispatch:
55- push:
66- branches:
77- - main
88- tags:
99- - 'v*'
1010-1111-env:
1212- REGISTRY: ghcr.io
1313- IMAGE_NAME: ${{ github.repository }}
1414-1515-jobs:
1616- build-and-push-image:
1717- strategy:
1818- matrix:
1919- include:
2020- - arch: amd64
2121- runner: ubuntu-latest
2222- - arch: arm64
2323- runner: ubuntu-24.04-arm
2424- runs-on: ${{ matrix.runner }}
2525- # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
2626- permissions:
2727- contents: read
2828- packages: write
2929- attestations: write
3030- id-token: write
3131- outputs:
3232- digest-amd64: ${{ matrix.arch == 'amd64' && steps.push.outputs.digest || '' }}
3333- digest-arm64: ${{ matrix.arch == 'arm64' && steps.push.outputs.digest || '' }}
3434- steps:
3535- - name: Checkout repository
3636- uses: actions/checkout@v4
3737-3838- # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
3939- - name: Log in to the Container registry
4040- uses: docker/login-action@v3
4141- with:
4242- registry: ${{ env.REGISTRY }}
4343- username: ${{ github.actor }}
4444- password: ${{ secrets.GITHUB_TOKEN }}
4545-4646- # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.
4747- - name: Extract metadata (tags, labels) for Docker
4848- id: meta
4949- uses: docker/metadata-action@v5
5050- with:
5151- images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
5252- tags: |
5353- type=raw,value=latest,enable={{is_default_branch}},suffix=-${{ matrix.arch }}
5454- type=sha,suffix=-${{ matrix.arch }}
5555- type=sha,format=long,suffix=-${{ matrix.arch }}
5656- type=semver,pattern={{version}},suffix=-${{ matrix.arch }}
5757- type=semver,pattern={{major}}.{{minor}},suffix=-${{ matrix.arch }}
5858-5959- # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
6060- # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
6161- # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
6262- - name: Build and push Docker image
6363- id: push
6464- uses: docker/build-push-action@v6
6565- with:
6666- context: .
6767- push: true
6868- tags: ${{ steps.meta.outputs.tags }}
6969- labels: ${{ steps.meta.outputs.labels }}
7070-7171- publish-manifest:
7272- needs: build-and-push-image
7373- runs-on: ubuntu-latest
7474- permissions:
7575- packages: write
7676- attestations: write
7777- id-token: write
7878- steps:
7979- - name: Log in to the Container registry
8080- uses: docker/login-action@v3
8181- with:
8282- registry: ${{ env.REGISTRY }}
8383- username: ${{ github.actor }}
8484- password: ${{ secrets.GITHUB_TOKEN }}
8585-8686- - name: Extract metadata (tags, labels) for Docker
8787- id: meta
8888- uses: docker/metadata-action@v5
8989- with:
9090- images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
9191- tags: |
9292- type=raw,value=latest,enable={{is_default_branch}}
9393- type=sha
9494- type=sha,format=long
9595- type=semver,pattern={{version}}
9696- type=semver,pattern={{major}}.{{minor}}
9797-9898- - name: Create and push manifest
9999- run: |
100100- # Split tags into an array
101101- readarray -t tags <<< "${{ steps.meta.outputs.tags }}"
102102-103103- # Create and push manifest for each tag
104104- for tag in "${tags[@]}"; do
105105- docker buildx imagetools create -t "$tag" \
106106- "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-push-image.outputs.digest-amd64 }}" \
107107- "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-push-image.outputs.digest-arm64 }}"
108108- done
109109-110110- # This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
111111- - name: Generate artifact attestation
112112- uses: actions/attest-build-provenance@v1
113113- with:
114114- subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
115115- subject-digest: ${{ needs.build-and-push-image.outputs.digest-amd64 }}
116116- push-to-registry: true