Skip to content

Commit 7811a01

Browse files
authored
Merge pull request #547 from crazy-max/bake-composable-attests
bake: composable attributes for attestations support
2 parents d78e250 + bfc74cf commit 7811a01

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

__tests__/.fixtures/bake-03-default.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
"default": {
44
"context": ".",
55
"dockerfile": "Dockerfile",
6+
"attest": [
7+
{
8+
"mode": "max",
9+
"type": "provenance"
10+
},
11+
{
12+
"disabled": "true",
13+
"type": "sbom"
14+
}
15+
],
616
"cache-from": [
717
{
818
"scope": "build",

__tests__/.fixtures/bake-03.hcl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
// limitations under the License.
1414

1515
target "default" {
16+
attest = [
17+
"type=provenance,mode=max",
18+
"type=sbom,disabled=true",
19+
]
1620
cache-from = [
1721
"type=gha,scope=build",
1822
"user/repo:cache",

src/buildx/bake.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {Exec} from '../exec';
2424
import {Util} from '../util';
2525

2626
import {ExecOptions} from '@actions/exec';
27-
import {BakeDefinition, CacheEntry, ExportEntry, SecretEntry, SSHEntry} from '../types/buildx/bake';
27+
import {AttestEntry, BakeDefinition, CacheEntry, ExportEntry, SecretEntry, SSHEntry} from '../types/buildx/bake';
2828
import {BuildMetadata} from '../types/buildx/build';
2929
import {VertexWarning} from '../types/buildkit/client';
3030

@@ -183,6 +183,11 @@ export class Bake {
183183
// convert to composable attributes: https://github.com/docker/buildx/pull/2758
184184
for (const name in definition.target) {
185185
const target = definition.target[name];
186+
if (target['attest'] && Array.isArray(target['attest'])) {
187+
target['attest'] = target['attest'].map((item: string | AttestEntry): AttestEntry => {
188+
return Bake.parseAttestEntry(item);
189+
});
190+
}
186191
if (target['cache-from'] && Array.isArray(target['cache-from'])) {
187192
target['cache-from'] = target['cache-from'].map((item: string | CacheEntry): CacheEntry => {
188193
return Bake.parseCacheEntry(item);
@@ -213,6 +218,34 @@ export class Bake {
213218
return definition;
214219
}
215220

221+
private static parseAttestEntry(item: AttestEntry | string): AttestEntry {
222+
if (typeof item !== 'string') {
223+
return item;
224+
}
225+
226+
const attestEntry: AttestEntry = {type: ''};
227+
const fields = parse(item, {
228+
relaxColumnCount: true,
229+
skipEmptyLines: true
230+
})[0];
231+
232+
for (const field of fields) {
233+
const [key, value] = field
234+
.toString()
235+
.split(/(?<=^[^=]+?)=/)
236+
.map((item: string) => item.trim());
237+
switch (key) {
238+
case 'type':
239+
attestEntry.type = value;
240+
break;
241+
default:
242+
attestEntry[key] = value;
243+
}
244+
}
245+
246+
return attestEntry;
247+
}
248+
216249
private static parseCacheEntry(item: CacheEntry | string): CacheEntry {
217250
if (typeof item !== 'string') {
218251
return item;

src/types/buildx/bake.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface Group {
2727
export interface Target {
2828
description?: string;
2929
args?: Record<string, string>;
30-
attest?: Array<string>;
30+
attest?: Array<AttestEntry> | Array<string>;
3131
'cache-from'?: Array<CacheEntry> | Array<string>;
3232
'cache-to'?: Array<CacheEntry> | Array<string>;
3333
call?: string;
@@ -50,6 +50,11 @@ export interface Target {
5050
ulimits?: Array<string>;
5151
}
5252

53+
export interface AttestEntry {
54+
type: string;
55+
[key: string]: string;
56+
}
57+
5358
export interface CacheEntry {
5459
type: string;
5560
[key: string]: string;

0 commit comments

Comments
 (0)