Skip to content

Commit 0191543

Browse files
committed
fix provenance input
When `provenance: false`, we should not set the builder-id. Signed-off-by: CrazyMax <[email protected]>
1 parent 2a16835 commit 0191543

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,41 @@ jobs:
523523
file: ./test/go/Dockerfile
524524
outputs: type=cacheonly
525525

526+
provenance:
527+
runs-on: ubuntu-latest
528+
strategy:
529+
fail-fast: false
530+
matrix:
531+
attrs:
532+
- ''
533+
- mode=max
534+
- builder-id=foo
535+
- false
536+
- true
537+
steps:
538+
-
539+
name: Checkout
540+
uses: actions/checkout@v3
541+
-
542+
name: Set up Docker Buildx
543+
uses: docker/setup-buildx-action@v2
544+
with:
545+
version: ${{ inputs.buildx-version || env.BUILDX_VERSION }}
546+
driver-opts: |
547+
network=host
548+
image=${{ inputs.buildkit-image || env.BUILDKIT_IMAGE }}
549+
-
550+
name: Build
551+
uses: ./
552+
with:
553+
context: ./test/go
554+
file: ./test/go/Dockerfile
555+
target: binary
556+
outputs: type=oci,dest=/tmp/build.tar
557+
provenance: ${{ matrix.attrs }}
558+
cache-from: type=gha,scope=provenance
559+
cache-to: type=gha,scope=provenance,mode=max
560+
526561
sbom:
527562
runs-on: ubuntu-latest
528563
strategy:

src/context.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export function tmpNameSync(options?: tmp.TmpNameOptions): string {
6868
return tmp.tmpNameSync(options);
6969
}
7070

71+
export function provenanceBuilderID(): string {
72+
return `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
73+
}
74+
7175
export async function getInputs(defaultContext: string): Promise<Inputs> {
7276
return {
7377
addHosts: await getInputList('add-hosts'),
@@ -88,7 +92,7 @@ export async function getInputs(defaultContext: string): Promise<Inputs> {
8892
noCacheFilters: await getInputList('no-cache-filters'),
8993
outputs: await getInputList('outputs', true),
9094
platforms: await getInputList('platforms'),
91-
provenance: core.getInput('provenance'),
95+
provenance: getProvenanceInput('provenance'),
9296
pull: core.getBooleanInput('pull'),
9397
push: core.getBooleanInput('push'),
9498
sbom: core.getInput('sbom'),
@@ -162,22 +166,19 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, context: str
162166
args.push('--platform', inputs.platforms.join(','));
163167
}
164168
if (buildx.satisfies(buildxVersion, '>=0.10.0')) {
165-
const prvBuilderID = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
166169
if (inputs.provenance) {
167-
args.push('--provenance', getProvenanceAttrs(inputs.provenance, prvBuilderID));
170+
args.push('--provenance', inputs.provenance);
168171
} else if ((await buildx.satisfiesBuildKitVersion(inputs.builder, '>=0.11.0', standalone)) && !hasDockerExport(inputs)) {
169172
// if provenance not specified and BuildKit version compatible for
170173
// attestation, set default provenance. Also needs to make sure user
171174
// doesn't want to explicitly load the image to docker.
172175
if (fromPayload('repository.private') !== false) {
173176
// if this is a private repository, we set the default provenance
174177
// attributes being set in buildx: https://github.com/docker/buildx/blob/fb27e3f919dcbf614d7126b10c2bc2d0b1927eb6/build/build.go#L603
175-
// along the builder-id attribute.
176-
args.push('--provenance', `mode=min,inline-only=true,builder-id=${prvBuilderID}`);
178+
args.push('--provenance', getProvenanceAttrs(`mode=min,inline-only=true`));
177179
} else {
178-
// for a public repository, we set max provenance mode and the
179-
// builder-id attribute.
180-
args.push('--provenance', `mode=max,builder-id=${prvBuilderID}`);
180+
// for a public repository, we set max provenance mode.
181+
args.push('--provenance', getProvenanceAttrs(`mode=max`));
181182
}
182183
}
183184
if (inputs.sbom) {
@@ -298,7 +299,24 @@ function select(obj: any, path: string): any {
298299
return select(obj[key], path.slice(i + 1));
299300
}
300301

301-
function getProvenanceAttrs(input: string, builderID: string): string {
302+
function getProvenanceInput(name: string): string {
303+
const input = core.getInput(name);
304+
if (!input) {
305+
// if input is not set, default values will be set later.
306+
return input;
307+
}
308+
const builderID = provenanceBuilderID();
309+
try {
310+
return core.getBooleanInput(name) ? `builder-id=${builderID}` : 'false';
311+
} catch (err) {
312+
// not a valid boolean, so we assume it's a string
313+
return getProvenanceAttrs(input);
314+
}
315+
}
316+
317+
function getProvenanceAttrs(input: string): string {
318+
const builderID = provenanceBuilderID();
319+
// parse attributes from input
302320
const fields = parse(input, {
303321
relaxColumnCount: true,
304322
skipEmptyLines: true

0 commit comments

Comments
 (0)