Skip to content

Commit 4534615

Browse files
committed
feat: Add reusable workflow for building Docker images across multiple platforms
1 parent 59629bf commit 4534615

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
name: Build Docker Images (Reusable)
2+
description: Reusable workflow to build Docker images for multiple platforms and stages
3+
4+
on:
5+
workflow_call:
6+
inputs:
7+
push:
8+
description: Whether to push the image to a registry
9+
type: boolean
10+
default: false
11+
tags_publish_amd64:
12+
description: Docker tag for publish image on amd64
13+
type: string
14+
default: 'local/openrouteservice:test'
15+
tags_publish_arm64:
16+
description: Docker tag for publish image on arm64
17+
type: string
18+
default: 'local/openrouteservice:test'
19+
tags_minimal_amd64:
20+
description: Docker tag for minimal image on amd64
21+
type: string
22+
default: 'local/openrouteservice:test-minimal'
23+
tags_minimal_arm64:
24+
description: Docker tag for minimal image on arm64
25+
type: string
26+
default: 'local/openrouteservice:test-minimal'
27+
cache_from_type:
28+
description: Cache backend type (default gha for GitHub Actions)
29+
type: string
30+
default: 'gha'
31+
java_version:
32+
description: Java version to use for Maven builds
33+
type: string
34+
default: '21'
35+
skip_arm64:
36+
description: Skip ARM64 builds
37+
type: boolean
38+
default: false
39+
outputs:
40+
dockerfile_hash:
41+
description: Hash of the Dockerfile
42+
value: ${{ jobs.prepare_environment.outputs.dockerfile_hash }}
43+
44+
jobs:
45+
prepare_environment:
46+
name: Prepare the environment variables
47+
runs-on: ubuntu-latest
48+
outputs:
49+
dockerfile_hash: ${{ steps.dockerfile-hash.outputs.hash }}
50+
steps:
51+
- name: Checkout
52+
uses: actions/checkout@v4
53+
- name: Generate Dockerfile hash
54+
id: dockerfile-hash
55+
run: |
56+
HASH=$(sha256sum Dockerfile | cut -d' ' -f1 | cut -c1-8)
57+
echo "hash=$HASH" >> $GITHUB_OUTPUT
58+
59+
prepare_maven_dependencies:
60+
name: Prepare Maven dependencies for ${{ matrix.name }}
61+
runs-on: ${{ matrix.runner }}
62+
needs:
63+
- prepare_environment
64+
strategy:
65+
matrix:
66+
include:
67+
- platform: linux/amd64
68+
name: linux-amd64
69+
runner: ubuntu-latest
70+
- platform: linux/arm64
71+
name: linux-arm64
72+
runner: ubuntu-24.04-arm
73+
steps:
74+
- name: Checkout
75+
uses: actions/checkout@v4
76+
with:
77+
fetch-depth: 0
78+
- name: Setup Java and Maven
79+
uses: ./.github/actions/setup-java-maven
80+
with:
81+
java_version: ${{ inputs.java_version }}
82+
83+
build_docker_images:
84+
name: Build ${{ matrix.image_stage }} for ${{ matrix.name }}
85+
runs-on: ${{ matrix.runner }}
86+
needs:
87+
- prepare_environment
88+
- prepare_maven_dependencies
89+
strategy:
90+
matrix:
91+
include:
92+
- platform: linux/amd64
93+
name: linux-amd64
94+
runner: ubuntu-latest
95+
image_stage: publish
96+
docker_tag_input: tags_publish_amd64
97+
- platform: linux/arm64
98+
name: linux-arm64
99+
runner: ubuntu-24.04-arm
100+
image_stage: publish
101+
docker_tag_input: tags_publish_arm64
102+
- platform: linux/amd64
103+
name: linux-amd64
104+
runner: ubuntu-latest
105+
image_stage: minimal
106+
docker_tag_input: tags_minimal_amd64
107+
- platform: linux/arm64
108+
name: linux-arm64
109+
runner: ubuntu-24.04-arm
110+
image_stage: minimal
111+
docker_tag_input: tags_minimal_arm64
112+
steps:
113+
- name: Check if should skip (ARM64)
114+
id: should_skip
115+
run: |
116+
SKIP="false"
117+
# Skip ARM64 builds if requested
118+
if [[ "${{ matrix.name }}" == "linux-arm64" && "${{ inputs.skip_arm64 }}" == "true" ]]; then
119+
SKIP="true"
120+
fi
121+
echo "skip=$SKIP" >> $GITHUB_OUTPUT
122+
123+
- name: Checkout
124+
if: steps.should_skip.outputs.skip != 'true'
125+
uses: actions/checkout@v4
126+
with:
127+
fetch-depth: 0
128+
129+
- name: Get and save the UID
130+
if: steps.should_skip.outputs.skip != 'true'
131+
run: |
132+
echo "UID=$(id -u)" >> $GITHUB_ENV
133+
134+
- name: Set up Docker Buildx
135+
if: steps.should_skip.outputs.skip != 'true'
136+
uses: docker/setup-buildx-action@v3
137+
id: buildx
138+
139+
- name: Setup Java and Maven
140+
if: steps.should_skip.outputs.skip != 'true'
141+
uses: ./.github/actions/setup-java-maven
142+
with:
143+
java_version: ${{ inputs.java_version }}
144+
145+
- name: Maven cache dance
146+
if: steps.should_skip.outputs.skip != 'true'
147+
uses: ./.github/actions/maven-cache-dance
148+
with:
149+
builder: ${{ steps.buildx.outputs.name }}
150+
dockerfile: 'Dockerfile'
151+
cache-hit: 'false'
152+
153+
- name: Resolve Docker tag
154+
if: steps.should_skip.outputs.skip != 'true'
155+
id: resolve_tag
156+
run: |
157+
# Map the docker_tag_input to the actual input value
158+
TAG_INPUT="${{ matrix.docker_tag_input }}"
159+
case "$TAG_INPUT" in
160+
tags_publish_amd64) TAG="${{ inputs.tags_publish_amd64 }}" ;;
161+
tags_publish_arm64) TAG="${{ inputs.tags_publish_arm64 }}" ;;
162+
tags_minimal_amd64) TAG="${{ inputs.tags_minimal_amd64 }}" ;;
163+
tags_minimal_arm64) TAG="${{ inputs.tags_minimal_arm64 }}" ;;
164+
*) TAG="local/openrouteservice:test" ;;
165+
esac
166+
echo "tag=$TAG" >> $GITHUB_OUTPUT
167+
168+
- name: Determine Docker output
169+
if: steps.should_skip.outputs.skip != 'true'
170+
id: output
171+
run: |
172+
if [ "${{ inputs.push }}" == "true" ]; then
173+
echo "type=docker" >> $GITHUB_OUTPUT
174+
else
175+
echo "type=docker,dest=${{ runner.temp }}/image-${{ matrix.name }}-${{ matrix.image_stage }}.tar" >> $GITHUB_OUTPUT
176+
fi
177+
178+
- name: Build ${{ matrix.image_stage }} image stage for ${{ matrix.name }}
179+
if: steps.should_skip.outputs.skip != 'true'
180+
uses: docker/build-push-action@v6
181+
with:
182+
context: .
183+
build-args: MAVEN_OPTS=-Dmaven.repo.local=/root/.m2/repository
184+
target: ${{ matrix.image_stage }}
185+
push: ${{ inputs.push }}
186+
load: false
187+
tags: ${{ steps.resolve_tag.outputs.tag }}
188+
platforms: "${{ matrix.platform }}"
189+
cache-from: type=${{ inputs.cache_from_type }}
190+
cache-to: type=${{ inputs.cache_from_type }},mode=max
191+
outputs: ${{ steps.output.outputs.type }}
192+
193+
- name: Upload image artifact
194+
if: steps.should_skip.outputs.skip != 'true' && inputs.push != true
195+
uses: actions/upload-artifact@v4
196+
with:
197+
name: image-${{ matrix.name }}-${{ matrix.image_stage }}-${{ needs.prepare_environment.outputs.dockerfile_hash }}-artifact
198+
path: ${{ runner.temp }}/image-${{ matrix.name }}-${{ matrix.image_stage }}.tar
199+
retention-days: 1
200+
if-no-files-found: error
201+
compression-level: 0
202+
overwrite: true

0 commit comments

Comments
 (0)