Skip to content

Commit 15508c3

Browse files
committed
chore(ci): add GitHub Action for Docker build
- actions/checkout@v4 (v4.1.7) - docker/setup-buildx-action@v3 (v3.6.1) - docker/login-action@v3 (v3.3.0) - docker/metadata-action@v5 (v5.5.1) - docker/build-push-action@v6 (v6.8.0) Special action from community: - jlumbroso/free-disk-space@v1.3.1 Signed-off-by: Stephan Linz <linz@li-pro.net>
1 parent b91be60 commit 15508c3

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

.github/workflows/docker-publish.yml

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
name: Docker
2+
3+
# Based on GitHub actions starter workflow for Docker publishing:
4+
# https://github.com/actions/starter-workflows/blob/main/ci/docker-publish.yml
5+
6+
# This workflow uses actions that are not certified by GitHub.
7+
# They are provided by a third-party and are governed by
8+
# separate terms of service, privacy policy, and support
9+
# documentation.
10+
11+
on:
12+
schedule:
13+
# every day at 00:15 UTC
14+
- cron: '15 0 * * *'
15+
push:
16+
branches:
17+
- 'main'
18+
# Publish calver or pep440 tags as releases.
19+
tags:
20+
- '[0-9]+.[0-9]+.[0-9]+'
21+
- '[0-9]+.[0-9]+.[0-9]+rc[0-9]+'
22+
pull_request:
23+
branches:
24+
- 'main'
25+
26+
env:
27+
# Use docker.io for Docker Hub if empty
28+
REGISTRY: ghcr.io
29+
# github.repository as <account>/<repo>
30+
IMAGE_NAME: ${{ github.repository }}
31+
32+
jobs:
33+
build:
34+
35+
runs-on: ubuntu-latest
36+
strategy:
37+
fail-fast: false
38+
matrix:
39+
platform:
40+
# Distribute build across multiple runners, use a matrix strategy.
41+
# https://docs.docker.com/build/ci/github-actions/multi-platform
42+
- linux/amd64
43+
- linux/arm/v7
44+
- linux/arm64/v8
45+
- linux/riscv64
46+
- linux/ppc64le
47+
- linux/s390x
48+
49+
steps:
50+
# Free Disk Space on Ubuntu runners, clear up to 35 GB disk space
51+
# https://github.com/jlumbroso/free-disk-space
52+
- name: Free disk space
53+
uses: jlumbroso/free-disk-space@v1.3.1
54+
with:
55+
# Tool cache: Saved 8.3GiB
56+
tool-cache: true
57+
# Android library: Saved 7.5GiB
58+
android: true
59+
# .NET runtime: Saved 1.6GiB
60+
dotnet: true
61+
# Haskell runtime: Saved 5.4GiB
62+
haskell: true
63+
# Large misc. packages: Saved 4.8GiB
64+
large-packages: true
65+
# Docker images: Saved 3.2GiB
66+
docker-images: true
67+
# Swap storage: Saved 4.0GiB
68+
swap-storage: true
69+
70+
- name: Prepare GitHub runner environment
71+
run: |
72+
platform=${{ matrix.platform }}
73+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
74+
75+
- name: Checkout repository
76+
uses: actions/checkout@v4
77+
78+
# Install QEMU static binaries for multi-arch image build
79+
# https://github.com/docker/setup-qemu-action
80+
# https://docs.docker.com/build/ci/github-actions/multi-platform
81+
- name: Set up QEMU
82+
uses: docker/setup-qemu-action@v3
83+
84+
# Set up BuildKit Docker container builder to be able to build
85+
# multi-platform images and export cache
86+
# https://github.com/docker/setup-buildx-action
87+
- name: Set up Docker Buildx
88+
uses: docker/setup-buildx-action@v3
89+
90+
# Login against a Docker registry except on PR
91+
# https://github.com/docker/login-action
92+
- name: Login to registry ${{ env.REGISTRY }}
93+
uses: docker/login-action@v3
94+
with:
95+
registry: ${{ env.REGISTRY }}
96+
username: ${{ github.actor }}
97+
password: ${{ secrets.GITHUB_TOKEN }}
98+
99+
# Extract metadata (tags, labels) for Docker
100+
# https://github.com/docker/metadata-action
101+
- name: Extract Docker metadata
102+
id: meta
103+
uses: docker/metadata-action@v5
104+
with:
105+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
106+
107+
# Build and push Docker image with Buildx (don't push on PR)
108+
# https://github.com/docker/build-push-action
109+
# https://docs.docker.com/build/ci/github-actions/cache
110+
- name: Build and push Docker image
111+
id: build-and-push
112+
uses: docker/build-push-action@v6
113+
with:
114+
context: .
115+
platforms: ${{ matrix.platform }}
116+
labels: ${{ steps.meta.outputs.labels }}
117+
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
118+
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache-${{ env.PLATFORM_PAIR }}
119+
cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:buildcache-${{ env.PLATFORM_PAIR }},mode=max
120+
121+
- name: Export digest
122+
run: |
123+
mkdir -p /tmp/digests
124+
digest="${{ steps.build-and-push.outputs.digest }}"
125+
touch "/tmp/digests/${digest#sha256:}"
126+
127+
- name: Upload digest
128+
uses: actions/upload-artifact@v4
129+
with:
130+
name: digests-${{ env.PLATFORM_PAIR }}
131+
path: /tmp/digests/*
132+
if-no-files-found: error
133+
retention-days: 1
134+
135+
merge:
136+
needs:
137+
- build
138+
139+
runs-on: ubuntu-latest
140+
permissions:
141+
contents: read
142+
packages: write
143+
# This is used to complete the identity challenge
144+
# with sigstore/fulcio when running outside of PRs.
145+
id-token: write
146+
147+
steps:
148+
# Free Disk Space on Ubuntu runners, clear up to 35 GB disk space
149+
# https://github.com/jlumbroso/free-disk-space
150+
- name: Free disk space
151+
uses: jlumbroso/free-disk-space@v1.3.1
152+
with:
153+
# Tool cache: Saved 8.3GiB
154+
tool-cache: true
155+
# Android library: Saved 7.5GiB
156+
android: true
157+
# .NET runtime: Saved 1.6GiB
158+
dotnet: true
159+
# Haskell runtime: Saved 5.4GiB
160+
haskell: true
161+
# Large misc. packages: Saved 4.8GiB
162+
large-packages: true
163+
# Docker images: Saved 3.2GiB
164+
docker-images: true
165+
# Swap storage: Saved 4.0GiB
166+
swap-storage: true
167+
168+
- name: Download digests
169+
uses: actions/download-artifact@v4
170+
with:
171+
path: /tmp/digests
172+
pattern: digests-*
173+
merge-multiple: true
174+
175+
# Set up BuildKit Docker container builder to be able to build
176+
# multi-platform images and export cache
177+
# https://github.com/docker/setup-buildx-action
178+
- name: Set up Docker Buildx
179+
uses: docker/setup-buildx-action@v3
180+
181+
# Login against a Docker registry except on PR
182+
# https://github.com/docker/login-action
183+
- name: Login to registry ${{ env.REGISTRY }}
184+
uses: docker/login-action@v3
185+
with:
186+
registry: ${{ env.REGISTRY }}
187+
username: ${{ github.actor }}
188+
password: ${{ secrets.GITHUB_TOKEN }}
189+
190+
# Extract metadata (tags, labels) for Docker
191+
# https://github.com/docker/metadata-action
192+
- name: Extract Docker metadata
193+
id: meta
194+
uses: docker/metadata-action@v5
195+
with:
196+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
197+
# generate Docker tags based on the following events/attributes
198+
tags: |
199+
type=schedule
200+
type=pep440,pattern={{version}}
201+
type=pep440,pattern={{major}}.{{minor}}
202+
type=pep440,pattern={{major}}
203+
type=ref,event=branch
204+
type=ref,event=pr
205+
206+
- name: Create manifest list and push
207+
working-directory: /tmp/digests
208+
run: |
209+
docker buildx imagetools create \
210+
$(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
211+
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
212+
213+
- name: Inspect image
214+
run: |
215+
docker buildx imagetools inspect \
216+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}

0 commit comments

Comments
 (0)