Skip to content

Commit b9cdc58

Browse files
committed
support metadata handlebars in build and bake inputs
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 1fbf7b5 commit b9cdc58

File tree

6 files changed

+157
-17
lines changed

6 files changed

+157
-17
lines changed

.github/workflows/.test-bake.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ jobs:
3939
output: image
4040
push: ${{ github.event_name != 'pull_request' }}
4141
sbom: true
42+
set: |
43+
*.args.VERSION={{meta.version}}
4244
target: hello
4345
meta-images: |
4446
public.ecr.aws/q3b5f1u4/test-docker-action
@@ -90,6 +92,8 @@ jobs:
9092
output: image
9193
push: ${{ github.event_name != 'pull_request' }}
9294
sbom: true
95+
set: |
96+
*.args.VERSION={{meta.version}}
9397
target: hello-cross
9498
meta-images: |
9599
public.ecr.aws/q3b5f1u4/test-docker-action
@@ -160,6 +164,8 @@ jobs:
160164
output: image
161165
push: ${{ github.event_name != 'pull_request' }}
162166
sbom: true
167+
set: |
168+
*.args.VERSION={{meta.version}}
163169
sign: false
164170
target: hello-cross
165171
meta-images: |
@@ -210,6 +216,8 @@ jobs:
210216
output: image
211217
push: ${{ github.event_name != 'pull_request' }}
212218
sbom: true
219+
set: |
220+
*.args.VERSION={{meta.version}}
213221
target: hello-cross
214222
meta-images: |
215223
registry-1-stage.docker.io/docker/github-builder-test
@@ -260,6 +268,8 @@ jobs:
260268
output: image
261269
push: ${{ github.event_name != 'pull_request' }}
262270
sbom: true
271+
set: |
272+
*.args.VERSION={{meta.version}}
263273
target: hello-cross
264274
meta-images: |
265275
ghcr.io/docker/github-builder-test
@@ -460,6 +470,8 @@ jobs:
460470
context: test
461471
output: image
462472
push: false
473+
set: |
474+
*.args.VERSION={{meta.version}}
463475
target: hello-cross
464476
meta-images: |
465477
public.ecr.aws/q3b5f1u4/test-docker-action
@@ -492,6 +504,8 @@ jobs:
492504
output: image
493505
push: ${{ github.event_name != 'pull_request' }}
494506
sbom: true
507+
set: |
508+
*.args.VERSION={{meta.version}}
495509
target: hello-cross
496510
meta-images: |
497511
public.ecr.aws/q3b5f1u4/test-docker-action
@@ -542,6 +556,8 @@ jobs:
542556
output: image
543557
push: ${{ github.event_name != 'pull_request' }}
544558
sbom: true
559+
set: |
560+
*.args.VERSION={{meta.version}}
545561
target: hello-cross
546562
meta-images: |
547563
registry-1-stage.docker.io/docker/github-builder-test

.github/workflows/.test-build.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
contents: read
3434
id-token: write
3535
with:
36+
build-args: |
37+
VERSION={{meta.version}}
3638
cache: true
3739
cache-scope: build-aws-single
3840
file: test/hello.Dockerfile
@@ -84,6 +86,8 @@ jobs:
8486
contents: read
8587
id-token: write
8688
with:
89+
build-args: |
90+
VERSION={{meta.version}}
8791
cache: true
8892
cache-scope: build-aws
8993
file: test/hello.Dockerfile
@@ -154,6 +158,8 @@ jobs:
154158
contents: read
155159
id-token: write
156160
with:
161+
build-args: |
162+
VERSION={{meta.version}}
157163
cache: true
158164
cache-scope: build-aws-nosign
159165
file: test/hello.Dockerfile
@@ -207,6 +213,8 @@ jobs:
207213
id-token: write
208214
packages: write
209215
with:
216+
build-args: |
217+
VERSION={{meta.version}}
210218
file: test/hello.Dockerfile
211219
output: image
212220
platforms: linux/amd64,linux/arm64
@@ -255,6 +263,8 @@ jobs:
255263
contents: read
256264
id-token: write
257265
with:
266+
build-args: |
267+
VERSION={{meta.version}}
258268
file: test/hello.Dockerfile
259269
output: image
260270
platforms: linux/amd64,linux/arm64
@@ -304,6 +314,8 @@ jobs:
304314
id-token: write
305315
packages: write
306316
with:
317+
build-args: |
318+
VERSION={{meta.version}}
307319
file: test/hello.Dockerfile
308320
output: image
309321
platforms: linux/amd64,linux/arm64
@@ -504,6 +516,8 @@ jobs:
504516
id-token: write
505517
with:
506518
runner: amd64
519+
build-args: |
520+
VERSION={{meta.version}}
507521
file: test/hello.Dockerfile
508522
output: image
509523
platforms: linux/amd64,linux/arm64
@@ -532,6 +546,8 @@ jobs:
532546
id-token: write
533547
with:
534548
distribute: false
549+
build-args: |
550+
VERSION={{meta.version}}
535551
cache: true
536552
cache-scope: build-aws-nodistrib
537553
file: test/hello.Dockerfile
@@ -570,6 +586,8 @@ jobs:
570586
contents: read
571587
id-token: write
572588
with:
589+
build-args: |
590+
VERSION={{meta.version}}
573591
file: test/hello.Dockerfile
574592
output: image
575593
platforms: linux/amd64,linux/arm64

