-
Notifications
You must be signed in to change notification settings - Fork 87
executable file
·281 lines (250 loc) · 9.95 KB
/
action-image-build.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
---
name: "Action: Image Build"
on:
workflow_call:
inputs:
imagesToBuild:
description: |-
Pass a json string with the images and channels to build. e.g.:
[
{
"app": "myapp",
"channel": "main"
},
{
"app": "myapp",
"channel": "develop"
}
]
required: false
type: string
default: ''
pushImages:
required: false
default: 'false'
type: string
sendNotification:
required: false
default: 'false'
type: string
jobs:
prepare:
name: Prepare Matrices
runs-on: ubuntu-latest
outputs:
matrices: ${{ steps.generate-matrices.outputs.matrices }}
if: ${{ inputs.imagesToBuild != '' && inputs.imagesToBuild != '[]' }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 1
- name: Log images to build
run: |
echo 'imagesToBuild=${{ inputs.imagesToBuild }}'
echo 'pushImages=${{ inputs.pushImages }}'
echo 'sendNotification=${{ inputs.sendNotification }}'
- name: Setup CUE
uses: cue-lang/setup-cue@a93fa358375740cd8b0078f76355512b9208acb1
- name: Validate Image Metadata
shell: bash
# 'xargs' rather than 'find -exec' so we can capture the status code
run: |
find ./ -name metadata.json | xargs -I {} cue vet --schema '#Spec' {} ./metadata.rules.cue
- name: Generate Matrices
id: generate-matrices
run: |
matrices=$(./.github/scripts/prepare-images.py '${{ inputs.imagesToBuild }}' '${{ inputs.pushImages }}')
echo "matrices=${matrices}" >> $GITHUB_OUTPUT
build-platform-images:
name: Build/Test ${{ matrix.image.chan_image_name }} - ${{ matrix.image.platform }}
runs-on: ubuntu-latest
needs:
- prepare
strategy:
matrix:
image: ["${{ fromJson(needs.prepare.outputs.matrices).imagePlatformPermutations }}"]
fail-fast: false
steps:
- name: Log Matrix Input
run: |
cat << EOF
${{ toJSON(matrix.image) }}
EOF
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
fetch-depth: 1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate Token
uses: actions/create-github-app-token@v1.11.0
id: generate-token
with:
app-id: "${{ secrets.APP_ID }}"
private-key: "${{ secrets.APP_PRIVATE_KEY }}"
- name: Setup GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ steps.generate-token.outputs.token }}
- name: Setup Tools
shell: bash
run: sudo apt-get install -y moreutils jo
- name: Setup Goss
if: ${{ matrix.image.chan_tests_enabled }}
uses: e1himself/goss-installation-action@v1.2.1
with:
version: v0.3.21
- name: Prepare Build Outputs
id: prepare-build-outputs
run: |
if [[ ${{ inputs.pushImages }} == 'true' ]]; then
image_name="ghcr.io/${{ github.repository_owner }}/${{ matrix.image.chan_image_name }}"
outputs="type=image,name=${image_name},push-by-digest=true,name-canonical=true,push=true"
else
image_name="ghcr.io/${{ github.repository_owner }}/${{ matrix.image.chan_tag_testing }}"
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.chan_upstream_version }}
CHANNEL=${{ matrix.image.channel }}
context: .
platforms: ${{ matrix.image.platform }}
file: ${{ matrix.image.chan_dockerfile }}
outputs: ${{ steps.prepare-build-outputs.outputs.outputs }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run Goss Tests
id: dgoss
if: ${{ matrix.image.chan_tests_enabled }}
shell: bash
env:
CONTAINER_RUNTIME: docker
GOSS_FILE: ${{ matrix.image.chan_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
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.chan_goss_args }}
- name: Export Digest
id: export-digest
if: ${{ inputs.pushImages == 'true' }}
run: |
mkdir -p /tmp/${{ matrix.image.chan_image_name }}/digests
digest="${{ steps.build.outputs.digest }}"
echo "${{ matrix.image.platform }}" > "/tmp/${{ matrix.image.chan_image_name }}/digests/${digest#sha256:}"
- name: Upload Digest
if: ${{ inputs.pushImages == 'true' }}
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.image.chan_image_name }}
path: /tmp/${{ matrix.image.chan_image_name }}/*
if-no-files-found: error
retention-days: 1
merge:
name: Merge ${{ matrix.manifest.image }}
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 == 'true'}}
strategy:
matrix:
manifest: ["${{ fromJSON(needs.prepare.outputs.matrices).manifestsToBuild }}"]
fail-fast: false
steps:
- name: Download Digests
uses: actions/download-artifact@v4
with:
name: ${{ matrix.manifest.image }}
path: /tmp/${{ matrix.manifest.image }}
- name: Ensure all platforms were built
id: ensure-platforms
run: |
EXPECTED_COUNT=$(cat << EOF | jq ". | length"
${{ toJSON(matrix.manifest.platforms) }}
EOF
)
ACTUAL_COUNT=$(ls -1 /tmp/${{ matrix.manifest.image }}/digests | wc -l)
if [[ $EXPECTED_COUNT != $ACTUAL_COUNT ]]; then
echo "Expected $EXPECTED_COUNT platforms, but only found $ACTUAL_COUNT"
echo "Expected: ${{ toJSON(matrix.manifest.platforms) }}"
echo "Actual: $(cat /tmp/${{ matrix.manifest.image }}/digests/*)"
exit 1
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Generate Token
uses: actions/create-github-app-token@v1.11.0
id: generate-token
with:
app-id: "${{ secrets.APP_ID }}"
private-key: "${{ secrets.APP_PRIVATE_KEY }}"
- name: Setup GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ steps.generate-token.outputs.token }}
- name: Log Files
working-directory: /tmp/${{ matrix.manifest.image }}/digests
run: |
ls -la
cat *
# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners
- name: Merge Manifests
id: merge
working-directory: /tmp/${{ matrix.manifest.image }}/digests
env:
TAGS: ${{ toJSON(matrix.manifest.tags) }}
run: |
docker buildx imagetools create $(jq -cr '. | map("-t ghcr.io/${{ github.repository_owner }}/" + .) | join(" ")' <<< "$TAGS") \
$(printf 'ghcr.io/${{ github.repository_owner }}/${{ matrix.manifest.image }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ghcr.io/${{ github.repository_owner }}/${{ matrix.manifest.image }}:${{ matrix.manifest.version }}
- name: Build successful
id: build-success
if: ${{ always() && steps.merge.outcome == 'success' && steps.failure-test.outcome == 'success' }}
run: |-
echo "message=🎉 ${{ matrix.manifest.app }}-${{ matrix.manifest.channel }} (${{ matrix.manifest.version }})" >> $GITHUB_OUTPUT
echo "color=0x00FF00" >> $GITHUB_OUTPUT
- name: Build failed
id: build-failed
if: ${{ always() && (steps.merge.outcome == 'failure' || steps.failure-test.outcome == 'failure') }}
run: |-
echo "message=💥 ${{ matrix.manifest.app }}-${{ matrix.manifest.channel }} (${{ matrix.manifest.version }})" >> $GITHUB_OUTPUT
echo "color=0xFF0000" >> $GITHUB_OUTPUT
# - name: Send Discord Webhook
# uses: sarisia/actions-status-discord@v1
# if: ${{ always() && inputs.sendNotification == '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:
- merge
if: ${{ always() }}
steps:
- name: Check build matrix status
if: ${{ (inputs.imagesToBuild != '' && inputs.imagesToBuild != '[]') && (needs.merge.result != 'success' && needs.merge.result != 'skipped') }}
run: exit 1