Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f868d35

Browse files
committedMar 5, 2025
workflow for publishing docker images on tag
Signed-off-by: Daniel Thamdrup <[email protected]>
1 parent 435015b commit f868d35

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
 

‎.github/workflows/docker-images.yml

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
name: Publish Docker images
3+
4+
concurrency:
5+
group: ${{ github.workflow }}-${{ github.ref }}
6+
cancel-in-progress: true
7+
8+
on:
9+
push:
10+
tags:
11+
- 'v*'
12+
13+
env:
14+
DH_REPO: ${{ github.repository }}
15+
GHCR_REPO: ghcr.io/${{ github.repository }}
16+
17+
jobs:
18+
build:
19+
strategy:
20+
fail-fast: true
21+
matrix:
22+
platform:
23+
- linux/amd64
24+
- linux/arm64/v8
25+
os:
26+
- ubuntu-latest
27+
- ubuntu-24.04-arm
28+
exclude:
29+
- os: ubuntu-latest
30+
platform: linux/arm64/v8
31+
- os: ubuntu-24.04-arm
32+
platform: linux/amd64
33+
runs-on: ${{ matrix.os }}
34+
steps:
35+
- name: Prepare
36+
run: |
37+
platform=${{ matrix.platform }}
38+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
39+
- name: Checkout source
40+
uses: actions/checkout@v4
41+
42+
- name: Docker meta
43+
id: meta
44+
uses: docker/metadata-action@v5
45+
with:
46+
images: |
47+
${{ env.DH_REPO }}
48+
${{ env.GHCR_REPO }}
49+
env:
50+
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,manifest-descriptor # Important for manifest annotation (used by Github packages)
51+
52+
- name: Login to Docker Hub
53+
uses: docker/login-action@v3
54+
with:
55+
username: ${{ secrets.DOCKERHUB_USERNAME }}
56+
password: ${{ secrets.DOCKERHUB_TOKEN }}
57+
58+
- name: Login to GHCR
59+
uses: docker/login-action@v3
60+
with:
61+
registry: ghcr.io
62+
username: ${{ github.repository_owner }}
63+
password: ${{ secrets.GITHUB_TOKEN }}
64+
65+
- name: Set up Docker Buildx
66+
uses: docker/setup-buildx-action@v3
67+
68+
- name: Build and push by digest
69+
id: build
70+
uses: docker/build-push-action@v6
71+
with:
72+
context: .
73+
platforms: ${{ matrix.platform }}
74+
labels: ${{ steps.meta.outputs.labels }}
75+
outputs: |
76+
type=image,"name=${{ env.DH_REPO }},${{ env.GHCR_REPO }}",push-by-digest=true,name-canonical=true,push=true
77+
attests: |
78+
type=sbom
79+
type=provenance,mode=max
80+
build-args: |
81+
TARGET_PLATFORM=${{ matrix.platform }}
82+
- name: Export digest
83+
run: |
84+
mkdir -p ${{ runner.temp }}/digests
85+
digest="${{ steps.build.outputs.digest }}"
86+
touch "${{ runner.temp }}/digests/${digest#sha256:}"
87+
- name: Upload digest
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: digests-${{ env.PLATFORM_PAIR }}
91+
path: ${{ runner.temp }}/digests/*
92+
if-no-files-found: error
93+
retention-days: 1
94+
95+
merge:
96+
runs-on: ubuntu-latest
97+
needs:
98+
- build
99+
steps:
100+
- name: Download digests
101+
uses: actions/download-artifact@v4
102+
with:
103+
path: ${{ runner.temp }}/digests
104+
pattern: digests-*
105+
merge-multiple: true
106+
107+
- name: Login to Docker Hub
108+
uses: docker/login-action@v3
109+
with:
110+
username: ${{ secrets.DOCKERHUB_USERNAME }}
111+
password: ${{ secrets.DOCKERHUB_TOKEN }}
112+
113+
- name: Login to GHCR
114+
uses: docker/login-action@v3
115+
with:
116+
registry: ghcr.io
117+
username: ${{ github.repository_owner }}
118+
password: ${{ secrets.GITHUB_TOKEN }}
119+
120+
- name: Set up Docker Buildx
121+
uses: docker/setup-buildx-action@v3
122+
123+
- name: Docker meta
124+
id: meta
125+
uses: docker/metadata-action@v5
126+
with:
127+
images: |
128+
${{ env.DH_REPO }}
129+
${{ env.GHCR_REPO }}
130+
tags: |
131+
type=ref,event=tag
132+
type=semver,pattern={{raw}}
133+
type=semver,pattern={{version}}
134+
type=semver,pattern={{major}}.{{minor}}
135+
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }}
136+
env:
137+
DOCKER_METADATA_ANNOTATIONS_LEVELS: index # Important for manifest annotation (used by Github packages)
138+
139+
- name: Create manifest list and push
140+
working-directory: ${{ runner.temp }}/digests
141+
run: |
142+
docker buildx imagetools create \
143+
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
144+
$(printf '${{ env.DH_REPO }}@sha256:%s ' *)
145+
docker buildx imagetools create \
146+
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
147+
$(printf '${{ env.GHCR_REPO }}@sha256:%s ' *)
148+
- name: Inspect image
149+
run: |
150+
docker buildx imagetools inspect ${{ env.DH_REPO }}:${{ steps.meta.outputs.version }}
151+
docker buildx imagetools inspect ${{ env.GHCR_REPO }}:${{ steps.meta.outputs.version }}
152+
...

0 commit comments

Comments
 (0)
Please sign in to comment.