Skip to content

Commit 64169d7

Browse files
committed
build/bake: registry-login input for optional auth before build
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 1e7f549 commit 64169d7

7 files changed

Lines changed: 122 additions & 11 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,3 +710,25 @@ jobs:
710710
- registry: ghcr.io
711711
username: ${{ github.actor }}
712712
password: ${{ secrets.GITHUB_TOKEN }}
713+
714+
bake-local-login:
715+
uses: ./.github/workflows/bake.yml
716+
if: ${{ github.event_name != 'pull_request' }}
717+
permissions:
718+
contents: read
719+
id-token: write
720+
with:
721+
artifact-name: bake-login-output
722+
artifact-upload: true
723+
context: test
724+
output: local
725+
registry-login: true
726+
sbom: true
727+
sign: true
728+
target: dhi
729+
registry-identities: |
730+
- type: dockerhub
731+
registry: dhi.io
732+
username: ${{ vars.DOCKERHUB_OIDC_USERNAME }}
733+
connection_id: ${{ vars.DOCKERHUB_OIDC_CONNECTIONID }}
734+
scope: 'dhi.io@pull'

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,24 @@ jobs:
727727
- registry: ghcr.io
728728
username: ${{ github.actor }}
729729
password: ${{ secrets.GITHUB_TOKEN }}
730+
731+
build-local-login:
732+
uses: ./.github/workflows/build.yml
733+
if: ${{ github.event_name != 'pull_request' }}
734+
permissions:
735+
contents: read
736+
id-token: write
737+
with:
738+
artifact-name: build-login-output
739+
artifact-upload: true
740+
file: test/dhi.Dockerfile
741+
output: local
742+
registry-login: true
743+
sbom: true
744+
sign: true
745+
registry-identities: |
746+
- type: dockerhub
747+
registry: dhi.io
748+
username: ${{ vars.DOCKERHUB_OIDC_USERNAME }}
749+
connection_id: ${{ vars.DOCKERHUB_OIDC_CONNECTIONID }}
750+
scope: 'dhi.io@pull'

‎.github/workflows/bake.yml‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ on:
7777
description: "Push image to the registry (for image output)"
7878
required: false
7979
default: false
80+
registry-login:
81+
type: string
82+
description: "Login to registry before build (one of auto, true or false). Auto enables login only when output is image and push is true"
83+
required: false
84+
default: auto
8085
sbom:
8186
type: boolean
8287
description: "Generate SBOM attestation for the build"
@@ -147,7 +152,7 @@ on:
147152
default: false
148153
secrets:
149154
registry-auths:
150-
description: "Raw authentication to registries, defined as YAML objects (for image output)"
155+
description: "Raw authentication to registries, defined as YAML objects"
151156
required: false
152157
build-secrets:
153158
description: "YAML object mapping BuildKit secret IDs to values, with optional nested target mappings"
@@ -204,7 +209,7 @@ jobs:
204209
registry-identities:
205210
uses: ./.github/workflows/setup-registry-identities.yml
206211
with:
207-
registry-login: ${{ inputs.push && inputs.output == 'image' }}
212+
registry-login: ${{ inputs.registry-login == 'true' || (inputs.registry-login == 'auto' && inputs.push && inputs.output == 'image') }}
208213
registry-identities: ${{ inputs.registry-identities }}
209214

