Skip to content

Commit ba3c74a

Browse files
committed
feat: add reusable Docker build workflow and image publishing process
1 parent 8c55e84 commit ba3c74a

2 files changed

Lines changed: 95 additions & 77 deletions

File tree

.github/workflows/build-docker.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Reusable Docker Build
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
platform-runner:
7+
description: 'The runner to use (e.g., ubuntu-latest, ubuntu-24.04-arm)'
8+
required: true
9+
type: string
10+
platform-name:
11+
description: 'The Docker platform name (e.g., linux/amd64, linux/arm64)'
12+
required: true
13+
type: string
14+
image-name:
15+
description: 'The name of the image to build'
16+
required: true
17+
type: string
18+
secrets:
19+
GITHUB_TOKEN:
20+
required: true
21+
22+
jobs:
23+
build:
24+
runs-on: ${{ inputs.platform-runner }}
25+
env:
26+
IMAGE_NAME: ${{ inputs.image-name }}
27+
permissions:
28+
contents: read
29+
packages: write
30+
steps:
31+
- name: Checkout repository
32+
uses: actions/checkout@v4
33+
34+
- name: Log in to the Container registry
35+
uses: docker/login-action@v3
36+
with:
37+
registry: ghcr.io
38+
username: ${{ github.actor }}
39+
password: ${{ secrets.GITHUB_TOKEN }}
40+
41+
- name: Extract metadata (tags, labels) for Docker
42+
id: meta
43+
uses: docker/metadata-action@v5
44+
with:
45+
images: ${{ env.IMAGE_NAME }}
46+
47+
- name: Set up Docker Buildx
48+
uses: docker/setup-buildx-action@v3
49+
50+
- name: Build and push Docker image
51+
uses: docker/build-push-action@v6
52+
with:
53+
context: .
54+
platforms: ${{ inputs.platform-name }}
55+
labels: ${{ steps.meta.outputs.labels }}
56+
outputs: type=image,"name=${{ env.IMAGE_NAME }}",push-by-digest=true,name-canonical=true,push=true
57+
58+
- name: Export digest
59+
run: |
60+
mkdir -p ${{ runner.temp }}/digests
61+
digest="${{ steps.build.outputs.digest }}"
62+
touch "${{ runner.temp }}/digests/${digest#sha256:}"
63+
64+
- name: Upload digest
65+
uses: actions/upload-artifact@v4
66+
with:
67+
name: digests-${{ inputs.platform-name }} # Use platform name for unique artifact
68+
path: ${{ runner.temp }}/digests/*
69+
if-no-files-found: error
70+
retention-days: 7
Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ on:
44
workflow_dispatch:
55
workflow_run:
66
workflows: ["Playwright Tests"]
7-
types:
8-
- completed
7+
types: [completed]
98
branches: [development, production]
109

1110
env:
@@ -33,83 +32,38 @@ jobs:
3332
- name: Convert image name
3433
id: step1
3534
run: |
36-
echo "Current image name:"
37-
echo $UPPER_IMAGE_NAME
3835
echo "IMAGE_NAME=$(echo $UPPER_IMAGE_NAME | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
39-
echo "Converted image name:"
40-
echo $(echo $UPPER_IMAGE_NAME | tr '[:upper:]' '[:lower:]')
41-
echo "Step output:"
42-
echo $GITHUB_OUTPUT
4336
44-
build:
45-
runs-on: ubuntu-latest
46-
needs: prepare
47-
strategy:
48-
matrix:
49-
platform:
50-
- linux/amd64
51-
- linux/arm64
52-
env:
53-
IMAGE_NAME: ${{ needs.prepare.outputs.image-name }}
54-
permissions:
55-
contents: read
56-
packages: write
57-
steps:
58-
- name: Prepare
59-
run: |
60-
platform=${{ matrix.platform }}
61-
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
62-
63-
- name: Checkout repository
64-
uses: actions/checkout@v4
65-
66-
- name: Log in to the Container registry
67-
uses: docker/login-action@v3
68-
with:
69-
registry: ${{ env.REGISTRY }}
70-
username: ${{ github.actor }}
71-
password: ${{ secrets.GITHUB_TOKEN }}
37+
# Dispatch builds
7238

73-
- name: Extract metadata (tags, labels) for Docker
74-
id: meta
75-
uses: docker/metadata-action@v5
76-
with:
77-
images: ${{ env.IMAGE_NAME }}
78-
79-
- name: Set up QEMU
80-
uses: docker/setup-qemu-action@v3
81-
82-
- name: Set up Docker Buildx
83-
uses: docker/setup-buildx-action@v3
84-
85-
- name: Build and push by digest
86-
id: build
87-
uses: docker/build-push-action@v6
88-
with:
89-
context: .
90-
platforms: ${{ matrix.platform }}
91-
labels: ${{ steps.meta.outputs.labels }}
92-
outputs: type=image,"name=${{ env.IMAGE_NAME }}",push-by-digest=true,name-canonical=true,push=true
93-
94-
- name: Export digest
95-
run: |
96-
mkdir -p ${{ runner.temp }}/digests
97-
digest="${{ steps.build.outputs.digest }}"
98-
touch "${{ runner.temp }}/digests/${digest#sha256:}"
39+
build-amd64:
40+
name: Build AMD64 Image
41+
uses: ./.github/workflows/build-docker.yml
42+
needs: prepare
43+
with:
44+
platform-runner: ubuntu-latest
45+
platform-name: linux/amd64
46+
image-name: ${{ needs.prepare.outputs.image-name }}
47+
secrets: inherit
48+
49+
build-arm64:
50+
name: Build ARM64 Image
51+
uses: ./.github/workflows/build-docker.yml
52+
needs: prepare
53+
with:
54+
platform-runner: ubuntu-24.04-arm
55+
platform-name: linux/arm64
56+
image-name: ${{ needs.prepare.outputs.image-name }}
57+
secrets: inherit
9958

100-
- name: Upload digest
101-
uses: actions/upload-artifact@v4
102-
with:
103-
name: digests-${{ env.PLATFORM_PAIR }}
104-
path: ${{ runner.temp }}/digests/*
105-
if-no-files-found: error
106-
retention-days: 7
59+
# Merge and publish multi-arch image
10760

10861
merge:
10962
runs-on: ubuntu-latest
11063
needs:
11164
- prepare
112-
- build
65+
- build-amd64
66+
- build-arm64
11367
env:
11468
IMAGE_NAME: ${{ needs.prepare.outputs.image-name }}
11569
BUILDX_NO_DEFAULT_ATTESTATIONS: 1
@@ -140,17 +94,11 @@ jobs:
14094
with:
14195
images: ${{ env.IMAGE_NAME }}
14296
tags: |
143-
# for example: develop
14497
type=ref,event=branch
145-
# for example: pr-35
14698
type=ref,event=pr
147-
# for example: 2.3.4
14899
type=semver,pattern={{version}}
149-
# for example: 2.3
150100
type=semver,pattern={{major}}.{{minor}}
151-
# for example: 2
152101
type=semver,pattern={{major}}
153-
# set 'latest' for new tags
154102
type=raw,value=latest,enable=${{ startsWith( github.ref, 'refs/tags/' ) }}
155103
156104
- name: Create manifest list and push
@@ -160,4 +108,4 @@ jobs:
160108
$(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)
161109
162110
- name: Inspect image
163-
run: docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
111+
run: docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}

0 commit comments

Comments
 (0)