1name: Docker image
2
3on:
4 workflow_dispatch:
5 push:
6 branches:
7 - main
8 tags:
9 - 'v*'
10
11env:
12 REGISTRY: ghcr.io
13 IMAGE_NAME: ${{ github.repository }}
14
15jobs:
16 build-and-push-image:
17 strategy:
18 matrix:
19 include:
20 - arch: amd64
21 runner: ubuntu-latest
22 - arch: arm64
23 runner: ubuntu-24.04-arm
24 runs-on: ${{ matrix.runner }}
25 # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
26 permissions:
27 contents: read
28 packages: write
29 attestations: write
30 id-token: write
31 outputs:
32 digest-amd64: ${{ matrix.arch == 'amd64' && steps.push.outputs.digest || '' }}
33 digest-arm64: ${{ matrix.arch == 'arm64' && steps.push.outputs.digest || '' }}
34 steps:
35 - name: Checkout repository
36 uses: actions/checkout@v4
37
38 # 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.
39 - name: Log in to the Container registry
40 uses: docker/login-action@v3
41 with:
42 registry: ${{ env.REGISTRY }}
43 username: ${{ github.actor }}
44 password: ${{ secrets.GITHUB_TOKEN }}
45
46 # 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.
47 - name: Extract metadata (tags, labels) for Docker
48 id: meta
49 uses: docker/metadata-action@v5
50 with:
51 images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
52 tags: |
53 type=raw,value=latest,enable={{is_default_branch}},suffix=-${{ matrix.arch }}
54 type=sha,suffix=-${{ matrix.arch }}
55 type=sha,format=long,suffix=-${{ matrix.arch }}
56 type=semver,pattern={{version}},suffix=-${{ matrix.arch }}
57 type=semver,pattern={{major}}.{{minor}},suffix=-${{ matrix.arch }}
58
59 # 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.
60 # 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.
61 # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
62 - name: Build and push Docker image
63 id: push
64 uses: docker/build-push-action@v6
65 with:
66 context: .
67 push: true
68 tags: ${{ steps.meta.outputs.tags }}
69 labels: ${{ steps.meta.outputs.labels }}
70
71 publish-manifest:
72 needs: build-and-push-image
73 runs-on: ubuntu-latest
74 permissions:
75 packages: write
76 attestations: write
77 id-token: write
78 steps:
79 - name: Log in to the Container registry
80 uses: docker/login-action@v3
81 with:
82 registry: ${{ env.REGISTRY }}
83 username: ${{ github.actor }}
84 password: ${{ secrets.GITHUB_TOKEN }}
85
86 - name: Extract metadata (tags, labels) for Docker
87 id: meta
88 uses: docker/metadata-action@v5
89 with:
90 images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
91 tags: |
92 type=raw,value=latest,enable={{is_default_branch}}
93 type=sha
94 type=sha,format=long
95 type=semver,pattern={{version}}
96 type=semver,pattern={{major}}.{{minor}}
97
98 - name: Create and push manifest
99 run: |
100 # Split tags into an array
101 readarray -t tags <<< "${{ steps.meta.outputs.tags }}"
102
103 # Create and push manifest for each tag
104 for tag in "${tags[@]}"; do
105 docker buildx imagetools create -t "$tag" \
106 "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-push-image.outputs.digest-amd64 }}" \
107 "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ needs.build-and-push-image.outputs.digest-arm64 }}"
108 done
109
110 # 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)."
111 - name: Generate artifact attestation
112 uses: actions/attest-build-provenance@v1
113 with:
114 subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
115 subject-digest: ${{ needs.build-and-push-image.outputs.digest-amd64 }}
116 push-to-registry: true