210215
prepare:
@@ -221,6 +226,7 @@ jobs:
221226
targets: ${{ steps.set.outputs.targets }}
222227
ghaCacheSign: ${{ steps.set.outputs.ghaCacheSign }}
223228
proxyNetwork: ${{ steps.set.outputs.proxyNetwork }}
229+
registryLogin: ${{ steps.set.outputs.registryLogin }}
224230
steps:
225231
-
226232
name: Install dependencies
@@ -320,6 +326,7 @@ jobs:
320326
INPUT_FILES: ${{ inputs.files }}
321327
INPUT_OUTPUT: ${{ inputs.output }}
322328
INPUT_PUSH: ${{ inputs.push }}
329+
INPUT_REGISTRY-LOGIN: ${{ inputs.registry-login }}
323330
INPUT_SBOM: ${{ inputs.sbom }}
324331
INPUT_SET: ${{ inputs.set }}
325332
INPUT_SIGN: ${{ inputs.sign }}
@@ -351,6 +358,7 @@ jobs:
351358
const inpFiles = Util.getInputList('files');
352359
const inpOutput = core.getInput('output');
353360
const inpPush = core.getBooleanInput('push');
361+
const inpRegistryLogin = core.getInput('registry-login');
354362
const inpSbom = core.getBooleanInput('sbom');
355363
const inpSet = Util.getInputList('set', {ignoreComma: true, quote: false});
356364
const inpSign = core.getInput('sign');
@@ -389,7 +397,13 @@ jobs:
389397
await core.group(`Set bake source`, async () => {
390398
core.info(bakeSource);
391399
});
392-
400+
401+
if (!['auto', 'true', 'false'].includes(inpRegistryLogin)) {
402+
core.setFailed(`Invalid registry-login input: ${inpRegistryLogin}`);
403+
return;
404+
}
405+
const registryLogin = inpRegistryLogin === 'auto' ? inpOutput === 'image' && inpPush : inpRegistryLogin === 'true';
406+
393407
const envs = Object.assign({},
394408
inpVars ? inpVars.reduce((acc, curr) => {
395409
const idx = curr.indexOf('=');
@@ -444,7 +458,7 @@ jobs:
444458
core.setFailed(error);
445459
return;
446460
}
447-
461+
448462
const platforms = def.target[target].platforms || [];
449463
if (inpDistribute && platforms.length > inpMatrixSizeLimit) {
450464
core.setFailed(`Platforms to build exceed matrix size limit of ${inpMatrixSizeLimit}`);
@@ -497,6 +511,10 @@ jobs:
497511
core.info(`ghaCacheSign: ${ghaCacheSign}`);
498512
core.setOutput('ghaCacheSign', ghaCacheSign);
499513
});
514+
await core.group(`Set registryLogin output`, async () => {
515+
core.info(`registryLogin: ${registryLogin}`);
516+
core.setOutput('registryLogin', registryLogin);
517+
});
500518
501519
build:
502520
runs-on: ${{ matrix.runner }}
@@ -794,7 +812,7 @@ jobs:
794812
libraries-host: ${{ needs.registry-identities.outputs.chainguard-libraries-host }}
795813
-
796814
name: Login to registry
797-
if: ${{ inputs.push && inputs.output == 'image' && env.REGISTRY_AUTHS_PRESENT == 'true' }}
815+
if: ${{ needs.prepare.outputs.registryLogin == 'true' && env.REGISTRY_AUTHS_PRESENT == 'true' }}
798816
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
799817
with:
800818
registry-auth: ${{ secrets.registry-auths }}

‎.github/workflows/build.yml‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ on:
9393
description: "Push image to the registry (for image output)"
9494
required: false
9595
default: false
96+
registry-login:
97+
type: string
98+
description: "Login to registry before build (one of auto, true or false). Auto enables login only when output is image and push is true"
99+
required: false
100+
default: auto
96101
sbom:
97102
type: boolean
98103
description: "Generate SBOM attestation for the build"
@@ -158,7 +163,7 @@ on:
158163
default: false
159164
secrets:
160165
registry-auths:
161-
description: "Raw authentication to registries, defined as YAML objects (for image output)"
166+
description: "Raw authentication to registries, defined as YAML objects"
162167
required: false
163168
build-secrets:
164169
description: "YAML object mapping BuildKit secret IDs to secret values"
@@ -215,7 +220,7 @@ jobs:
215220
registry-identities:
216221
uses: ./.github/workflows/setup-registry-identities.yml
217222
with:
218-
registry-login: ${{ inputs.push && inputs.output == 'image' }}
223+
registry-login: ${{ inputs.registry-login == 'true' || (inputs.registry-login == 'auto' && inputs.push && inputs.output == 'image') }}
219224
registry-identities: ${{ inputs.registry-identities }}
220225

221226
prepare:
@@ -231,6 +236,7 @@ jobs:
231236
privateRepo: ${{ steps.set.outputs.privateRepo }}
232237
ghaCacheSign: ${{ steps.set.outputs.ghaCacheSign }}
233238
proxyNetwork: ${{ steps.set.outputs.proxyNetwork }}
239+
registryLogin: ${{ steps.set.outputs.registryLogin }}
234240
steps:
235241
-
236242
name: Install dependencies
@@ -320,6 +326,7 @@ jobs:
320326
INPUT_OUTPUT: ${{ inputs.output }}
321327
INPUT_PLATFORMS: ${{ inputs.platforms }}
322328
INPUT_PUSH: ${{ inputs.push }}
329+
INPUT_REGISTRY-LOGIN: ${{ inputs.registry-login }}
323330
INPUT_SIGN: ${{ inputs.sign }}
324331
INPUT_BUILDKIT-PROXY-NETWORK: ${{ inputs.buildkit-proxy-network }}
325332
with:
@@ -339,6 +346,7 @@ jobs:
339346
const inpPlatforms = Util.getInputList('platforms');
340347
const inpOutput = core.getInput('output');
341348
const inpPush = core.getBooleanInput('push');
349+
const inpRegistryLogin = core.getInput('registry-login');
342350
const inpSign = core.getInput('sign');
343351
const inpProxyNetwork = core.getBooleanInput('buildkit-proxy-network');
344352
@@ -367,7 +375,13 @@ jobs:
367375
core.setFailed(`signing attestation manifests requires push to be enabled`);
368376
return;
369377
}
370-
378+
379+
if (!['auto', 'true', 'false'].includes(inpRegistryLogin)) {
380+
core.setFailed(`Invalid registry-login input: ${inpRegistryLogin}`);
381+
return;
382+
}
383+
const registryLogin = inpRegistryLogin === 'auto' ? inpOutput === 'image' && inpPush : inpRegistryLogin === 'true';
384+
371385
if (inpDistribute && inpPlatforms.length > inpMatrixSizeLimit) {
372386
core.setFailed(`Platforms to build exceed matrix size limit of ${inpMatrixSizeLimit}`);
373387
return;
@@ -425,6 +439,10 @@ jobs:
425439
core.info(`ghaCacheSign: ${ghaCacheSign}`);
426440
core.setOutput('ghaCacheSign', ghaCacheSign);
427441
});
442+
await core.group(`Set registryLogin output`, async () => {
443+
core.info(`registryLogin: ${registryLogin}`);
444+
core.setOutput('registryLogin', registryLogin);
445+
});
428446
429447
build:
430448
runs-on: ${{ matrix.runner }}
@@ -721,7 +739,7 @@ jobs:
721739
libraries-host: ${{ needs.registry-identities.outputs.chainguard-libraries-host }}
722740
-
723741
name: Login to registry
724-
if: ${{ inputs.push && inputs.output == 'image' && env.REGISTRY_AUTHS_PRESENT == 'true' }}
742+
if: ${{ needs.prepare.outputs.registryLogin == 'true' && env.REGISTRY_AUTHS_PRESENT == 'true' }}
725743
uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0
726744
with:
727745
registry-auth: ${{ secrets.registry-auths }}

