-
Notifications
You must be signed in to change notification settings - Fork 19
261 lines (232 loc) · 9.11 KB
/
docker-build.yml
File metadata and controls
261 lines (232 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
name: DEV - build & publish branch containers
on:
workflow_dispatch:
inputs:
branch:
description: "Branch to build (e.g. my-feature-branch)"
type: string
required: true
containers:
description: "Comma-separated container names to build, or 'all' for all containers"
type: string
default: "all"
tag:
description: "Custom image tag (optional, defaults to sanitized branch name)"
type: string
default: ""
env:
REGISTRY: ghcr.io
DEV_REGISTRY_PATH: ghcr.io/${{ github.repository_owner }}/
permissions:
contents: read
packages: write
id-token: write
jobs:
detect-changes:
runs-on: ubuntu-latest
if: inputs.branch != 'main'
outputs:
containers: ${{ steps.detect.outputs.containers }}
downstream_containers: ${{ steps.detect.outputs.downstream_containers }}
image_tag: ${{ steps.detect.outputs.image_tag }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
ref: ${{ inputs.branch }}
- name: Detect containers to build
id: detect
run: |
# Compute image tag from branch name unless an explicit tag was provided
if [[ -n "${{ inputs.tag }}" ]]; then
IMAGE_TAG="${{ inputs.tag }}"
else
IMAGE_TAG="dev-$(echo "${{ inputs.branch }}" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9._-]/-/g')"
fi
echo "image_tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
INPUT="${{ inputs.containers }}"
CHANGED=()
if [[ "$INPUT" == "all" ]]; then
for CI_JSON in containers/*/ci.json; do
CHANGED+=("$(basename "$(dirname "$CI_JSON")")")
done
else
while IFS= read -r CONTAINER; do
CHANGED+=("$CONTAINER")
done < <(echo "$INPUT" | tr ',' '\n' | sed 's/^ *//;s/ *$//')
fi
# Forward cascade: if a base container is selected, add its downstream containers
for CONTAINER in "${CHANGED[@]}"; do
DOWNSTREAM=$(jq -r '.downstream // [] | .[]' "containers/$CONTAINER/ci.json")
for DS in $DOWNSTREAM; do
if [[ ! " ${CHANGED[*]} " =~ " $DS " ]]; then
echo "Cascading downstream: $DS depends on $CONTAINER"
CHANGED+=("$DS")
fi
done
done
# Reverse cascade: if a downstream container is selected, add its parent so the
# base image is always built fresh before the downstream container
for CONTAINER in "${CHANGED[@]}"; do
for CI_JSON in containers/*/ci.json; do
PARENT=$(basename "$(dirname "$CI_JSON")")
DS_LIST=$(jq -r '.downstream // [] | .[]' "$CI_JSON" 2>/dev/null)
for DS in $DS_LIST; do
if [[ "$DS" == "$CONTAINER" ]] && [[ ! " ${CHANGED[*]} " =~ " $PARENT " ]]; then
echo "Cascading upstream: $PARENT required by $CONTAINER"
CHANGED+=("$PARENT")
fi
done
done
done
if [[ ${#CHANGED[@]} -eq 0 ]]; then
echo "No containers to build"
echo "containers=[]" >> $GITHUB_OUTPUT
echo "downstream_containers=[]" >> $GITHUB_OUTPUT
exit 0
fi
# Split into base and downstream containers.
# A container is downstream if it appears in another container's downstream list.
DOWNSTREAM_IN_BUILD=()
for CONTAINER in "${CHANGED[@]}"; do
DS_LIST=$(jq -r '.downstream // [] | .[]' "containers/$CONTAINER/ci.json" 2>/dev/null || true)
for DS in $DS_LIST; do
if [[ " ${CHANGED[*]} " =~ " $DS " ]] && [[ ! " ${DOWNSTREAM_IN_BUILD[*]} " =~ " $DS " ]]; then
DOWNSTREAM_IN_BUILD+=("$DS")
fi
done
done
BASE_CONTAINERS=()
for CONTAINER in "${CHANGED[@]}"; do
if [[ ! " ${DOWNSTREAM_IN_BUILD[*]} " =~ " $CONTAINER " ]]; then
BASE_CONTAINERS+=("$CONTAINER")
fi
done
echo "Base containers: ${BASE_CONTAINERS[*]}"
echo "Downstream containers: ${DOWNSTREAM_IN_BUILD[*]}"
echo "containers=$(printf '%s\n' "${BASE_CONTAINERS[@]}" | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
if [[ ${#DOWNSTREAM_IN_BUILD[@]} -eq 0 ]]; then
echo "downstream_containers=[]" >> $GITHUB_OUTPUT
else
echo "downstream_containers=$(printf '%s\n' "${DOWNSTREAM_IN_BUILD[@]}" | jq -R . | jq -sc .)" >> $GITHUB_OUTPUT
fi
build:
needs: [ detect-changes ]
if: needs.detect-changes.outputs.containers != '[]'
runs-on: ubuntu-24.04-arm
env:
PYTHON_VERSION: "3.13"
TASKFILE_VERSION: "3.45.4"
strategy:
fail-fast: false
matrix:
container: ${{ fromJson(needs.detect-changes.outputs.containers) }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
ref: ${{ inputs.branch }}
- name: Set up Python
if: matrix.container == 'python-apps-base'
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install and build library
if: matrix.container == 'python-apps-base'
env:
PUBLIC_IMAGE_REGISTRY_BASE: ${{ env.DEV_REGISTRY_PATH }}
run: |
pip install go-task-bin==${{ env.TASKFILE_VERSION }}
task init:ci
task build-dev
cp ./dist/arduino*.whl ./containers/python-apps-base/
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v4
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
tags: |
type=raw,value=${{ needs.detect-changes.outputs.image_tag }}
type=raw,value=${{ needs.detect-changes.outputs.image_tag }}-${{ github.run_number }}
images: ghcr.io/${{ github.repository_owner }}/app-bricks/${{ matrix.container }}
- name: Build and push Docker image
uses: docker/build-push-action@v7
with:
context: ./containers/${{ matrix.container }}
file: ./containers/${{ matrix.container }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
build-args: |
REGISTRY=${{ env.DEV_REGISTRY_PATH }}
build-downstream:
needs: [ detect-changes, build ]
if: needs.detect-changes.outputs.downstream_containers != '[]'
runs-on: ubuntu-24.04-arm
env:
PYTHON_VERSION: "3.13"
TASKFILE_VERSION: "3.45.4"
strategy:
fail-fast: false
matrix:
container: ${{ fromJson(needs.detect-changes.outputs.downstream_containers) }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
ref: ${{ inputs.branch }}
- name: Set up Python
if: matrix.container == 'python-apps-base'
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install and build library
if: matrix.container == 'python-apps-base'
env:
PUBLIC_IMAGE_REGISTRY_BASE: ${{ env.DEV_REGISTRY_PATH }}
DEV_TAG_VERSION: ${{ needs.detect-changes.outputs.image_tag }}
run: |
pip install go-task-bin==${{ env.TASKFILE_VERSION }}
task init:ci
task build-dev
cp ./dist/arduino*.whl ./containers/python-apps-base/
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v4
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
tags: |
type=raw,value=${{ needs.detect-changes.outputs.image_tag }}
type=raw,value=${{ needs.detect-changes.outputs.image_tag }}-${{ github.run_number }}
images: ghcr.io/${{ github.repository_owner }}/app-bricks/${{ matrix.container }}
- name: Build and push Docker image
uses: docker/build-push-action@v7
with:
context: ./containers/${{ matrix.container }}
file: ./containers/${{ matrix.container }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false
build-args: |
REGISTRY=${{ env.DEV_REGISTRY_PATH }}
BASE_IMAGE_VERSION=${{ needs.detect-changes.outputs.image_tag }}