-
Notifications
You must be signed in to change notification settings - Fork 0
429 lines (407 loc) · 16.8 KB
/
docker-build-image.yml
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
---
name: "Build image"
# This reusable workflow builds Docker images. It allows images to be pushed to
# Docker Hub and the GitHub Container Registry, or uploaded as an artifact for
# later use by other workflows.
#
# Additional workflow features include:
# - Caching of Docker layers to Github Actions cache
# - Optional encryption of Docker images stored as artifacts
# - Ability to suffix tags with additional information. For example, adding the
# platform identifier so the image can be later included in a multi-platform
# manifest.
on:
workflow_call:
inputs:
# Required inputs
platforms:
description: "The platform(s) to build (CSV)"
required: true
type: string
# Optional inputs
artifact_name:
default: ""
description: "Upload the image archive as an artifact if specified (name will be cleaned)"
required: false
type: string
artifact_retention_days:
default: 1
description: "Number of days to retain the artifact"
required: false
type: number
cache_from_scopes:
default: "buildkit"
description: "The GitHub Action Cache scopes to read from (name will be cleaned, CSV)"
required: false
type: string
cache_to_scope:
default: "buildkit"
description: "The GitHub Action Cache scope to write to (name will be cleaned)"
required: false
type: string
docker_file:
default: ./Dockerfile
description: "The Dockerfile to use"
required: false
type: string
image_labels:
description: "Image labels to apply to the image"
required: false
type: string
image_tags:
default: ""
description: "The tags to apply to the image"
required: false
type: string
image_archive_name_stem:
default: image
description: "The stem of the image archive name (will be cleaned)"
required: false
type: string
push:
default: false
description: "Push the image to the registries (disables artifact upload)"
required: false
type: boolean
tag_suffix:
default: ""
description: "Suffix to append to the image tags (name will be cleaned)"
required: false
type: string
build_arg_1_name:
description: "The name of the first build arg"
required: false
type: string
build_arg_2_name:
description: "The name of the second build arg"
required: false
type: string
build_arg_3_name:
description: "The name of the third build arg"
required: false
type: string
build_arg_4_name:
description: "The name of the fourth build arg"
required: false
type: string
build_arg_5_name:
description: "The name of the fifth build arg"
required: false
type: string
build_arg_6_name:
description: "The name of the sixth build arg"
required: false
type: string
build_arg_7_name:
description: "The name of the seventh build arg"
required: false
type: string
build_arg_8_name:
description: "The name of the eighth build arg"
required: false
type: string
build_secret_1_name:
description: "The name of the first build secret"
required: false
type: string
build_secret_2_name:
description: "The name of the second build secret"
required: false
type: string
build_secret_3_name:
description: "The name of the third build secret"
required: false
type: string
build_secret_4_name:
description: "The name of the fourth build secret"
required: false
type: string
build_secret_5_name:
description: "The name of the fifth build secret"
required: false
type: string
build_secret_6_name:
description: "The name of the sixth build secret"
required: false
type: string
build_secret_7_name:
description: "The name of the seventh build secret"
required: false
type: string
build_secret_8_name:
description: "The name of the eighth build secret"
required: false
type: string
outputs:
artifact_name:
description: "The cleaned name of the artifact (if any)"
value: ${{ jobs.build-image.outputs.artifact_name }}
image_archive_name:
description: "The name of the image archive"
value: ${{ jobs.build-image.outputs.image_archive_name }}
secrets:
image_archive_key:
description: "Enables encryption of the image archive if specified"
required: false
build_arg_1_value:
description: "The value of the first build arg"
required: false
build_arg_2_value:
description: "The value of the second build arg"
required: false
build_arg_3_value:
description: "The value of the third build arg"
required: false
build_arg_4_value:
description: "The value of the fourth build arg"
required: false
build_arg_5_value:
description: "The value of the fifth build arg"
required: false
build_arg_6_value:
description: "The value of the sixth build arg"
required: false
build_arg_7_value:
description: "The value of the seventh build arg"
required: false
build_arg_8_value:
description: "The value of the eighth build arg"
required: false
build_secret_1_value:
description: "The value of the first build secret"
required: false
build_secret_2_value:
description: "The value of the second build secret"
required: false
build_secret_3_value:
description: "The value of the third build secret"
required: false
build_secret_4_value:
description: "The value of the fourth build secret"
required: false
build_secret_5_value:
description: "The value of the fifth build secret"
required: false
build_secret_6_value:
description: "The value of the sixth build secret"
required: false
build_secret_7_value:
description: "The value of the seventh build secret"
required: false
build_secret_8_value:
description: "The value of the eighth build secret"
required: false
docker_password:
description: "The password for the Docker hub account (required for push)"
required: false
docker_username:
description: "The username for the Docker hub account (required for push)"
required: false
env:
OUTPUT_ARTIFACT_WORK_DIR: /tmp/output-artifact-work
jobs:
build-image:
name: "Build image"
runs-on: ubuntu-latest
outputs:
artifact_name: ${{ steps.clean.outputs.artifact_name }}
image_archive_name: ${{ steps.check_image_archive_key.outputs.file_name }}
steps:
- name: Harden Runner
uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # tag=v2.10.2
with:
disable-sudo: true
egress-policy: audit
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag=v4.2.2
- name: Set up QEMU
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # tag=v3.2.0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 # tag=v3.8.0
- name: Docker credentials available for push
if: inputs.push == true
run: |
return_code=0
if [ -z "${{ secrets.docker_username }}" ]; then
echo "::warning::Set the docker_username secret when using push=true."
return_code=1
fi
if [ -z "${{ secrets.docker_password }}" ]; then
echo "::warning::Set the docker_password secret when using push=true."
return_code=1
fi
exit $return_code
- name: Login to Docker Hub
if: inputs.push == true
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # tag=v3.3.0
with:
username: ${{ secrets.docker_username }}
password: ${{ secrets.docker_password }}
- name: Login to GitHub Container Registry
if: inputs.push == true
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # tag=v3.3.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create directories for artifact work
run: |
mkdir -p ${{ env.OUTPUT_ARTIFACT_WORK_DIR }}
- name: Clean variables
id: clean
run: |
# Replace illegal characters in artifact name
echo artifact_name=$(echo "${{ inputs.artifact_name }}" | sed -e 's/[^a-zA-Z0-9._-]/_/g') >> $GITHUB_OUTPUT
# Replace illegal characters in cache from scope list
echo cache_from_scopes=$(echo "${{ inputs.cache_from_scopes }}" | sed -e 's/[^a-zA-Z0-9.,_-]/_/g') >> $GITHUB_OUTPUT
# Replace illegal characters in cache to scope
echo cache_to_scope=$(echo "${{ inputs.cache_to_scope }}" | sed -e 's/[^a-zA-Z0-9._-]/_/g') >> $GITHUB_OUTPUT
# Replace illegal characters in image archive name
echo image_archive_name_stem=$(echo "${{ inputs.image_archive_name_stem }}" | sed -e 's/[^a-zA-Z0-9._-]/_/g') >> $GITHUB_OUTPUT
# Replace illegal characters in tag suffix
echo suffix=$(echo "${{ inputs.tag_suffix }}" | sed -e 's/[^a-zA-Z0-9._-]/_/g') >> $GITHUB_OUTPUT
- name: Add suffix to image tags
id: tag_suffix
run: |
delimiter="$(openssl rand -hex 8)"
echo "tags<<${delimiter}" >> $GITHUB_OUTPUT
# if tag_suffix is set, append it to each tag
if [[ -n "${{ inputs.tag_suffix }}" ]]; then
echo "${{ inputs.image_tags }}" | sed -e "s/$/-${{ steps.clean.outputs.suffix }}/" >> $GITHUB_OUTPUT
else
echo "${{ inputs.image_tags }}" >> $GITHUB_OUTPUT
fi
echo "${delimiter}" >> $GITHUB_OUTPUT
- name: Prepare build args
id: prepare-build-args
env:
ARG_1_NAME: ${{ inputs.build_arg_1_name }}
ARG_1_VALUE: ${{ secrets.build_arg_1_value }}
ARG_2_NAME: ${{ inputs.build_arg_2_name }}
ARG_2_VALUE: ${{ secrets.build_arg_2_value }}
ARG_3_NAME: ${{ inputs.build_arg_3_name }}
ARG_3_VALUE: ${{ secrets.build_arg_3_value }}
ARG_4_NAME: ${{ inputs.build_arg_4_name }}
ARG_4_VALUE: ${{ secrets.build_arg_4_value }}
ARG_5_NAME: ${{ inputs.build_arg_5_name }}
ARG_5_VALUE: ${{ secrets.build_arg_5_value }}
ARG_6_NAME: ${{ inputs.build_arg_6_name }}
ARG_6_VALUE: ${{ secrets.build_arg_6_value }}
ARG_7_NAME: ${{ inputs.build_arg_7_name }}
ARG_7_VALUE: ${{ secrets.build_arg_7_value }}
ARG_8_NAME: ${{ inputs.build_arg_8_name }}
ARG_8_VALUE: ${{ secrets.build_arg_8_value }}
run: |
delimiter="$(openssl rand -hex 8)"
echo "build_args<<${delimiter}" >> $GITHUB_OUTPUT
for i in {1..8}; do
arg_name_var="ARG_${i}_NAME"
arg_value_var="ARG_${i}_VALUE"
arg_name=${!arg_name_var}
arg_value=${!arg_value_var}
if [[ -n "$arg_name" && -n "$arg_value" ]]; then
echo "${arg_name}=${arg_value}" >> $GITHUB_OUTPUT
fi
done
echo "${delimiter}" >> $GITHUB_OUTPUT
- name: Prepare build secrets
id: prepare-build-secrets
env:
SECRET_1_NAME: ${{ inputs.build_secret_1_name }}
SECRET_1_VALUE: ${{ secrets.build_secret_1_value }}
SECRET_2_NAME: ${{ inputs.build_secret_2_name }}
SECRET_2_VALUE: ${{ secrets.build_secret_2_value }}
SECRET_3_NAME: ${{ inputs.build_secret_3_name }}
SECRET_3_VALUE: ${{ secrets.build_secret_3_value }}
SECRET_4_NAME: ${{ inputs.build_secret_4_name }}
SECRET_4_VALUE: ${{ secrets.build_secret_4_value }}
SECRET_5_NAME: ${{ inputs.build_secret_5_name }}
SECRET_5_VALUE: ${{ secrets.build_secret_5_value }}
SECRET_6_NAME: ${{ inputs.build_secret_6_name }}
SECRET_6_VALUE: ${{ secrets.build_secret_6_value }}
SECRET_7_NAME: ${{ inputs.build_secret_7_name }}
SECRET_7_VALUE: ${{ secrets.build_secret_7_value }}
SECRET_8_NAME: ${{ inputs.build_secret_8_name }}
SECRET_8_VALUE: ${{ secrets.build_secret_8_value }}
run: |
delimiter="$(openssl rand -hex 8)"
echo "build_secrets<<${delimiter}" >> $GITHUB_OUTPUT
for i in {1..3}; do
secret_name_var="SECRET_${i}_NAME"
secret_value_var="SECRET_${i}_VALUE"
secret_name=${!secret_name_var}
secret_value=${!secret_value_var}
if [[ -n "$secret_name" && -n "$secret_value" ]]; then
echo "\"${secret_name}=${secret_value}\"" >> $GITHUB_OUTPUT
fi
done
echo "${delimiter}" >> $GITHUB_OUTPUT
- name: Calculate GitHub Actions cache scopes
id: cache_scopes
run: |
# Create a list of cache object lines from the cleaned cache scope CSV
delimiter="$(openssl rand -hex 8)"
echo "from<<${delimiter}" >> $GITHUB_OUTPUT
for scope in $(echo ${{ steps.clean.outputs.cache_from_scopes }} | tr "," " "); do
echo "type=gha,scope=${scope}" >> $GITHUB_OUTPUT
done
echo "${delimiter}" >> $GITHUB_OUTPUT
# Create a cache object line if cache_to_scope is set
if [[ -n "${{ steps.clean.outputs.cache_to_scope }}" ]]; then
echo "to=type=gha,mode=max,scope=${{ steps.clean.outputs.cache_to_scope }}" >> $GITHUB_OUTPUT
else
echo "to=" >> $GITHUB_OUTPUT
fi
- name: Build image for push
if: inputs.push == true
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # tag=v6.10.0
with:
build-args: ${{ steps.prepare-build-args.outputs.build_args }}
cache-from: ${{ steps.cache_scopes.outputs.from }}
cache-to: ${{ steps.cache_scopes.outputs.to }}
context: .
file: ${{ inputs.docker_file}}
labels: ${{ inputs.image_labels }}
platforms: ${{ inputs.platforms }}
push: true
secrets: ${{ steps.prepare-build-secrets.outputs.build_secrets }}
tags: ${{ steps.tag_suffix.outputs.tags }}
- name: Build image for archive
if: inputs.push == false
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # tag=v6.10.0
with:
build-args: ${{ steps.prepare-build-args.outputs.build_args }}
cache-from: ${{ steps.cache_scopes.outputs.from }}
cache-to: ${{ steps.cache_scopes.outputs.to }}
context: .
file: ${{ inputs.docker_file}}
labels: ${{ inputs.image_labels }}
outputs: type=docker,dest=${{ env.OUTPUT_ARTIFACT_WORK_DIR }}/${{ steps.clean.outputs.image_archive_name_stem }}.tar
platforms: ${{ inputs.platforms }}
push: false
secrets: ${{ steps.prepare-build-secrets.outputs.build_secrets }}
tags: ${{ steps.tag_suffix.outputs.tags }}
- name: Check if archive key is available
id: check_image_archive_key
if: inputs.push == false
run: |
if [ -n "${{ secrets.image_archive_key }}" ]; then
echo "do_encryption=true" >> $GITHUB_OUTPUT
echo "file_name=${{ steps.clean.outputs.image_archive_name_stem }}.tar.7z" >> $GITHUB_OUTPUT
else
echo "do_encryption=false" >> $GITHUB_OUTPUT
echo "file_name=${{ steps.clean.outputs.image_archive_name_stem }}".tar >> $GITHUB_OUTPUT
fi
- name: Encrypt image archive
if: ${{ (inputs.push == false) && (steps.check_image_archive_key.outputs.do_encryption == 'true') }}
run: |
7za a -p${{ secrets.image_archive_key }} \
${{ env.OUTPUT_ARTIFACT_WORK_DIR }}/${{ steps.clean.outputs.image_archive_name_stem }}.tar.7z \
${{ env.OUTPUT_ARTIFACT_WORK_DIR }}/${{ steps.clean.outputs.image_archive_name_stem }}.tar
- name: Upload artifacts
if: ${{ (inputs.push == false) && (steps.clean.outputs.artifact_name != '') }}
uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # tag=v4.5.0
with:
name: ${{ steps.clean.outputs.artifact_name }}
path: ${{ env.OUTPUT_ARTIFACT_WORK_DIR }}/${{ steps.check_image_archive_key.outputs.file_name }}
retention-days: ${{ inputs.artifact_retention_days }}