-
Notifications
You must be signed in to change notification settings - Fork 1
544 lines (489 loc) · 21.4 KB
/
Copy pathdocker-build-images.yml
File metadata and controls
544 lines (489 loc) · 21.4 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# Workflow to build multiple Docker images.
# Build images using [Docker build image](https://github.com/hoverkraft-tech/ci-github-container/blob/main/actions/docker/build-image/README.md).
# This includes [multi-platform](https://docs.docker.com/build/building/multi-platform/) build.
---
name: Docker build images
on: # yamllint disable-line rule:truthy
workflow_call:
inputs:
runs-on:
description: |
Runner to use. JSON array of runners.
See https://docs.github.com/en/actions/using-jobs/choosing-the-runner-for-a-job.
type: string
default: '["ubuntu-latest"]'
required: false
oci-registry:
description: |
OCI registry configuration used to pull, push and cache images.
Accepts either a registry hostname string (default format) or a JSON object.
JSON example: `{"pull":"docker.io","pull:private":"ghcr.io","push":"ghcr.io"}`
JSON object keys:
- `pull`: registry used to pull public or default base images
- `pull:<name>`: additional pull registry
- `push`: registry used for published images
- `cache`: registry used when `cache-type` is `registry`
If no `pull` key is provided, the `push` registry is also used for pulls.
type: string
default: "ghcr.io"
required: false
oci-registry-username:
description: |
Username configuration used to log against OCI registries.
Accepts either a single username string (default format) or a JSON object using the same keys as `oci-registry`.
JSON example: `{"pull:private":"$\{{ github.repository_owner }}","push":"$\{{ github.repository_owner }}"}`
See https://github.com/docker/login-action#usage.
type: string
default: ${{ github.repository_owner }}
required: false
images:
description: |
Images to build parameters.
JSON array of objects.
Example:
```json
[
{
"name": "application",
"context": ".",
"dockerfile": "./docker/application/Dockerfile",
"target": "prod",
"build-args": {
"APP_PATH": "./application/",
"PROD_MODE": "true"
},
"secret-envs": {
"GH_TOKEN": "GITHUB_TOKEN"
},
"platforms": [
"linux/amd64",
{
"name": "darwin/amd64",
"runs-on": "macos-latest"
}
]
}
]
```
type: string
required: true
lfs:
description: |
Enable Git LFS.
See https://github.com/actions/checkout?tab=readme-ov-file#usage.
type: boolean
default: true
required: false
build-secret-github-app-token-env:
description: |
Environment variable name(s) to pass GitHub token generated by GitHub App.
Can be a multiline string list.
This is useful to pass a generated token to the build, as it is not possible to share generated secrets between jobs.
Needs input `build-secret-github-app-client-id` and secret `build-secret-github-app-key`.
type: string
required: false
default: "GITHUB_APP_TOKEN"
build-secret-github-app-client-id:
description: |
GitHub App client ID to generate GitHub token to be passed as build secret env.
See https://github.com/actions/create-github-app-token.
required: false
type: string
build-secret-github-app-owner:
description: |
The owner of the GitHub App installation.
See https://github.com/actions/create-github-app-token.
required: false
type: string
default: ${{ github.repository_owner }}
cache-type:
description: |
Cache type.
See https://docs.docker.com/build/cache/backends.
default: "gha"
type: string
required: false
buildkitd-config-inline:
description: |
Inline BuildKit daemon configuration.
See https://github.com/docker/setup-buildx-action#inputs.
Example for insecure registry:
```ini
[registry."my-registry.local:5000"]
http = true
insecure = true
```
type: string
required: false
sign:
description: |
Sign built images.
See [sign-images](../../actions/docker/sign-images/README.md).
type: boolean
default: true
required: false
secrets:
oci-registry-password:
description: |
Password or GitHub token (`packages:read` and `packages:write` scopes) configuration used to log against OCI registries.
Accepts either a single password/token string (default format) or a JSON object using the same keys as `oci-registry`.
JSON example: `{"pull:private":"$\{{ github.token }}","push":"$\{{ github.token }}"}`
See https://github.com/docker/login-action#usage.
required: true
build-secrets:
description: |
List of secrets to expose to the build.
See https://docs.docker.com/build/ci/github-actions/secrets/.
required: false
build-secret-github-app-key:
description: |
GitHub App private key to generate GitHub token to be passed as build secret env.
See https://github.com/actions/create-github-app-token.
required: false
outputs:
built-images:
description: |
Built images data.
Example:
```json
{
"application": {
"name": "application",
"registry": "ghcr.io",
"repository": "my-org/my-repo/application",
"tags": ["pr-63-5222075","pr-63"],
"images": [
"ghcr.io/my-org/my-repo/application:pr-63-5222075@sha256:d31aa93410434ac9dcfc9179cac2cb1fd4d7c27f11527addc40299c7c675f49d",
"ghcr.io/my-org/my-repo/application:pr-63@sha256:d31aa93410434ac9dcfc9179cac2cb1fd4d7c27f11527addc40299c7c675f49d"
],
"digest": "sha256:d31aa93410434ac9dcfc9179cac2cb1fd4d7c27f11527addc40299c7c675f49d",
"annotations": {
"org.opencontainers.image.created": "2021-09-30T14:00:00Z",
"org.opencontainers.image.description": "Application image"
},
"platforms": ["linux/amd64", "linux/arm64"]
}
}
```
value: ${{ jobs.publish-manifests.outputs.built-images }}
permissions: {}
jobs:
prepare-variables:
outputs:
artifact-name: ${{ steps.define-artifact-name.outputs.artifact-name }}
images: ${{ steps.define-images-by-platform.outputs.images }}
runs-on: ${{ fromJson(inputs.runs-on) }}
steps:
- id: validate-inputs
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
OCI_REGISTRY_PASSWORD: ${{ secrets.oci-registry-password }} # zizmor: ignore[secrets-outside-env]
RUNS_ON_INPUT: ${{ inputs.runs-on }}
IMAGES_INPUT: ${{ inputs.images }}
with:
script: |
const ociRegistryPassword = process.env.OCI_REGISTRY_PASSWORD;
if (!ociRegistryPassword) {
throw new Error(`"oci-registry-password" secret is missing`);
}
const runsOnInput = process.env.RUNS_ON_INPUT;
let runsOn = null;
try {
runsOn = JSON.parse(runsOnInput);
} catch (error) {
throw new Error(`"runs-on" input is not a valid JSON: ${error}`);
}
// jscpd:ignore-start
const imagesInput = process.env.IMAGES_INPUT;
// Check if is valid Json
let images = null;
try {
images = JSON.parse(imagesInput);
} catch (error) {
throw new Error(`"images" input is not a valid JSON: ${error}`);
}
// Check if is an array
if (!Array.isArray(images)) {
throw new Error(`"images" input is not an array`);
}
// jscpd:ignore-end
// Check each item
for (const key in images) {
const image = images[key];
if (typeof image !== 'object') {
throw new Error(`"images[${key}]" input is not an object`);
}
// Check mandatory properties
for (const property of ['name', 'platforms']) {
if (!image.hasOwnProperty(property)) {
throw new Error(`"images[${key}].${property}" input is missing`);
}
}
if (typeof image['name'] !== 'string') {
throw new Error(`"images[${key}].name" input is not a string`);
}
if (!Array.isArray(image['platforms'])) {
throw new Error(`"images[${key}].platforms" input is not an array`);
}
// Format build-args object to string
if (image['build-args']) {
const buildArgs = Object.keys(image['build-args'])
.map(key => `${key}=${image['build-args'][key]}`)
.join('\n');
image['build-args'] = buildArgs;
}
// Format secret-envs object to string
if (image['secret-envs']) {
const secretEnvs = Object.keys(image['secret-envs'])
.map(key => `${key}=${image['secret-envs'][key]}`)
.join('\n');
image['secret-envs'] = secretEnvs;
}
// Set default repository
if (!image['repository']) {
image['repository'] = `${context.repo.owner}/${context.repo.repo}`;
}
}
core.setOutput('images', JSON.stringify(images));
- id: define-images-by-platform
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
IMAGES: ${{ steps.validate-inputs.outputs.images }}
RUNS_ON_INPUT: ${{ inputs.runs-on }}
with:
script: |
const imagesInput = process.env.IMAGES;
const images = JSON.parse(imagesInput.replace(/\n/g, '\\n'));
const runsOnInput = process.env.RUNS_ON_INPUT;
const isDefaultRunsOn = runsOnInput === '["ubuntu-latest"]';
const runsOn = JSON.parse(runsOnInput);
// See https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners
const standardHostedRunnerByPlatform = [
{ runner: "macos-latest", platformPattern: /^darwin\// },
{ runner: "windows-latest", platformPattern: /^windows\// }
];
standardHostedRunnerByPlatform.unshift(
{ runner: "ubuntu-24.04-arm", platformPattern: /^linux\/arm/ }, // FIXME: should use latest when available,
{ runner: "windows-11-arm", platformPattern: /^windows\/arm/ }
);
function getPlatformRunsOn(platform) {
if (platform.hasOwnProperty('runs-on')) {
return platform['runs-on'];
}
if (!isDefaultRunsOn) {
return runsOn;
}
for (const { runner, platformPattern } of standardHostedRunnerByPlatform) {
if (platformPattern.test(platform.name)) {
return [runner];
}
}
return runsOn;
}
// Check each item
const imagesByPlatform = [];
for (const key in images) {
const image = images[key];
// Add image for each platform
const platforms = image['platforms'];
for (const platform of platforms) {
let platformName;
let platformRunsOn;
// Platform can be a string or an object
if (typeof platform === 'object') {
if (!platform.hasOwnProperty('name')) {
throw new Error(`"images[${key}].platforms[${platform}].name" input is missing`);
}
platformName = platform['name'];
platformRunsOn = getPlatformRunsOn(platform);
} else if (typeof platform === 'string') {
platformName = platform;
platformRunsOn = getPlatformRunsOn({ name: platform });
} else {
throw new Error(`"images[${key}].platforms[${platform}]" input is not a string or an object`);
}
const imageByPlatform = {
...image,
platform: platformName,
"runs-on": platformRunsOn,
// Flag multi-platform builds so downstream steps adjust publishing behavior
"multi-platform": platforms.length > 1,
};
imagesByPlatform.push(imageByPlatform);
}
}
core.debug(`Images by platform: ${JSON.stringify(imagesByPlatform, null, 2)}`);
core.setOutput('images', JSON.stringify(imagesByPlatform));
- id: define-artifact-name
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
function uniqid(prefix = "") {
const sec = Date.now() * 1000 + Math.random() * 1000;
const id = sec.toString(16).replace(/\./g, "").padEnd(14, "0");
return `${prefix}${id}`;
};
core.setOutput('artifact-name', uniqid("build-images-"));
build-images:
name: Build image ${{ matrix.image.name }} for ${{ matrix.image.platform }}
needs: prepare-variables
runs-on: ${{ matrix.image.runs-on }}
strategy:
fail-fast: false
matrix:
image: ${{ fromJson(needs.prepare-variables.outputs.images) }}
permissions:
contents: read
pull-requests: read
issues: read
packages: write
steps:
- uses: hoverkraft-tech/ci-github-common/actions/checkout@624be17604ee0a7378488191aacb35851e7cf001 # 0.37.1
with:
lfs: ${{ inputs.lfs }}
- if: inputs.lfs
shell: bash
run: git lfs pull
- id: local-workflow-actions
uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@624be17604ee0a7378488191aacb35851e7cf001 # 0.37.1
with:
actions-path: actions
- uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
if: inputs.build-secret-github-app-client-id
id: generate-token
with:
client-id: ${{ inputs.build-secret-github-app-client-id }}
owner: ${{ inputs.build-secret-github-app-owner }}
private-key: ${{ secrets.build-secret-github-app-key }} # zizmor: ignore[secrets-outside-env]
- id: prepare-secret-envs
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
SECRET_ENVS: ${{ matrix.image.secret-envs }}
GITHUB_APP_TOKEN: ${{ steps.generate-token.outputs.token }}
BUILD_SECRET_GITHUB_APP_TOKEN_ENV: ${{ inputs.build-secret-github-app-token-env }}
with:
script: |
const githubAppTokenEnvName = 'GITHUB_APP_TOKEN';
let secretEnvs = process.env.SECRET_ENVS || '';
const githubAppToken = process.env.GITHUB_APP_TOKEN || '';
if (!githubAppToken) {
if (secretEnvs) {
core.setOutput('secret-envs', secretEnvs);
}
return;
}
core.exportVariable(githubAppTokenEnvName, githubAppToken);
const buildSecretGithubAppTokenEnv = (process.env.BUILD_SECRET_GITHUB_APP_TOKEN_ENV || '').trim();
if (!buildSecretGithubAppTokenEnv.length) {
throw new Error(`"build-secret-github-app-token-env" input is empty`);
}
const mappedBuildSecretGithubAppTokenEnv = buildSecretGithubAppTokenEnv
.split('\n')
.map(key => key.trim())
.filter(Boolean)
.map(key => `${key}=${githubAppTokenEnvName}`).join('\n')
secretEnvs = secretEnvs.trim() + '\n' + mappedBuildSecretGithubAppTokenEnv.trim();
core.setOutput('secret-envs', secretEnvs.trim());
- id: build
uses: ./../self-workflow/actions/docker/build-image
with:
oci-registry: ${{ inputs.oci-registry }}
oci-registry-username: ${{ inputs.oci-registry-username }}
oci-registry-password: ${{ secrets.oci-registry-password }} # zizmor: ignore[secrets-outside-env]
repository: ${{ matrix.image.repository }}
image: ${{ matrix.image.name }}
tag: ${{ matrix.image.tag }}
context: ${{ matrix.image.context }}
dockerfile: ${{ matrix.image.dockerfile }}
build-args: ${{ matrix.image.build-args }}
target: ${{ matrix.image.target }}
platform: ${{ matrix.image.platform }}
secret-envs: ${{ steps.prepare-secret-envs.outputs.secret-envs }}
secrets: ${{ secrets.build-secrets }} # zizmor: ignore[secrets-outside-env]
cache-type: ${{ inputs.cache-type }}
buildkitd-config-inline: ${{ inputs.buildkitd-config-inline }}
multi-platform: ${{ matrix.image.multi-platform }}
# FIXME: Set built images infos in file to be uploaded as artifacts, because github action does not handle job outputs for matrix
# https://github.com/orgs/community/discussions/26639
- uses: hoverkraft-tech/ci-github-common/actions/set-matrix-output@624be17604ee0a7378488191aacb35851e7cf001 # 0.37.1
with:
artifact-name: ${{ needs.prepare-variables.outputs.artifact-name }}
value: ${{ steps.build.outputs.built-image }}
publish-manifests:
name: Publish images manifests
permissions:
contents: read
packages: write
id-token: write # Needed for signing images
needs: [prepare-variables, build-images]
runs-on: ${{ fromJson(inputs.runs-on) }}
outputs:
built-images: ${{ steps.create-images-manifests.outputs.built-images }}
steps:
- id: get-matrix-outputs
uses: hoverkraft-tech/ci-github-common/actions/get-matrix-outputs@624be17604ee0a7378488191aacb35851e7cf001 # 0.37.1
with:
artifact-name: ${{ needs.prepare-variables.outputs.artifact-name }}
- id: built-images
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
BUILT_IMAGES_INPUT: ${{ steps.get-matrix-outputs.outputs.result }}
with:
script: |
const builtImagesInput = process.env.BUILT_IMAGES_INPUT;
let builtImages = null;
try {
builtImages = JSON.parse(builtImagesInput);
} catch (error) {
throw new Error(`"built-images" input is not a valid JSON: ${error}`);
}
// Group by image name
const images = {};
builtImages.forEach(builtImage => {
const { name, image, platform, ...rest } = builtImage;
if (!images[name]) {
images[name] = {
name,
images: [image],
platforms: [platform],
...rest,
};
} else {
images[name].images = [...new Set([...images[name].images, image])];
images[name].platforms = [...new Set([...images[name].platforms, platform])];
}
});
core.setOutput('built-images', JSON.stringify(images));
- id: local-workflow-actions
uses: hoverkraft-tech/ci-github-common/actions/local-workflow-actions@624be17604ee0a7378488191aacb35851e7cf001 # 0.37.1
with:
actions-path: actions
- id: create-images-manifests
uses: ./../self-workflow/actions/docker/create-images-manifests
with:
oci-registry: ${{ inputs.oci-registry }}
oci-registry-username: ${{ inputs.oci-registry-username }}
oci-registry-password: ${{ secrets.oci-registry-password }} # zizmor: ignore[secrets-outside-env]
built-images: ${{ steps.built-images.outputs.built-images }}
- id: get-images-to-sign
if: inputs.sign
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
BUILT_IMAGES_INPUT: ${{ steps.create-images-manifests.outputs.built-images }}
with:
script: |
const builtImagesInput = process.env.BUILT_IMAGES_INPUT;
let builtImages = null;
try {
builtImages = JSON.parse(builtImagesInput);
} catch (error) {
throw new Error(`"built-images" input is not a valid JSON: ${error}`);
}
// Get images to sign
const imagesToSign = Object.values(builtImages).map(image => image.images).flat();
core.setOutput('images-to-sign', JSON.stringify(imagesToSign));
- uses: ./../self-workflow/actions/docker/sign-images
if: steps.get-images-to-sign.outputs.images-to-sign
with:
images: ${{ steps.get-images-to-sign.outputs.images-to-sign }}
github-token: ${{ secrets.GITHUB_TOKEN }}