Skip to content

Commit 70685ff

Browse files
committed
bake: vars support
Signed-off-by: CrazyMax <[email protected]>
1 parent 478a43d commit 70685ff

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

.github/workflows/.test-bake.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,17 @@ jobs:
447447
sbom: true
448448
sign: ${{ github.event_name != 'pull_request' }}
449449
target: hello-cross
450+
451+
bake-vars:
452+
uses: ./.github/workflows/bake.yml
453+
permissions:
454+
contents: read
455+
id-token: write
456+
with:
457+
setup-qemu: true
458+
artifact-upload: false
459+
context: test
460+
output: local
461+
target: go
462+
vars: |
463+
XX_VERSION=1.9.0

.github/workflows/bake.yml

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ on:
7979
description: "Bake target to build"
8080
required: true
8181
default: default
82+
vars:
83+
type: string
84+
description: "Variables to set in the Bake definition as list of key-value pair"
85+
required: false
8286
# docker/metadata-action
8387
set-meta-annotations:
8488
type: boolean
@@ -189,6 +193,7 @@ jobs:
189193
INPUT_SET: ${{ inputs.set }}
190194
INPUT_SIGN: ${{ inputs.sign }}
191195
INPUT_TARGET: ${{ inputs.target }}
196+
INPUT_VARS: ${{ inputs.vars }}
192197
INPUT_GITHUB-TOKEN: ${{ secrets.github-token || github.token }}
193198
with:
194199
script: |
@@ -205,6 +210,7 @@ jobs:
205210
const inpDistribute = core.getBooleanInput('distribute');
206211
const inpArtifactUpload = core.getBooleanInput('artifact-upload');
207212
const inpContext = core.getInput('context');
213+
const inpVars = Util.getInputList('vars');
208214
const inpFiles = Util.getInputList('files');
209215
const inpOutput = core.getInput('output');
210216
const inpPush = core.getBooleanInput('push');
@@ -243,6 +249,21 @@ jobs:
243249
await core.group(`Set bake source`, async () => {
244250
core.info(bakeSource);
245251
});
252+
253+
const envs = Object.assign({},
254+
inpVars ? inpVars.reduce((acc, curr) => {
255+
const [key, ...rest] = curr.split('=');
256+
acc[key] = rest.join('=');
257+
return acc;
258+
}, {}) : {},
259+
{
260+
BUILDKIT_MULTI_PLATFORM: '1',
261+
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
262+
}
263+
);
264+
await core.group(`Set envs`, async () => {
265+
core.info(JSON.stringify(envs, null, 2));
266+
});
246267
247268
let def;
248269
let target;
@@ -254,8 +275,9 @@ jobs:
254275
overrides: inpSet,
255276
sbom: inpSbom ? `generator=${inpSbomImage}` : 'false',
256277
source: bakeSource,
257-
targets: [inpTarget],
258-
githubToken: inpGitHubToken
278+
targets: [inpTarget]
279+
}, {
280+
env: Object.keys(envs).length > 0 ? envs : undefined
259281
});
260282
if (!def) {
261283
throw new Error('Bake definition not set');
@@ -495,6 +517,7 @@ jobs:
495517
INPUT_SBOM: ${{ inputs.sbom }}
496518
INPUT_SET: ${{ inputs.set }}
497519
INPUT_TARGET: ${{ inputs.target }}
520+
INPUT_VARS: ${{ inputs.vars }}
498521
INPUT_META-IMAGES: ${{ inputs.meta-images }}
499522
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
500523
INPUT_SET-META-LABELS: ${{ inputs.set-meta-labels }}
@@ -526,6 +549,7 @@ jobs:
526549
const inpSbom = core.getBooleanInput('sbom');
527550
const inpSet = Util.getInputList('set', {ignoreComma: true, quote: false});
528551
const inpTarget = core.getInput('target');
552+
const inpVars = Util.getInputList('vars');
529553
const inpMetaImages = core.getMultilineInput('meta-images');
530554
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
531555
const inpSetMetaLabels = core.getBooleanInput('set-meta-labels');
@@ -546,6 +570,22 @@ jobs:
546570
core.setOutput('sbom', sbom);
547571
});
548572
573+
const envs = Object.assign({},
574+
inpVars ? inpVars.reduce((acc, curr) => {
575+
const [key, ...rest] = curr.split('=');
576+
acc[key] = rest.join('=');
577+
return acc;
578+
}, {}) : {},
579+
{
580+
BUILDKIT_MULTI_PLATFORM: '1',
581+
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
582+
}
583+
);
584+
await core.group(`Set envs`, async () => {
585+
core.info(JSON.stringify(envs, null, 2));
586+
core.setOutput('envs', JSON.stringify(envs));
587+
});
588+
549589
let target;
550590
try {
551591
await core.group(`Validating definition`, async () => {
@@ -555,8 +595,9 @@ jobs:
555595
overrides: inpSet,
556596
sbom: sbom,
557597
source: bakeSource,
558-
targets: [inpTarget],
559-
githubToken: inpGitHubToken
598+
targets: [inpTarget]
599+
}, {
600+
env: Object.keys(envs).length > 0 ? envs : undefined
560601
});
561602
if (!def) {
562603
throw new Error('Bake definition not set');
@@ -645,9 +686,7 @@ jobs:
645686
targets: ${{ steps.prepare.outputs.target }}
646687
sbom: ${{ steps.prepare.outputs.sbom }}
647688
set: ${{ steps.prepare.outputs.overrides }}
648-
env:
649-
BUILDKIT_MULTI_PLATFORM: 1
650-
BUILDX_BAKE_GIT_AUTH_TOKEN: ${{ secrets.github-token || github.token }}
689+
env: ${{ fromJson(steps.prepare.outputs.envs) }}
651690
-
652691
name: Get image digest
653692
id: get-image-digest

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ on:
248248
| `sign` | String | `auto` | Sign attestation manifest for `image` output or artifacts for `local` output, can be one of `auto`, `true` or `false`. The `auto` mode will enable signing if `push` is enabled for pushing the `image` or if `artifact-upload` is enabled for uploading the `local` build output as GitHub Artifact |
249249
| `target` | String | | Sets the target stage to build |
250250
| `ulimit` | List | | [Ulimit](https://docs.docker.com/engine/reference/commandline/buildx_build/#ulimit) options (e.g., `nofile=1024:1024`) |
251+
| `vars` | List | | [Variables](https://docs.docker.com/build/bake/variables/) to set in the Bake definition as list of key-value pair |
251252
| `set-meta-annotations` | Bool | `false` | Append OCI Image Format Specification annotations generated by `docker/metadata-action` |
252253
| `set-meta-labels` | Bool | `false` | Append OCI Image Format Specification labels generated by `docker/metadata-action` |
253254
| `meta-images` | List | | [List of images](https://github.com/docker/metadata-action?tab=readme-ov-file#images-input) to use as base name for tags (required for image output) |

test/docker-bake.hcl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ group "grp" {
1111
targets = ["go", "hello"]
1212
}
1313

14+
variable "XX_VERSION" {
15+
default = null
16+
}
17+
1418
target "go" {
1519
inherits = ["docker-metadata-action"]
20+
args = {
21+
XX_VERSION = XX_VERSION
22+
}
1623
dockerfile = "go.Dockerfile"
1724
}
1825

test/go.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# syntax=docker/dockerfile:1
22

33
ARG GO_VERSION="1.25"
4+
ARG XX_VERSION="1.7.0"
45

56
# xx is a helper for cross-compilation
6-
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.7.0 AS xx
7+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
78

89
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS base
910
COPY --from=xx / /

0 commit comments

Comments
 (0)