‎README.md‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ___
2727
* [Azure Container Registry](#azure-container-registry)
2828
* [Chainguard](#chainguard)
2929
* [Runner mapping](#runner-mapping)
30+
* [Registry login](#registry-login)
3031
* [Metadata templates](#metadata-templates)
3132

3233
## Overview
@@ -244,6 +245,7 @@ jobs:
244245
| `platforms` | List/CSV | | List of [target platforms](https://docs.docker.com/engine/reference/commandline/buildx_build/#platform) to build |
245246
| `push` | Bool | `false` | [Push](https://docs.docker.com/engine/reference/commandline/buildx_build/#push) image to the registry (for `image` output) |
246247
| `registry-identities` | YAML | | Keyless registry identity configuration. See [Registry identities](#registry-identities). |
248+
| `registry-login` | String | `auto` | [Login to registry](#registry-login) before build (one of `auto`, `true` or `false`) |
247249
| `sbom` | Bool | `false` | Generate [SBOM](https://docs.docker.com/build/attestations/sbom/) attestation for the build |
248250
| `shm-size` | String | | Size of [`/dev/shm`](https://docs.docker.com/engine/reference/commandline/buildx_build/#shm-size) (e.g., `2g`) |
249251
| `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 |
@@ -262,7 +264,7 @@ jobs:
262264

263265
| Name | Default | Description |
264266
|------------------|-----------------------|--------------------------------------------------------------------------------|
265-
| `registry-auths` | | Raw authentication to registries, defined as YAML objects (for `image` output) |
267+
| `registry-auths` | | Raw authentication to registries, defined as YAML objects |
266268
| `build-secrets` | | YAML object mapping BuildKit secret IDs to secret values |
267269
| `github-token` | `${{ github.token }}` | GitHub Token used to authenticate against the repository for Git context |
268270

@@ -358,6 +360,7 @@ jobs:
358360
| `output` | String | | Build output destination (one of [`image`](https://docs.docker.com/build/exporters/image-registry/) or [`local`](https://docs.docker.com/build/exporters/local-tar/)). |
359361
| `push` | Bool | `false` | Push image to the registry (for `image` output) |
360362
| `registry-identities` | YAML | | Keyless registry identity configuration. See [Registry identities](#registry-identities). |
363+
| `registry-login` | String | `auto` | [Login to registry](#registry-login) before build (one of `auto`, `true` or `false`) |
361364
| `sbom` | Bool | `false` | Generate [SBOM](https://docs.docker.com/build/attestations/sbom/) attestation for the build |
362365
| `set` | List | | List of [target values to override](https://docs.docker.com/engine/reference/commandline/buildx_bake/#set) (e.g., `targetpattern.key=value`) |
363366
| `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 |
@@ -376,7 +379,7 @@ jobs:
376379

377380
| Name | Default | Description |
378381
|------------------|-----------------------|-----------------------------------------------------------------------------------------|
379-
| `registry-auths` | | Raw authentication to registries, defined as YAML objects (for `image` output) |
382+
| `registry-auths` | | Raw authentication to registries, defined as YAML objects |
380383
| `build-secrets` | | YAML object mapping BuildKit secret IDs to values, with optional nested target mappings |
381384
| `github-token` | `${{ github.token }}` | GitHub Token used to authenticate against the repository for Git context |
382385

@@ -737,6 +740,21 @@ runner: |
737740
For example, `linux` matches all Linux platforms, `linux/arm` matches variants
738741
such as `linux/arm/v7`, and `linux/arm64` is separate from `linux/arm`.
739742

743+
### Registry login
744+
745+
The `registry-login` input controls whether the workflows run a registry login
746+
before the build step. `auto` preserves the existing behavior and enables login
747+
only when `output=image` and `push=true`.
748+
749+
When `registry-login=true`, the workflow always attempts pre-build login. If
750+
credentials resolve to empty values, for example on forked pull requests where
751+
secrets are not exposed, login fails. Gate the input at the caller side for
752+
fork-safe workflows:
753+
754+
```yaml
755+
registry-login: ${{ github.event_name != 'pull_request' || !github.event.pull_request.head.repo.fork }}
756+
```
757+
740758
### Metadata templates
741759

742760
When `output=image`, some inputs support Handlebars templates rendered from

‎test/dhi.Dockerfile‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM dhi.io/alpine-base:3.23 AS base
4+
ARG TARGETPLATFORM
5+
RUN echo "Hello, World! This is ${TARGETPLATFORM}" > /tmp/hello.txt
6+
ARG BUILDKIT_SBOM_SCAN_STAGE=true
7+
8+
FROM scratch
9+
COPY --from=base /tmp/hello.txt /

‎test/docker-bake.hcl‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ target "generated-hello2" {
8181
dockerfile = "hello.Dockerfile"
8282
output = ["type=cacheonly"]
8383
}
84+
85+
target "dhi" {
86+
inherits = ["docker-metadata-action"]
87+
dockerfile = "dhi.Dockerfile"
88+
}

0 commit comments

Comments
 (0)