Skip to content

Commit dc986ed

Browse files
authored
Add reusable builder workflow (#328)
1 parent 6a35666 commit dc986ed

File tree

2 files changed

+149
-156
lines changed

2 files changed

+149
-156
lines changed

.github/workflows/builder.yml

Lines changed: 51 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ env:
1616
jobs:
1717
init:
1818
name: Initialize build
19-
runs-on: &runs-on-ubuntu ubuntu-24.04
19+
runs-on: ubuntu-latest
2020
outputs:
2121
architectures_alpine: ${{ steps.info_alpine.outputs.architectures }}
2222
architectures_debian: ${{ steps.info_debian.outputs.architectures }}
2323
architectures_ubuntu: ${{ steps.info_ubuntu.outputs.architectures }}
2424
release: ${{ steps.version.outputs.version }}
25+
alpine_latest: ${{ steps.set-latest.outputs.alpine_latest}}
26+
debian_latest: ${{ steps.set-latest.outputs.debian_latest}}
27+
ubuntu_latest: ${{ steps.set-latest.outputs.ubuntu_latest}}
28+
python_latest: ${{ steps.set-latest.outputs.python_latest}}
2529
steps:
2630
- name: Checkout the repository
2731
uses: actions/[email protected]
@@ -52,183 +56,74 @@ jobs:
5256
with:
5357
type: ${{ env.BUILD_TYPE }}
5458

59+
- name: Set latest tags
60+
id: set-latest
61+
run: |
62+
echo "alpine_latest=${{ env.ALPINE_LATEST }}" >> $GITHUB_ENV
63+
echo "debian_latest=${{ env.DEBIAN_LATEST }}" >> $GITHUB_ENV
64+
echo "ubuntu_latest=${{ env.UBUNTU_LATEST }}" >> $GITHUB_ENV
65+
echo "python_latest=${{ env.PYTHON_LATEST }}" >> $GITHUB_ENV
66+
5567
build_alpine:
56-
name: Build ${{ matrix.arch }} alpine ${{ matrix.version }} base image
68+
name: Alpine ${{ matrix.version }}
5769
needs: init
58-
runs-on: ${{ matrix.os }}
59-
permissions: &permissions
60-
contents: read
61-
id-token: write
62-
packages: write
6370
strategy:
6471
matrix:
65-
arch: ${{ fromJson(needs.init.outputs.architectures_alpine) }}
6672
version: ["3.20", "3.21", "3.22"]
67-
include: &matrix-include-build
68-
- os: *runs-on-ubuntu
69-
- arch: aarch64
70-
os: ubuntu-24.04-arm
71-
steps:
72-
- &checkout
73-
name: Checkout the repository
74-
uses: actions/[email protected]
75-
76-
- &login-container-registry
77-
name: Login to GitHub Container Registry
78-
if: github.event_name == 'release'
79-
uses: docker/login-action@v3
80-
with:
81-
registry: ghcr.io
82-
username: ${{ github.repository_owner }}
83-
password: ${{ secrets.GITHUB_TOKEN }}
84-
85-
- name: Set build arguments
86-
shell: bash
87-
run: |
88-
if [[ "${{ github.event_name }}" == "release" ]]; then
89-
BUILD_ARGS="--additional-tag ${{ matrix.version }}-${{ github.event.release.tag_name }}"
90-
if [[ "${{ env.ALPINE_LATEST }}" != "${{ matrix.version }}" ]]; then
91-
BUILD_ARGS="$BUILD_ARGS --no-latest"
92-
fi
93-
else
94-
BUILD_ARGS="--test"
95-
fi
96-
97-
echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV
98-
99-
- name: Build base image
100-
uses: home-assistant/[email protected]
101-
with:
102-
image: ${{ matrix.arch }}
103-
args: |
104-
$BUILD_ARGS \
105-
--${{ matrix.arch }} \
106-
--target /data/alpine \
107-
--cosign \
108-
--release ${{ needs.init.outputs.release }} \
109-
--base ${{ matrix.version }}
73+
uses: ./.github/workflows/reuseable-builder.yml
74+
with:
75+
target: alpine
76+
architectures: ${{ needs.init.outputs.architectures_alpine }}
77+
version: ${{ matrix.version }}
78+
release_name: ${{ needs.init.outputs.release }}
79+
tag_latest: ${{ needs.init.outputs.alpine_latest }}
11080

11181
build_debian:
112-
name: Build ${{ matrix.arch }} debian ${{ matrix.version }} base image
82+
name: Debian ${{ matrix.version }}
11383
needs: init
114-
runs-on: ${{ matrix.os }}
115-
permissions: *permissions
11684
strategy:
85+
fail-fast: false
11786
matrix:
118-
arch: ${{ fromJson(needs.init.outputs.architectures_debian) }}
11987
version: ["bookworm", "trixie"]
120-
include: *matrix-include-build
121-
steps:
122-
- *checkout
123-
- *login-container-registry
124-
125-
- name: Set build arguments
126-
shell: bash
127-
run: |
128-
if [[ "${{ github.event_name }}" == "release" ]]; then
129-
BUILD_ARGS="--additional-tag ${{ matrix.version }}-${{ github.event.release.tag_name }}"
130-
if [[ "${{ env.DEBIAN_LATEST }}" != "${{ matrix.version }}" ]]; then
131-
BUILD_ARGS="$BUILD_ARGS --no-latest"
132-
fi
133-
else
134-
BUILD_ARGS="--test"
135-
fi
136-
137-
echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV
138-
139-
- name: Build base image
140-
uses: home-assistant/[email protected]
141-
with:
142-
image: ${{ matrix.arch }}
143-
args: |
144-
$BUILD_ARGS \
145-
--${{ matrix.arch }} \
146-
--target /data/debian \
147-
--cosign \
148-
--release ${{ needs.init.outputs.release }} \
149-
--version-from ${{ matrix.version }}-slim \
150-
--base ${{ matrix.version }}
88+
uses: ./.github/workflows/reuseable-builder.yml
89+
with:
90+
target: debian
91+
architectures: ${{ needs.init.outputs.architectures_debian }}
92+
version: ${{ matrix.version }}
93+
release_name: ${{ needs.init.outputs.release }}
94+
version_from: ${{ matrix.version }}-slim
95+
tag_latest: ${{ needs.init.outputs.debian_latest }}
15196

15297
build_ubuntu:
153-
name: Build ${{ matrix.arch }} ubuntu ${{ matrix.version }} base image
98+
name: Ubuntu ${{ matrix.version }}
15499
needs: init
155-
runs-on: ${{ matrix.os }}
156-
permissions: *permissions
157100
strategy:
101+
fail-fast: false
158102
matrix:
159-
arch: ${{ fromJson(needs.init.outputs.architectures_ubuntu) }}
160103
version: ["16.04", "18.04", "20.04", "22.04", "24.04"]
161-
include: *matrix-include-build
162-
steps:
163-
- *checkout
164-
- *login-container-registry
165-
166-
- name: Set build arguments
167-
shell: bash
168-
run: |
169-
if [[ "${{ github.event_name }}" == "release" ]]; then
170-
BUILD_ARGS="--additional-tag ${{ matrix.version }}-${{ github.event.release.tag_name }}"
171-
if [[ "${{ env.UBUNTU_LATEST }}" != "${{ matrix.version }}" ]]; then
172-
BUILD_ARGS="$BUILD_ARGS --no-latest"
173-
fi
174-
else
175-
BUILD_ARGS="--test"
176-
fi
177-
178-
echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV
179-
180-
- name: Build base image
181-
uses: home-assistant/[email protected]
182-
with:
183-
image: ${{ matrix.arch }}
184-
args: |
185-
$BUILD_ARGS \
186-
--${{ matrix.arch }} \
187-
--target /data/ubuntu \
188-
--cosign \
189-
--release ${{ needs.init.outputs.release }} \
190-
--base ${{ matrix.version }}
104+
uses: ./.github/workflows/reuseable-builder.yml
105+
with:
106+
target: ubuntu
107+
architectures: ${{ needs.init.outputs.architectures_ubuntu }}
108+
version: ${{ matrix.version }}
109+
release_name: ${{ needs.init.outputs.release }}
110+
tag_latest: ${{ needs.init.outputs.ubuntu_latest }}
191111

192112
build_python:
193-
name: Build ${{ matrix.arch }} alpine ${{ matrix.version }} - python ${{ matrix.python }} base image
113+
name: Alpine ${{ matrix.version }} - python ${{ matrix.python }}
194114
needs: [init, build_alpine]
195-
runs-on: ${{ matrix.os }}
196-
permissions: *permissions
197115
strategy:
198116
fail-fast: false
199117
matrix:
200-
arch: ${{ fromJson(needs.init.outputs.architectures_alpine) }}
201118
version: ["3.20", "3.21", "3.22"]
202119
python: ["3.12", "3.13", "3.14"]
203-
include: *matrix-include-build
204-
steps:
205-
- *checkout
206-
- *login-container-registry
207-
208-
- name: Set build arguments
209-
shell: bash
210-
run: |
211-
if [[ "${{ github.event_name }}" == "release" ]]; then
212-
BUILD_ARGS="--additional-tag ${{ matrix.python }}-alpine${{ matrix.version }}-${{ github.event.release.tag_name }}"
213-
if [[ "${{ env.ALPINE_LATEST }}" != "${{ matrix.version }}" ]] || [[ "${{ env.PYTHON_LATEST }}" != "${{ matrix.python }}" ]]; then
214-
BUILD_ARGS="$BUILD_ARGS --no-latest"
215-
fi
216-
else
217-
BUILD_ARGS="--test"
218-
fi
219-
220-
echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV
221-
222-
- name: Build base image
223-
uses: home-assistant/[email protected]
224-
with:
225-
image: ${{ matrix.arch }}
226-
args: |
227-
$BUILD_ARGS \
228-
--${{ matrix.arch }} \
229-
--target "/data/python/${{ matrix.python }}" \
230-
--cosign \
231-
--release ${{ needs.init.outputs.release }} \
232-
--version-from ${{ matrix.version }} \
233-
--version ${{ matrix.python }} \
234-
--base ${{ matrix.python }}-alpine${{ matrix.version }}
120+
uses: ./.github/workflows/reuseable-builder.yml
121+
with:
122+
target: python/${{ matrix.python }}
123+
architectures: ${{ needs.init.outputs.architectures_alpine }}
124+
version: ${{ matrix.version }}
125+
python: ${{ matrix.python }}
126+
release_name: ${{ needs.init.outputs.release }}
127+
version_from: ${{ matrix.version }}
128+
tag_latest: ${{ needs.init.outputs.alpine_latest }}
129+
python_latest: ${{ needs.init.outputs.python_latest }}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: Reusable builder
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target:
7+
description: Target name
8+
required: true
9+
type: string
10+
architectures:
11+
description: List of architectures to build
12+
required: true
13+
type: string
14+
version:
15+
description: Version to build
16+
required: true
17+
type: string
18+
python:
19+
description: Python version to build
20+
type: string
21+
release_name:
22+
description: Release name
23+
required: true
24+
type: string
25+
version_from:
26+
description: Version to build image from
27+
type: string
28+
tag_latest:
29+
description: Tag to mark docker image as latest
30+
required: true
31+
type: string
32+
python_latest:
33+
description: Python tag to mark docker image as latest
34+
type: string
35+
36+
jobs:
37+
build:
38+
name: Build ${{ matrix.arch }} base image
39+
runs-on: ${{ matrix.os }}
40+
permissions:
41+
contents: read
42+
id-token: write
43+
packages: write
44+
strategy:
45+
fail-fast: false
46+
matrix:
47+
arch: ${{ fromJson(inputs.architectures) }}
48+
include:
49+
- os: ubuntu-24.04
50+
- arch: aarch64
51+
os: ubuntu-24.04-arm
52+
steps:
53+
- name: Checkout the repository
54+
uses: actions/[email protected]
55+
56+
- name: Login to GitHub Container Registry
57+
if: github.event_name == 'release'
58+
uses: docker/login-action@v3
59+
with:
60+
registry: ghcr.io
61+
username: ${{ github.repository_owner }}
62+
password: ${{ secrets.GITHUB_TOKEN }}
63+
64+
- name: Set build arguments
65+
shell: bash
66+
run: |
67+
if [[ "${{ github.event_name }}" == "release" ]]; then
68+
BUILD_ARGS="--additional-tag ${{ inputs.version }}-${{ github.event.release.tag_name }}"
69+
if [[ "${{ inputs.tag_latest }}" != "${{ inputs.version }}" ]] \
70+
|| [[ -n "${{ inputs.python }}" && "${{ inputs.python_latest }}" != "${{ inputs.python }}" ]];
71+
then
72+
BUILD_ARGS="$BUILD_ARGS --no-latest"
73+
fi
74+
else
75+
BUILD_ARGS="--test"
76+
fi
77+
78+
if [[ -n "${{ inputs.version_from }}" ]]; then
79+
BUILD_ARGS="$BUILD_ARGS --version-from ${{ inputs.version_from }}"
80+
fi
81+
if [[ -n "${{ inputs.python }}" ]]; then
82+
BUILD_ARGS="$BUILD_ARGS --version ${{ inputs.python }} --base ${{ inputs.python }}-alpine${{ inputs.version }}"
83+
else
84+
BUILD_ARGS="$BUILD_ARGS --base ${{ inputs.version }}"
85+
fi
86+
87+
echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV
88+
89+
- name: Build base image
90+
uses: home-assistant/[email protected]
91+
with:
92+
image: ${{ matrix.arch }}
93+
args: |
94+
$BUILD_ARGS \
95+
--${{ matrix.arch }} \
96+
--target /data/${{ inputs.target }} \
97+
--cosign \
98+
--release ${{ inputs.release_name }} \

0 commit comments

Comments
 (0)