Skip to content

Commit 0129ed6

Browse files
committed
bake: pass variables explicitly to buildx
Disable Bake environment variable lookup so workflow and runner environment values do not implicitly override Bake variables. Forward only GITHUB_ and RUNNER_ prefixed values as explicit Bake vars, then pass the caller vars input through docker/bake-action. Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent e69b8f7 commit 0129ed6

5 files changed

Lines changed: 88 additions & 35 deletions

File tree

‎.github/workflows/.test-bake.yml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,10 @@ jobs:
582582
contents: read
583583
id-token: write
584584
with:
585-
setup-qemu: true
586585
artifact-upload: false
587586
context: test
588587
output: local
589-
target: go
588+
target: vars
590589
vars: |
591590
XX_VERSION=1.9.0
592591

‎.github/workflows/bake.yml‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ jobs:
337337
const inpArtifactUpload = core.getBooleanInput('artifact-upload');
338338
const inpJobNamePrefix = core.getInput('job-name-prefix');
339339
const inpContext = core.getInput('context');
340-
const inpVars = Util.getInputList('vars');
340+
const inpVars = Util.getInputList('vars', {ignoreComma: true, quote: false});
341341
const inpFiles = Util.getInputList('files');
342342
const inpOutput = core.getInput('output');
343343
const inpPush = core.getBooleanInput('push');
@@ -471,22 +471,26 @@ jobs:
471471
core.info(bakeSource);
472472
});
473473
474-
const envs = Object.assign({},
475-
inpVars ? inpVars.reduce((acc, curr) => {
476-
const idx = curr.indexOf('=');
477-
if (idx !== -1) {
478-
acc[curr.substring(0, idx)] = curr.substring(idx + 1);
479-
}
480-
return acc;
481-
}, {}) : {},
482-
{
483-
BUILDKIT_MULTI_PLATFORM: '1',
484-
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
485-
}
486-
);
474+
const defaultBakeVars = Object.keys(process.env)
475+
.filter(key => key.startsWith('GITHUB_') || key.startsWith('RUNNER_'))
476+
.sort()
477+
.map(key => `${key}=${process.env[key] || ''}`);
478+
const bakeVars = [...defaultBakeVars, ...inpVars];
479+
const bakeVarKeys = bakeVars.map(value => {
480+
const idx = value.indexOf('=');
481+
return idx === -1 ? '<invalid>' : value.substring(0, idx);
482+
});
483+
const envs = {
484+
BUILDKIT_MULTI_PLATFORM: '1',
485+
BUILDX_BAKE_DISABLE_VARS_ENV_LOOKUP: '1',
486+
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
487+
};
487488
await core.group(`Set envs`, async () => {
488489
core.info(JSON.stringify(Object.keys(envs).sort(), null, 2));
489490
});
491+
await core.group(`Set bake vars`, async () => {
492+
core.info(JSON.stringify(bakeVarKeys.sort(), null, 2));
493+
});
490494
491495
const metaImages = inpMetaImages.map(image => image.toLowerCase());
492496
await core.group(`Set metaImages output`, async () => {
@@ -504,7 +508,8 @@ jobs:
504508
overrides: inpSet,
505509
sbom: inpSbom ? `generator=${inpSbomImage}` : 'false',
506510
source: bakeSource,
507-
targets: [inpTarget]
511+
targets: [inpTarget],
512+
vars: bakeVars
508513
}, {
509514
env: Object.keys(envs).length > 0 ? envs : undefined
510515
});
@@ -889,7 +894,7 @@ jobs:
889894
const inpSbom = core.getBooleanInput('sbom');
890895
const inpSet = Util.getInputList('set', {ignoreComma: true, quote: false});
891896
const inpTarget = core.getInput('target');
892-
const inpVars = Util.getInputList('vars');
897+
const inpVars = Util.getInputList('vars', {ignoreComma: true, quote: false});
893898
const inpMetaImages = core.getMultilineInput('meta-images');
894899
const inpMetaVersion = core.getInput('meta-version');
895900
const inpMetaTags = core.getMultilineInput('meta-tags');
@@ -983,31 +988,36 @@ jobs:
983988
return;
984989
}
985990
986-
const envs = Object.assign({},
987-
inpVars ? inpVars.reduce((acc, curr) => {
988-
const idx = curr.indexOf('=');
989-
if (idx !== -1) {
990-
acc[curr.substring(0, idx)] = curr.substring(idx + 1);
991-
}
992-
return acc;
993-
}, {}) : {},
994-
{
995-
BUILDKIT_MULTI_PLATFORM: '1',
996-
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
997-
}
998-
);
991+
const defaultBakeVars = Object.keys(process.env).filter(key => key.startsWith('GITHUB_') || key.startsWith('RUNNER_')).sort().map(key => `${key}=${process.env[key] || ''}`);
992+
const bakeVars = [...defaultBakeVars, ...inpVars];
993+
const bakeVarKeys = bakeVars.map(value => {
994+
const idx = value.indexOf('=');
995+
return idx === -1 ? '<invalid>' : value.substring(0, idx);
996+
});
997+
998+
const envs = {
999+
BUILDKIT_MULTI_PLATFORM: '1',
1000+
BUILDX_BAKE_DISABLE_VARS_ENV_LOOKUP: '1',
1001+
BUILDX_BAKE_GIT_AUTH_TOKEN: inpGitHubToken
1002+
};
1003+
9991004
const secretOverrides = [];
10001005
buildSecrets.forEach(({target, id, secret}, index) => {
10011006
const envName = toBuildSecretEnvName(id, index);
10021007
envs[envName] = secret;
10031008
secretOverrides.push(`${target}.secret.${id}=env=${envName}`);
10041009
});
1010+
10051011
await core.group(`Set envs`, async () => {
10061012
core.info(JSON.stringify(Object.keys(envs).sort(), null, 2));
10071013
Object.entries(envs).forEach(([key, value]) => {
10081014
core.exportVariable(key, value);
10091015
});
10101016
});
1017+
await core.group(`Set bake vars`, async () => {
1018+
core.info(JSON.stringify(bakeVarKeys.sort(), null, 2));
1019+
core.setOutput('vars', bakeVars.join(os.EOL));
1020+
});
10111021
10121022
let bakeFiles = inpFiles;
10131023
await core.group(`Set bake files`, async () => {
@@ -1133,6 +1143,7 @@ jobs:
11331143
targets: ${{ steps.prepare.outputs.target }}
11341144
sbom: ${{ steps.prepare.outputs.sbom }}
11351145
set: ${{ steps.prepare.outputs.overrides }}
1146+
vars: ${{ steps.prepare.outputs.vars }}
11361147
-
11371148
name: Get image digest
11381149
id: get-image-digest

‎README.md‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,19 @@ jobs:
367367
| `meta-annotations` | List | | [List of custom annotations](https://github.com/docker/metadata-action?tab=readme-ov-file#overwrite-labels-and-annotations) |
368368
| `meta-flavor` | List | | [Flavor](https://github.com/docker/metadata-action?tab=readme-ov-file#flavor-input) defines a global behavior for `meta-tags` |
369369

370+
> [!NOTE]
371+
> The `vars` input is passed to Buildx as explicit Bake variables. Ambient
372+
> environment lookup for Bake variables is disabled, except that step environment
373+
> variables with `GITHUB_` or `RUNNER_` prefixes are forwarded as explicit vars
374+
> for workflows that declare them in their Bake definition.
375+
370376
### Secrets
371377

372-
| Name | Default | Description |
373-
|------------------|-----------------------|--------------------------------------------------------------------------------|
374-
| `registry-auths` | | Raw authentication to registries, defined as YAML objects (for `image` output) |
378+
| Name | Default | Description |
379+
|------------------|-----------------------|-------------------------------------------------------------------------------------|
380+
| `registry-auths` | | Raw authentication to registries, defined as YAML objects (for `image` output) |
375381
| `build-secrets` | | YAML object mapping BuildKit secret IDs, optionally target-scoped, to secret values |
376-
| `github-token` | `${{ github.token }}` | GitHub Token used to authenticate against the repository for Git context |
382+
| `github-token` | `${{ github.token }}` | GitHub Token used to authenticate against the repository for Git context |
377383

378384
### Outputs
379385

‎test/docker-bake.hcl‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ variable "XX_VERSION" {
1515
default = null
1616
}
1717

18+
variable "BUILDX_VERSION" {
19+
default = "not-leaked"
20+
}
21+
22+
variable "GITHUB_SHA" {
23+
default = ""
24+
}
25+
26+
variable "RUNNER_OS" {
27+
default = ""
28+
}
29+
1830
target "go" {
1931
inherits = ["docker-metadata-action"]
2032
args = {
@@ -23,6 +35,16 @@ target "go" {
2335
dockerfile = "go.Dockerfile"
2436
}
2537

38+
target "vars" {
39+
args = {
40+
BUILDX_VERSION = BUILDX_VERSION
41+
GITHUB_SHA = GITHUB_SHA
42+
RUNNER_OS = RUNNER_OS
43+
XX_VERSION = XX_VERSION
44+
}
45+
dockerfile = "vars.Dockerfile"
46+
}
47+
2648
target "go-cross" {
2749
inherits = ["go"]
2850
platforms = ["linux/amd64", "linux/arm64"]

‎test/vars.Dockerfile‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM alpine
4+
ARG BUILDX_VERSION
5+
ARG GITHUB_SHA
6+
ARG RUNNER_OS
7+
ARG XX_VERSION
8+
RUN test "$BUILDX_VERSION" = "not-leaked"
9+
RUN test -n "$GITHUB_SHA"
10+
RUN test -n "$RUNNER_OS"
11+
RUN test "$XX_VERSION" = "1.9.0"
12+
RUN mkdir -p /out && printf 'ok\n' > /out/vars.txt
13+
14+
FROM scratch
15+
COPY --from=0 /out /

0 commit comments

Comments
 (0)