-
Notifications
You must be signed in to change notification settings - Fork 18
build/bake: BuildKit secrets support #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,6 +137,9 @@ on: | |
| registry-auths: | ||
| description: "Raw authentication to registries, defined as YAML objects (for image output)" | ||
| required: false | ||
| build-secrets: | ||
| description: "YAML object mapping BuildKit secret IDs to secret values" | ||
| required: false | ||
| github-token: | ||
| description: "GitHub Token used to authenticate against the repository for Git context" | ||
| required: false | ||
|
|
@@ -707,6 +710,7 @@ jobs: | |
| INPUT_CACHE: ${{ inputs.cache }} | ||
| INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }} | ||
| INPUT_CACHE-MODE: ${{ inputs.cache-mode }} | ||
| INPUT_BUILD-SECRETS: ${{ secrets.build-secrets }} | ||
| INPUT_LABELS: ${{ inputs.labels }} | ||
| INPUT_CONTEXT: ${{ inputs.context }} | ||
| INPUT_OUTPUT: ${{ inputs.output }} | ||
|
|
@@ -721,12 +725,20 @@ jobs: | |
| INPUT_META-ANNOTATIONS: ${{ steps.meta.outputs.annotations }} | ||
| INPUT_SET-META-LABELS: ${{ inputs.set-meta-labels }} | ||
| INPUT_META-LABELS: ${{ steps.meta.outputs.labels }} | ||
| INPUT_GITHUB-TOKEN: ${{ secrets.github-token || github.token }} | ||
| with: | ||
| script: | | ||
| const { Build } = require('@docker/github-builder-runtime/lib/buildx/build'); | ||
| const { GitHub } = require('@docker/github-builder-runtime/lib/github/github'); | ||
| const { Util } = require('@docker/github-builder-runtime/lib/util'); | ||
|
|
||
|
|
||
| let yaml; | ||
| try { | ||
| yaml = require('js-yaml'); | ||
| } catch { | ||
| yaml = require('@docker/github-builder-runtime/node_modules/js-yaml'); | ||
| } | ||
|
|
||
| const inpPlatform = core.getInput('platform'); | ||
| const platformPairSuffix = inpPlatform ? `-${inpPlatform.replace(/\//g, '-')}` : ''; | ||
| core.setOutput('platform-pair-suffix', platformPairSuffix); | ||
|
|
@@ -740,6 +752,7 @@ jobs: | |
| const inpCache = core.getBooleanInput('cache'); | ||
| const inpCacheScope = core.getInput('cache-scope'); | ||
| const inpCacheMode = core.getInput('cache-mode'); | ||
| const inpBuildSecrets = core.getInput('build-secrets'); | ||
| const inpContext = core.getInput('context'); | ||
| const inpLabels = core.getInput('labels'); | ||
| const inpOutput = core.getInput('output'); | ||
|
|
@@ -755,6 +768,7 @@ jobs: | |
| const inpMetaAnnotations = core.getMultilineInput('meta-annotations'); | ||
| const inpSetMetaLabels = core.getBooleanInput('set-meta-labels'); | ||
| const inpMetaLabels = core.getMultilineInput('meta-labels'); | ||
| const inpGitHubToken = core.getInput('github-token'); | ||
|
|
||
| const meta = { | ||
| version: inpMetaVersion, | ||
|
|
@@ -763,6 +777,44 @@ jobs: | |
|
|
||
| const renderTemplate = value => Util.compileHandlebars(value, {noEscape: true}, {meta}); | ||
| const toMultilineInput = value => value.split(/\r?\n/).map(line => line.trim()).filter(Boolean); | ||
| const parseBuildSecrets = value => { | ||
| const normalized = value.trim(); | ||
| if (!normalized) { | ||
| return {}; | ||
| } | ||
| let parsed; | ||
| try { | ||
| parsed = yaml.load(normalized, {schema: yaml.FAILSAFE_SCHEMA}); | ||
| } catch (err) { | ||
| const location = err.mark ? ` at line ${err.mark.line + 1}, column ${err.mark.column + 1}` : ''; | ||
| throw new Error(`Failed to parse build-secrets YAML${location}`); | ||
| } | ||
| if (!parsed) { | ||
| return {}; | ||
| } | ||
| if (Array.isArray(parsed) || typeof parsed !== 'object') { | ||
| throw new Error('build-secrets must be a YAML object'); | ||
| } | ||
| const secrets = {}; | ||
| for (const [id, secret] of Object.entries(parsed)) { | ||
| if (!/^[A-Za-z0-9_.-]+$/.test(id)) { | ||
| throw new Error(`Invalid build secret id "${id}": use letters, digits, dots, underscores or dashes`); | ||
| } | ||
| if (id === 'GIT_AUTH_TOKEN') { | ||
| throw new Error('Build secret id "GIT_AUTH_TOKEN" is reserved for Git context authentication'); | ||
| } | ||
| if (typeof secret !== 'string') { | ||
| throw new Error(`build-secrets value for "${id}" must be a string`); | ||
| } | ||
| if (secret.length === 0) { | ||
| throw new Error(`build-secrets value for "${id}" must not be empty`); | ||
| } | ||
| core.setSecret(secret); | ||
| secrets[id] = secret; | ||
| } | ||
| return secrets; | ||
| }; | ||
| const toBuildSecretEnvName = (id, index) => `BUILD_SECRET_${index}_${id.toUpperCase().replace(/[^A-Z0-9_]/g, '_')}`; | ||
|
|
||
| const gitContextAttrs = GitHub.context.ref.startsWith('refs/tags/') ? {checksum: GitHub.context.sha} : {'fetch-by-commit': 'true'}; | ||
| const buildContext = await new Build().gitContext({subdir: inpContext, attrs: gitContextAttrs}); | ||
|
|
@@ -819,6 +871,28 @@ jobs: | |
| } | ||
| core.setOutput('labels', labels.join('\n')); | ||
| core.setOutput('build-args', buildArgs); | ||
|
|
||
| let buildSecrets; | ||
| try { | ||
| buildSecrets = parseBuildSecrets(inpBuildSecrets); | ||
| } catch (err) { | ||
| core.setFailed(err.message); | ||
| return; | ||
| } | ||
| const envs = { | ||
| BUILDKIT_MULTI_PLATFORM: '1', | ||
| GIT_AUTH_TOKEN: inpGitHubToken | ||
| }; | ||
| const secretEnvs = ['GIT_AUTH_TOKEN=GIT_AUTH_TOKEN']; | ||
| Object.entries(buildSecrets).forEach(([id, secret], index) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to verify that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes good catch, I added a guard that rejects a user-provided BuildKit secret ID of |
||
| const envName = toBuildSecretEnvName(id, index); | ||
| envs[envName] = secret; | ||
| secretEnvs.push(`${id}=${envName}`); | ||
| }); | ||
| Object.entries(envs).forEach(([key, value]) => { | ||
| core.exportVariable(key, value); | ||
| }); | ||
| core.setOutput('secret-envs', secretEnvs.join('\n')); | ||
|
|
||
| if (GitHub.context.payload.repository?.private ?? false) { | ||
| // if this is a private repository, we set min provenance mode | ||
|
|
@@ -849,13 +923,10 @@ jobs: | |
| platforms: ${{ steps.prepare.outputs.platform }} | ||
| provenance: ${{ steps.prepare.outputs.provenance }} | ||
| sbom: ${{ steps.prepare.outputs.sbom }} | ||
| secret-envs: GIT_AUTH_TOKEN=GIT_AUTH_TOKEN | ||
| secret-envs: ${{ steps.prepare.outputs.secret-envs }} | ||
| shm-size: ${{ inputs.shm-size }} | ||
| target: ${{ inputs.target }} | ||
| ulimit: ${{ inputs.ulimit }} | ||
| env: | ||
| BUILDKIT_MULTI_PLATFORM: 1 | ||
| GIT_AUTH_TOKEN: ${{ secrets.github-token || github.token }} | ||
| - | ||
| name: Login to registry for signing | ||
| if: ${{ needs.prepare.outputs.sign == 'true' && inputs.output == 'image' }} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this adding secrets to the definition? I would think secrets need to be defined already and just values are loaded here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dug into this more and I think the
secrets+=override is intentional here.For Bake,
target.secretdefines both the BuildKit secret ID and the source for that secret. If a caller hassecret = ["id=foo,env=MY_FOO"], only exporting another env var from the workflow does not change what Bake uses. Using*.secrets+=id=foo,env=<internal env>replaces the existing same-ID secret source instead of duplicating it, which I verified withbuildx bake --print.That keeps the reusable workflow contract as
build-secrets: { foo: value }and avoids making callers reference github-builder internal env names in their Bake files. It also lets existing Bake targets keep local sources for directdocker buildx bakeusage, while the reusable workflow overrides those sources when a matchingbuild-secretsentry is provided.I updated the docs to avoid saying file-based secrets are supported as workflow payloads. The workflow still accepts secret values only and exposes them to BuildKit from env vars. A Bake target can still declare a file source for local use, for example
type=file,id=aws,src=${HOME}/.aws/credentials, and the reusable workflow overrides that source whenbuild-secretscontainsaws.