Skip to content

Commit e767ef9

Browse files
committed
bake: vars support
Signed-off-by: CrazyMax <[email protected]>
1 parent 62de965 commit e767ef9

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
@@ -408,3 +408,17 @@ jobs:
408408
output: local
409409
sbom: true
410410
target: hello-cross
411+
412+
bake-envs:
413+
uses: ./.github/workflows/bake.yml
414+
permissions:
415+
contents: read
416+
id-token: write
417+
with:
418+
setup-qemu: true
419+
artifact-upload: false
420+
context: test
421+
output: local
422+
target: go
423+
vars: |
424+
XX_VERSION=1.9.0

.github/workflows/bake.yml

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ on:
7474
description: "Bake target to build"
7575
required: true
7676
default: default
77+
vars:
78+
type: string
79+
description: "Variables to set in the Bake definition as list of key-value pair"
80+
required: false
7781
# docker/metadata-action
7882
set-meta-annotations:
7983
type: boolean
@@ -183,6 +187,7 @@ jobs:
183187
INPUT_SET: ${{ inputs.set }}
184188
INPUT_SIGN: ${{ inputs.sign }}
185189
INPUT_TARGET: ${{ inputs.target }}
190+
INPUT_VARS: ${{ inputs.vars }}
186191
INPUT_GITHUB-TOKEN: ${{ secrets.github-token || github.token }}
187192
with:
188193
script: |
@@ -198,6 +203,7 @@ jobs:
198203
const inpRunner = core.getInput('runner');
199204
const inpArtifactUpload = core.getBooleanInput('artifact-upload');
200205
const inpContext = core.getInput('context');
206+
const inpVars = Util.getInputList('vars');
201207
const inpFiles = Util.getInputList('files');
202208
const inpOutput = core.getInput('output');
203209
const inpPush = core.getBooleanInput('push');
@@ -236,6 +242,21 @@ jobs:
236242
await core.group(`Set bake source`, async () => {
237243
core.info(bakeSource);
238244
});
245+
246+
const envs = Object.assign({},
247+
inpVars ? inpVars.reduce((acc, curr) => {
248+
const [key, ...rest] = curr.split('=');
249+
acc[key] = rest.join('=');
250+
return acc;
251+
}, {}) : {},
252+
{
253+
BUILDKIT_MULTI_PLATFORM: '1',
254+
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
255+
}
256+
);
257+
await core.group(`Set envs`, async () => {
258+
core.info(JSON.stringify(envs, null, 2));
259+
});
239260
240261
let def;
241262
let target;
@@ -247,8 +268,9 @@ jobs:
247268
overrides: inpSet,
248269
sbom: inpSbom ? `generator=${inpSbomImage}` : 'false',
249270
source: bakeSource,
250-
targets: [inpTarget],
251-
githubToken: inpGitHubToken
271+
targets: [inpTarget]
272+
}, {
273+
env: Object.keys(envs).length > 0 ? envs : undefined
252274
});
253275
if (!def) {
254276
throw new Error('Bake definition not set');
@@ -488,6 +510,7 @@ jobs:
488510
INPUT_SBOM: ${{ inputs.sbom }}
489511
INPUT_SET: ${{ inputs.set }}
490512
INPUT_TARGET: ${{ inputs.target }}
513+
INPUT_VARS: ${{ inputs.vars }}
491514
INPUT_META-IMAGES: ${{ inputs.meta-images }}
492515
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
493516
INPUT_SET-META-LABELS: ${{ inputs.set-meta-labels }}
@@ -519,6 +542,7 @@ jobs:
519542
const inpSbom = core.getBooleanInput('sbom');
520543
const inpSet = Util.getInputList('set', {ignoreComma: true, quote: false});
521544
const inpTarget = core.getInput('target');
545+
const inpVars = Util.getInputList('vars');
522546
const inpMetaImages = core.getMultilineInput('meta-images');
523547
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
524548
const inpSetMetaLabels = core.getBooleanInput('set-meta-labels');
@@ -539,6 +563,22 @@ jobs:
539563
core.setOutput('sbom', sbom);
540564
});
541565
566+
const envs = Object.assign({},
567+
inpVars ? inpVars.reduce((acc, curr) => {
568+
const [key, ...rest] = curr.split('=');
569+
acc[key] = rest.join('=');
570+
return acc;
571+
}, {}) : {},
572+
{
573+
BUILDKIT_MULTI_PLATFORM: '1',
574+
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
575+
}
576+
);
577+
await core.group(`Set envs`, async () => {
578+
core.info(JSON.stringify(envs, null, 2));
579+
core.setOutput('envs', JSON.stringify(envs));
580+
});
581+
542582
let target;
543583
try {
544584
await core.group(`Validating definition`, async () => {
@@ -548,8 +588,9 @@ jobs:
548588
overrides: inpSet,
549589
sbom: sbom,
550590
source: bakeSource,
551-
targets: [inpTarget],
552-
githubToken: inpGitHubToken
591+
targets: [inpTarget]
592+
}, {
593+
env: Object.keys(envs).length > 0 ? envs : undefined
553594
});
554595
if (!def) {
555596
throw new Error('Bake definition not set');
@@ -638,9 +679,7 @@ jobs:
638679
targets: ${{ steps.prepare.outputs.target }}
639680
sbom: ${{ steps.prepare.outputs.sbom }}
640681
set: ${{ steps.prepare.outputs.overrides }}
641-
env:
642-
BUILDKIT_MULTI_PLATFORM: 1
643-
BUILDX_BAKE_GIT_AUTH_TOKEN: ${{ secrets.github-token || github.token }}
682+
env: ${{ fromJson(steps.prepare.outputs.envs) }}
644683
-
645684
name: Get image digest
646685
id: get-image-digest

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ on:
247247
| `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 |
248248
| `target` | String | | Sets the target stage to build |
249249
| `ulimit` | List | | [Ulimit](https://docs.docker.com/engine/reference/commandline/buildx_build/#ulimit) options (e.g., `nofile=1024:1024`) |
250+
| `vars` | List | | [Variables](https://docs.docker.com/build/bake/variables/) to set in the Bake definition as list of key-value pair |
250251
| `set-meta-annotations` | Bool | `false` | Append OCI Image Format Specification annotations generated by `docker/metadata-action` |
251252
| `set-meta-labels` | Bool | `false` | Append OCI Image Format Specification labels generated by `docker/metadata-action` |
252253
| `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)