Skip to content

Commit 972f3dc

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

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
@@ -42,6 +42,10 @@ on:
4242
description: "Context to build from in the Git working tree"
4343
required: false
4444
default: .
45+
vars:
46+
type: string
47+
description: "Variables to set in the Bake definition as list of key-value pair"
48+
required: false
4549
files:
4650
type: string
4751
description: "List of bake definition files"
@@ -176,6 +180,7 @@ jobs:
176180
INPUT_RUNNER: ${{ inputs.runner }}
177181
INPUT_ARTIFACT-UPLOAD: ${{ inputs.artifact-upload }}
178182
INPUT_CONTEXT: ${{ inputs.context }}
183+
INPUT_VARS: ${{ inputs.vars }}
179184
INPUT_FILES: ${{ inputs.files }}
180185
INPUT_OUTPUT: ${{ inputs.output }}
181186
INPUT_PUSH: ${{ inputs.push }}
@@ -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');
@@ -482,6 +504,7 @@ jobs:
482504
INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }}
483505
INPUT_CACHE-MODE: ${{ inputs.cache-mode }}
484506
INPUT_CONTEXT: ${{ inputs.context }}
507+
INPUT_VARS: ${{ inputs.vars }}
485508
INPUT_FILES: ${{ inputs.files }}
486509
INPUT_OUTPUT: ${{ inputs.output }}
487510
INPUT_PUSH: ${{ inputs.push }}
@@ -513,6 +536,7 @@ jobs:
513536
const inpCacheScope = core.getInput('cache-scope');
514537
const inpCacheMode = core.getInput('cache-mode');
515538
const inpContext = core.getInput('context');
539+
const inpVars = Util.getInputList('vars');
516540
const inpFiles = Util.getInputList('files');
517541
const inpOutput = core.getInput('output');
518542
const inpPush = core.getBooleanInput('push');
@@ -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)