Skip to content

Commit eaf3875

Browse files
authored
[+] use a dedicated action for Docker builds (#717)
1 parent b47b4a2 commit eaf3875

File tree

4 files changed

+175
-69
lines changed

4 files changed

+175
-69
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
name: 'Build and Push Docker Images'
2+
description: 'Builds multi-arch Docker images with caching and SemVer tagging using official Docker actions'
3+
4+
inputs:
5+
dockerfile:
6+
description: 'Path to Dockerfile (relative to repository root)'
7+
required: true
8+
image-name:
9+
description: 'Docker image name (without registry prefix)'
10+
required: true
11+
registry:
12+
description: 'Docker registry (docker.io or ghcr.io)'
13+
required: false
14+
default: 'docker.io'
15+
username:
16+
description: 'Docker registry username'
17+
required: true
18+
password:
19+
description: 'Docker registry password/token'
20+
required: true
21+
platforms:
22+
description: 'Target platforms for multi-arch build'
23+
required: false
24+
default: 'linux/amd64,linux/arm64'
25+
push:
26+
description: 'Whether to push the image (true/false)'
27+
required: false
28+
default: 'false'
29+
build-args:
30+
description: 'Build arguments as KEY=VALUE pairs (one per line)'
31+
required: false
32+
default: ''
33+
34+
cache-scope:
35+
description: 'Cache scope for build cache'
36+
required: false
37+
default: 'default'
38+
39+
outputs:
40+
image-id:
41+
description: 'Image ID of the built image'
42+
value: ${{ steps.build.outputs.imageid }}
43+
digest:
44+
description: 'Image digest of the built image'
45+
value: ${{ steps.build.outputs.digest }}
46+
metadata:
47+
description: 'Build result metadata'
48+
value: ${{ steps.build.outputs.metadata }}
49+
50+
runs:
51+
using: 'composite'
52+
steps:
53+
- name: Set up QEMU
54+
uses: docker/setup-qemu-action@v3
55+
with:
56+
platforms: all
57+
58+
- name: Set up Docker Buildx
59+
uses: docker/setup-buildx-action@v3
60+
with:
61+
driver-opts: |
62+
network=host
63+
64+
- name: Log in to Docker Registry
65+
if: inputs.push == 'true'
66+
uses: docker/login-action@v3
67+
with:
68+
registry: ${{ inputs.registry == 'docker.io' && '' || inputs.registry }}
69+
username: ${{ inputs.username }}
70+
password: ${{ inputs.password }}
71+
72+
- name: Extract metadata
73+
id: meta
74+
uses: docker/metadata-action@v5
75+
with:
76+
images: ${{ inputs.image-name }}
77+
tags: |
78+
# For releases: apply SemVer tags and latest (excludes pre-releases automatically)
79+
type=semver,pattern={{version}}
80+
type=semver,pattern={{major}}.{{minor}}
81+
type=semver,pattern={{major}}
82+
type=semver,pattern=latest
83+
# For branches: branch name as tag
84+
type=ref,event=branch
85+
# For PRs: pr-<number>
86+
type=ref,event=pr
87+
# SHA for unique identification
88+
type=sha,prefix=sha-,format=short
89+
labels: |
90+
org.opencontainers.image.title=${{ inputs.image-name }}
91+
org.opencontainers.image.description=PgWatch - PostgreSQL monitoring solution
92+
org.opencontainers.image.vendor=Cybertec PostgreSQL International GmbH
93+
org.opencontainers.image.licenses=BSD-3-Clause
94+
95+
- name: Build and push Docker image
96+
id: build
97+
uses: docker/build-push-action@v5
98+
with:
99+
context: .
100+
file: ${{ inputs.dockerfile }}
101+
platforms: ${{ inputs.platforms }}
102+
push: ${{ inputs.push == 'true' }}
103+
tags: ${{ steps.meta.outputs.tags }}
104+
labels: ${{ steps.meta.outputs.labels }}
105+
build-args: ${{ inputs.build-args }}
106+
cache-from: type=gha,scope=${{ inputs.cache-scope }}
107+
cache-to: type=gha,mode=max,scope=${{ inputs.cache-scope }}
108+
provenance: false
109+
sbom: false
110+
111+
- name: Output image details
112+
shell: bash
113+
run: |
114+
echo "🐳 Built image: ${{ inputs.image-name }}"
115+
echo "📋 Tags:"
116+
echo "${{ steps.meta.outputs.tags }}" | sed 's/^/ - /'
117+
echo "🔍 Digest: ${{ steps.build.outputs.digest }}"
118+
echo "🆔 Image ID: ${{ steps.build.outputs.imageid }}"

.github/workflows/build.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,34 @@ jobs:
197197
echo "push_opt=--push" >> $GITHUB_ENV
198198
199199
- name: Build mkdocs
200-
run: mike deploy ${{ env.push_opt }} devel
200+
run: mike deploy ${{ env.push_opt }} devel
201+
202+
test-docker-images:
203+
if: true # false to skip job during debug
204+
needs: [test-postgresql-ubuntu, test-postgresql-windows, test-postgresql-macos]
205+
name: Test Docker Image Build
206+
runs-on: ubuntu-latest
207+
steps:
208+
209+
- name: Check out code
210+
uses: actions/checkout@v5
211+
212+
- name: Prepare build metadata
213+
id: meta
214+
run: |
215+
echo "GIT_HASH=${{ github.sha }}" >> $GITHUB_OUTPUT
216+
echo "GIT_TIME=$(git show -s --format=%cI HEAD)" >> $GITHUB_OUTPUT
217+
echo "VERSION=${{ github.ref_name }}" >> $GITHUB_OUTPUT
218+
219+
- name: Test build Docker image (fast, amd64 only)
220+
uses: ./.github/actions/build-docker
221+
with:
222+
dockerfile: Dockerfile
223+
image-name: cybertecpostgresql/pg_timetable
224+
platforms: linux/amd64
225+
push: 'false'
226+
cache-scope: test-build
227+
build-args: |
228+
COMMIT=${{ steps.meta.outputs.GIT_HASH }}
229+
DATE=${{ steps.meta.outputs.GIT_TIME }}
230+
VERSION=${{ steps.meta.outputs.VERSION }}

.github/workflows/docker.yml

Lines changed: 0 additions & 56 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
name: Release
2+
3+
permissions:
4+
contents: write
5+
packages: write
6+
pages: write
7+
deployments: write
8+
pull-requests: write
9+
issues: write
10+
211
on:
312
release:
413
types: [created]
@@ -38,20 +47,25 @@ jobs:
3847
- name: Check out code into the Go module directory
3948
uses: actions/checkout@v5
4049

41-
- name: Version strings
42-
id: version
50+
- name: Prepare build metadata
51+
id: meta
4352
run: |
44-
echo "RELEASE_TIME=$(git show -s --format=%cI HEAD)" >> $GITHUB_OUTPUT
53+
echo "GIT_HASH=${{ github.sha }}" >> $GITHUB_OUTPUT
54+
echo "GIT_TIME=$(git show -s --format=%cI HEAD)" >> $GITHUB_OUTPUT
55+
echo "VERSION=${{ github.ref_name }}" >> $GITHUB_OUTPUT
4556
46-
- name: Publish to Registry
47-
uses: elgohr/Publish-Docker-Github-Action@v5
48-
env:
49-
VERSION: ${{ github.ref_name }}
50-
COMMIT: ${{ github.sha }}
51-
DATE: ${{ steps.version.outputs.RELEASE_TIME }}
57+
- name: Build and push Docker image
58+
uses: ./.github/actions/build-docker
5259
with:
53-
name: cybertecpostgresql/pg_timetable
60+
dockerfile: Dockerfile
61+
image-name: cybertecpostgresql/pg_timetable
62+
registry: docker.io
5463
username: ${{ secrets.DOCKER_USERNAME }}
5564
password: ${{ secrets.DOCKER_PASSWORD }}
56-
buildargs: VERSION,COMMIT,DATE
57-
tag_semver: true
65+
platforms: linux/amd64,linux/arm64
66+
push: 'true'
67+
cache-scope: shared-release
68+
build-args: |
69+
COMMIT=${{ steps.meta.outputs.GIT_HASH }}
70+
DATE=${{ steps.meta.outputs.GIT_TIME }}
71+
VERSION=${{ steps.meta.outputs.VERSION }}

0 commit comments

Comments
 (0)