.github/workflows/bake.yml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ env:
154154
SBOM_IMAGE: "docker/buildkit-syft-scanner:1.10.0"
155155
BINFMT_IMAGE: "tonistiigi/binfmt:qemu-v10.2.1-65"
156156
DOCKER_ACTIONS_TOOLKIT_MODULE: "@docker/actions-toolkit@0.81.0"
157+
HANDLEBARS_MODULE: "handlebars@4.7.8"
157158
COSIGN_VERSION: "v3.0.2"
158159
LOCAL_EXPORT_DIR: "/tmp/buildx-output"
159160
MATRIX_SIZE_LIMIT: "20"
@@ -167,7 +168,7 @@ jobs:
167168
ghaCacheSign: ${{ steps.set.outputs.ghaCacheSign }}
168169
steps:
169170
-
170-
name: Install @docker/actions-toolkit
171+
name: Install dependencies
171172
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
172173
env:
173174
INPUT_DAT-MODULE: ${{ env.DOCKER_ACTIONS_TOOLKIT_MODULE }}
@@ -458,13 +459,20 @@ jobs:
458459
result_19: ${{ steps.result.outputs.result_19 }}
459460
steps:
460461
-
461-
name: Install @docker/actions-toolkit
462+
name: Install dependencies
462463
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
463464
env:
464465
INPUT_DAT-MODULE: ${{ env.DOCKER_ACTIONS_TOOLKIT_MODULE }}
466+
INPUT_HANDLEBARS-MODULE: ${{ env.HANDLEBARS_MODULE }}
465467
with:
466468
script: |
467-
await exec.exec('npm', ['install', '--prefer-offline', '--ignore-scripts', core.getInput('dat-module')]);
469+
await exec.exec('npm', [
470+
'install',
471+
'--prefer-offline',
472+
'--ignore-scripts',
473+
core.getInput('dat-module'),
474+
core.getInput('handlebars-module')
475+
]);
468476
-
469477
name: Docker meta
470478
id: meta
@@ -610,6 +618,8 @@ jobs:
610618
INPUT_TARGET: ${{ inputs.target }}
611619
INPUT_VARS: ${{ inputs.vars }}
612620
INPUT_META-IMAGES: ${{ inputs.meta-images }}
621+
INPUT_META-VERSION: ${{ steps.meta.outputs.version }}
622+
INPUT_META-TAGS: ${{ steps.meta.outputs.tags }}
613623
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
614624
INPUT_SET-META-LABELS: ${{ inputs.set-meta-labels }}
615625
INPUT_BAKE-FILE-TAGS: ${{ steps.meta.outputs.bake-file-tags }}
@@ -619,6 +629,7 @@ jobs:
619629
with:
620630
script: |
621631
const os = require('os');
632+
const Handlebars = require('handlebars');
622633
const { Build } = require('@docker/actions-toolkit/lib/buildx/build');
623634
const { GitHub } = require('@docker/actions-toolkit/lib/github/github');
624635
const { Util } = require('@docker/actions-toolkit/lib/util');
@@ -642,12 +653,20 @@ jobs:
642653
const inpTarget = core.getInput('target');
643654
const inpVars = Util.getInputList('vars');
644655
const inpMetaImages = core.getMultilineInput('meta-images');
656+
const inpMetaVersion = core.getInput('meta-version');
657+
const inpMetaTags = core.getMultilineInput('meta-tags');
645658
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
646659
const inpSetMetaLabels = core.getBooleanInput('set-meta-labels');
647660
const inpBakeFileTags = core.getInput('bake-file-tags');
648661
const inpBakeFileAnnotations = core.getInput('bake-file-annotations');
649662
const inpBakeFileLabels = core.getInput('bake-file-labels');
650663
const inpGitHubToken = core.getInput('github-token');
664+
665+
const meta = {
666+
version: inpMetaVersion,
667+
tags: inpMetaTags
668+
};
669+
const renderTemplate = value => Handlebars.compile(value, {noEscape: true})({meta});
651670
652671
const bakeSource = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}.git#${process.env.GITHUB_REF}:${inpContext}`;
653672
await core.group(`Set source output`, async () => {
@@ -719,8 +738,15 @@ jobs:
719738
return;
720739
}
721740
722-
let bakeOverrides = [...inpSet, outputOverride];
741+
let bakeOverrides = [];
742+
try {
743+
bakeOverrides = inpSet.map(override => renderTemplate(override));
744+
} catch (err) {
745+
core.setFailed(`Failed to render Handlebars template: ${err.message}`);
746+
return;
747+
}
723748
await core.group(`Set bake overrides`, async () => {
749+
bakeOverrides.push(outputOverride);
724750
bakeOverrides.push('*.tags=');
725751
if (GitHub.context.payload.repository?.private ?? false) {
726752
// if this is a private repository, we set min provenance mode
@@ -915,7 +941,7 @@ jobs:
915941
- build
916942
steps:
917943
-
918-
name: Install @docker/actions-toolkit
944+
name: Install dependencies
919945
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
920946
env:
921947
INPUT_DAT-MODULE: ${{ env.DOCKER_ACTIONS_TOOLKIT_MODULE }}

0 commit comments

Comments
 (0)