-
Notifications
You must be signed in to change notification settings - Fork 1
351 lines (319 loc) · 12.6 KB
/
build-images.yaml
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
---
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: "Image Build"
on:
workflow_call:
inputs:
appsToBuild:
required: false
type: string
default: ""
channelsToBuild:
required: false
type: string
default: ""
pushImages:
required: false
default: false
type: boolean
sendNotifications:
required: false
default: false
type: boolean
force:
required: false
default: true
type: boolean
description: Force rebuild
secrets:
BOT_APP_ID:
description: The App ID of the GitHub App
required: true
BOT_APP_PRIVATE_KEY:
description: The private key of the GitHub App
required: true
jobs:
prepare:
name: Prepare to Build
runs-on: ubuntu-latest
outputs:
matrices: ${{ steps.prepare-matrices.outputs.matrices }}
steps:
- name: Lowercase repository owner
shell: bash
run: echo "LOWERCASE_REPO_OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_ENV
- name: Generate Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: "${{ secrets.BOT_APP_ID }}"
private-key: "${{ secrets.BOT_APP_PRIVATE_KEY }}"
- name: Checkout
uses: actions/checkout@v4
with:
token: "${{ steps.app-token.outputs.token }}"
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.x
cache: pip
- name: Install Python Requirements
shell: bash
run: pip install --requirement requirements.txt && pip freeze
- name: Prepare Matrices
id: prepare-matrices
env:
TOKEN: ${{ steps.app-token.outputs.token }}
shell: bash
run: |
if [[ -z "${{ inputs.appsToBuild }}" ]]; then
matrices=$(python ./scripts/prepare-matrices.py "all" "${{ inputs.pushImages }}" "${{ inputs.force }}")
else
if [[ -z "${{ inputs.channelsToBuild }}" ]]; then
matrices=$(python ./scripts/prepare-matrices.py "${{ inputs.appsToBuild }}" "${{ inputs.pushImages }}" "${{ inputs.force }}")
else
matrices=$(python ./scripts/prepare-matrices.py "${{ inputs.appsToBuild }}" "${{ inputs.pushImages }}" "${{ inputs.force }}" "${{ inputs.channelsToBuild }}")
fi
fi
echo "matrices=${matrices}" >> $GITHUB_OUTPUT
echo "${matrices}"
build-platform-images:
name: Build/Test ${{ matrix.image.name }} (${{ matrix.image.platform }})
needs: prepare
runs-on: ubuntu-latest
if: ${{ toJSON(fromJSON(needs.prepare.outputs.matrices).imagePlatforms) != '[]' && toJSON(fromJSON(needs.prepare.outputs.matrices).imagePlatforms) != '' }}
strategy:
fail-fast: false
matrix:
image:
["${{ fromJSON(needs.prepare.outputs.matrices).imagePlatforms }}"]
permissions:
contents: read
packages: write
steps:
- name: Lowercase repository owner
shell: bash
run: echo "LOWERCASE_REPO_OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_ENV
- name: Log Matrix Input
shell: bash
run: |
cat << EOF
${{ toJSON(matrix.image)}}
EOF
- name: Validate Matrix Input
shell: bash
run: |
if [[ -z "${{ matrix.image.name }}" ]]; then
echo "image.name is empty"
exit 1
fi
if [[ -z "${{ matrix.image.version }}" ]]; then
echo "image.version is empty"
exit 1
fi
if [[ -z "${{ matrix.image.context }}" ]]; then
echo "image.context is empty"
exit 1
fi
if [[ -z "${{ matrix.image.dockerfile }}" ]]; then
echo "image.dockerfile is empty"
exit 1
fi
if [[ -z "${{ matrix.image.platform }}" ]]; then
echo "image.platform is empty"
exit 1
fi
if [[ -z "${{ matrix.image.tests_enabled }}" ]]; then
echo "image.tests_enabled is empty"
exit 1
fi
echo "${{ matrix.image.name }}" | grep -E "[a-zA-Z0-9_\.\-]+" || "Image Name is invalid"
echo "${{ matrix.image.version }}" | grep -E "[a-zA-Z0-9_\.\-]+" || "Image Version is invalid"
- name: Generate Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: "${{ secrets.BOT_APP_ID }}"
private-key: "${{ secrets.BOT_APP_PRIVATE_KEY }}"
- name: Checkout
uses: actions/checkout@v4
with:
token: "${{ steps.app-token.outputs.token }}"
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: "${{ github.actor }}"
password: "${{ secrets.GITHUB_TOKEN }}"
- name: Setup Goss
if: ${{ matrix.image.tests_enabled }}
uses: e1himself/goss-installation-action@v1
with:
version: latest
- name: Prepare Build Outputs
id: prepare-build-outputs
shell: bash
run: |
if [[ "${{ inputs.pushImages }}" == "true" ]]; then
image_name="ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}"
outputs="type=image,name=${image_name},push-by-digest=true,name-canonical=true,push=true"
else
image_name="ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}:zztesting"
outputs="type=docker,name=${image_name},push=false"
fi
echo "image_name=${image_name}" >> $GITHUB_OUTPUT
echo "outputs=${outputs}" >> $GITHUB_OUTPUT
- name: Build Image
uses: docker/build-push-action@v6
id: build
with:
build-args: |-
VERSION=${{ matrix.image.version }}
REVISION=${{ github.sha }}
CHANNEL=${{ matrix.image.channel }}
# TODO: Use ${{ matrix.image.context }}, requires updates to all dockerfiles :-(
context: .
platforms: ${{ matrix.image.platform }}
file: ${{ matrix.image.dockerfile }}
outputs: ${{ steps.prepare-build-outputs.outputs.outputs }}
cache-from: type=gha
cache-to: type=gha,mode=max
labels: |-
org.opencontainers.image.title=${{ matrix.image.name }}
org.opencontainers.image.url=https://ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}
org.opencontainers.image.version=${{ matrix.image.version }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.vendor=${{ env.LOWERCASE_REPO_OWNER }}
- name: Run Goss Tests
id: dgoss
if: ${{ matrix.image.tests_enabled }}
env:
CONTAINER_RUNTIME: docker
GOSS_FILE: ${{ matrix.image.goss_config }}
GOSS_OPTS: --retry-timeout 60s --sleep 2s --color --format documentation
GOSS_SLEEP: 2
GOSS_FILES_STRATEGY: cp
CONTAINER_LOG_OUTPUT: goss_container_log_output
shell: bash
run: |
if [[ '${{ inputs.pushImages }}' == 'true' ]]; then
image_name="${{ steps.prepare-build-outputs.outputs.image_name }}@${{ steps.build.outputs.digest }}"
else
image_name="${{ steps.prepare-build-outputs.outputs.image_name }}"
fi
dgoss run ${image_name} ${{ matrix.image.goss_args }}
- name: Export Digest
id: export-digest
if: ${{ inputs.pushImages }}
shell: bash
run: |
mkdir -p /tmp/${{ matrix.image.name }}/digests
digest="${{ steps.build.outputs.digest }}"
echo "${{ matrix.image.name }}" > "/tmp/${{ matrix.image.name }}/digests/${digest#sha256:}"
- name: Upload Digest
if: ${{ inputs.pushImages}}
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.image.name }}-${{ matrix.image.target_os }}-${{ matrix.image.target_arch }}
path: /tmp/${{ matrix.image.name }}/*
if-no-files-found: error
retention-days: 1
merge:
name: Merge ${{ matrix.image.name }}
runs-on: ubuntu-latest
needs: ["prepare", "build-platform-images"]
# Always run merge, as the prior matrix is all or nothing. We test for prior step failure
# in the "Test Failed Bit" step. This ensures if one app fails, others can still complete.
if: ${{ always() && inputs.pushImages && toJSON(fromJSON(needs.prepare.outputs.matrices).images) != '[]' && toJSON(fromJSON(needs.prepare.outputs.matrices).images) != '' }}
strategy:
matrix:
image: ["${{ fromJSON(needs.prepare.outputs.matrices).images }}"]
fail-fast: false
steps:
- name: Lowercase repository owner
shell: bash
run: echo "LOWERCASE_REPO_OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_ENV
- name: Download Digests
uses: actions/download-artifact@v4
with:
pattern: "${{ matrix.image.name }}-{linux,darwin}-{amd64,arm64}"
merge-multiple: true
path: /tmp/${{ matrix.image.name }}
- name: Ensure all platforms were built
id: ensure-platforms
shell: bash
run: |
EXPECTED_COUNT=$(cat << EOF | jq ". | length"
${{ toJSON(matrix.image.platforms) }}
EOF
)
ACTUAL_COUNT=$(ls -1 /tmp/${{ matrix.image.name }}/digests | wc -l)
if [[ $EXPECTED_COUNT != $ACTUAL_COUNT ]]; then
echo "Expected $EXPECTED_COUNT platforms, but only found $ACTUAL_COUNT"
echo "Expected: ${{ toJSON(matrix.image.platforms) }}"
echo "Actual: $(cat /tmp/${{ matrix.image.name }}/digests/*)"
exit 1
fi
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: "${{ github.actor }}"
password: "${{ secrets.GITHUB_TOKEN }}"
- name: Log Files
working-directory: /tmp/${{ matrix.image.name }}/digests
shell: bash
run: |
ls -la
cat *
- name: Merge Manifests
id: merge
working-directory: /tmp/${{ matrix.image.name }}/digests
env:
TAGS: ${{ toJSON(matrix.image.tags) }}
shell: bash
run: |
docker buildx imagetools create $(jq -cr '. | map("-t ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{matrix.image.name}}:" + .) | join(" ")' <<< "$TAGS") \
$(printf 'ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}@sha256:%s ' *)
- name: Inspect image
id: inspect
shell: bash
run: |
docker buildx imagetools inspect ghcr.io/${{ env.LOWERCASE_REPO_OWNER }}/${{ matrix.image.name }}:${{ matrix.image.tags[0] }}
- name: Build successful
id: build-success
if: ${{ always() && steps.merge.outcome == 'success' && steps.inspect.outcome == 'success' }}
shell: bash
run: |
echo "message=🎉 ${{ matrix.image.name }} (${{ matrix.image.tags[0] }})" >> $GITHUB_OUTPUT
echo "color=0x00FF00" >> $GITHUB_OUTPUT
- name: Build failed
id: build-failed
if: ${{ always() && (steps.merge.outcome == 'failure' || steps.inspect.outcome == 'failure') }}
shell: bash
run: |
echo "message=💥 ${{ matrix.image.name }} (${{ matrix.image.tags[0] }})" >> $GITHUB_OUTPUT
echo "color=0xFF0000" >> $GITHUB_OUTPUT
- name: Send Discord Webhook
uses: sarisia/actions-status-discord@v1
if: ${{ always() && inputs.sendNotifications == 'true' }}
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
title: ${{ steps.build-failed.outputs.message || steps.build-success.outputs.message }}
color: ${{ steps.build-failed.outputs.color || steps.build-success.outputs.color }}
username: GitHub Actions
# Summarize matrix https://github.community/t/status-check-for-a-matrix-jobs/127354/7
build_success:
name: Build matrix success
runs-on: ubuntu-latest
needs: ["prepare", "merge"]
if: ${{ always() }}
steps:
- name: Check build matrix status
if: ${{ (inputs.appsToBuild != '' && inputs.appsToBuild != '[]') && (needs.merge.result != 'success' && needs.merge.result != 'skipped' && needs.prepare.result != 'success') }}
shell: bash
run: exit 1