Skip to content

Commit f6cafdf

Browse files
authored
Merge pull request #285 from crazy-max/bake-parse-opts
bake: additional opts when parsing definition
2 parents 264a0ee + 416c291 commit f6cafdf

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

__tests__/buildx/bake.test.itg.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@ beforeEach(() => {
2929
jest.clearAllMocks();
3030
});
3131

32-
maybe('parseDefinitions', () => {
32+
maybe('getDefinition', () => {
3333
// prettier-ignore
3434
test.each([
3535
[
36-
['https://github.com/docker/buildx.git#v0.10.4'],
36+
'https://github.com/docker/buildx.git#v0.10.4',
3737
['binaries-cross'],
3838
path.join(fixturesDir, 'bake-buildx-0.10.4-binaries-cross.json')
39-
]
40-
])('given %p', async (sources: string[], targets: string[], out: string) => {
39+
],
40+
])('given %p', async (source: string, targets: string[], out: string) => {
4141
const bake = new Bake();
4242
const expectedDef = <BakeDefinition>JSON.parse(fs.readFileSync(out, {encoding: 'utf-8'}).trim())
43-
expect(await bake.parseDefinitions(sources, targets)).toEqual(expectedDef);
43+
expect(await bake.getDefinition({
44+
source: source,
45+
targets: targets
46+
})).toEqual(expectedDef);
4447
});
4548
});

__tests__/buildx/bake.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import * as fs from 'fs';
1919
import * as path from 'path';
2020

2121
import {Bake} from '../../src/buildx/bake';
22+
23+
import {ExecOptions} from '@actions/exec';
2224
import {BakeDefinition} from '../../src/types/bake';
2325

2426
const fixturesDir = path.join(__dirname, '..', 'fixtures');
@@ -27,31 +29,38 @@ beforeEach(() => {
2729
jest.clearAllMocks();
2830
});
2931

30-
describe('parseDefinitions', () => {
32+
describe('getDefinition', () => {
3133
// prettier-ignore
3234
test.each([
3335
[
3436
[path.join(fixturesDir, 'bake-01.hcl')],
3537
['validate'],
3638
[],
39+
{silent: true},
3740
path.join(fixturesDir, 'bake-01-validate.json')
3841
],
3942
[
4043
[path.join(fixturesDir, 'bake-02.hcl')],
4144
['build'],
4245
[],
46+
undefined,
4347
path.join(fixturesDir, 'bake-02-build.json')
4448
],
4549
[
4650
[path.join(fixturesDir, 'bake-01.hcl')],
4751
['image'],
4852
['*.output=type=docker', '*.platform=linux/amd64'],
53+
undefined,
4954
path.join(fixturesDir, 'bake-01-overrides.json')
5055
]
51-
])('given %p', async (sources: string[], targets: string[], overrides: string[], out: string) => {
56+
])('given %p', async (files: string[], targets: string[], overrides: string[], execOptions: ExecOptions | undefined, out: string) => {
5257
const bake = new Bake();
5358
const expectedDef = <BakeDefinition>JSON.parse(fs.readFileSync(out, {encoding: 'utf-8'}).trim())
54-
expect(await bake.parseDefinitions(sources, targets, overrides)).toEqual(expectedDef);
59+
expect(await bake.getDefinition({
60+
files: files,
61+
targets: targets,
62+
overrides: overrides
63+
}, execOptions)).toEqual(expectedDef);
5564
});
5665
});
5766

src/buildx/bake.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,43 @@ import {Exec} from '../exec';
1919
import {Inputs} from './inputs';
2020
import {Util} from '../util';
2121

22+
import {ExecOptions} from '@actions/exec';
2223
import {BakeDefinition} from '../types/bake';
2324

2425
export interface BakeOpts {
2526
buildx?: Buildx;
2627
}
2728

29+
export interface BakeCmdOpts {
30+
files?: Array<string>;
31+
load?: boolean;
32+
noCache?: boolean;
33+
overrides?: Array<string>;
34+
provenance?: string;
35+
push?: boolean;
36+
sbom?: string;
37+
source?: string;
38+
targets?: Array<string>;
39+
}
40+
2841
export class Bake {
2942
private readonly buildx: Buildx;
3043

3144
constructor(opts?: BakeOpts) {
3245
this.buildx = opts?.buildx || new Buildx();
3346
}
3447

35-
public async parseDefinitions(sources: Array<string>, targets?: Array<string>, overrides?: Array<string>, load?: boolean, push?: boolean, workdir?: string): Promise<BakeDefinition> {
48+
public async getDefinition(cmdOpts: BakeCmdOpts, execOptions?: ExecOptions): Promise<BakeDefinition> {
49+
execOptions = execOptions || {ignoreReturnCode: true};
50+
execOptions.ignoreReturnCode = true;
51+
3652
const args = ['bake'];
3753

38-
let remoteDef;
54+
let remoteDef: string | undefined;
3955
const files: Array<string> = [];
56+
const sources = [...(cmdOpts.files || []), cmdOpts.source];
4057
if (sources) {
41-
for (const source of sources.map(v => v.trim())) {
58+
for (const source of sources.map(v => (v ? v.trim() : ''))) {
4259
if (source.length == 0) {
4360
continue;
4461
}
@@ -47,7 +64,7 @@ export class Bake {
4764
continue;
4865
}
4966
if (remoteDef) {
50-
throw new Error(`Only one remote bake definition is allowed`);
67+
throw new Error(`Only one remote bake definition can be defined`);
5168
}
5269
remoteDef = source;
5370
}
@@ -58,31 +75,40 @@ export class Bake {
5875
for (const file of files) {
5976
args.push('--file', file);
6077
}
61-
if (overrides) {
62-
for (const override of overrides) {
78+
if (cmdOpts.overrides) {
79+
for (const override of cmdOpts.overrides) {
6380
args.push('--set', override);
6481
}
6582
}
66-
if (load) {
83+
if (cmdOpts.load) {
6784
args.push('--load');
6885
}
69-
if (push) {
86+
if (cmdOpts.noCache) {
87+
args.push('--no-cache');
88+
}
89+
if (cmdOpts.provenance) {
90+
args.push('--provenance', cmdOpts.provenance);
91+
}
92+
if (cmdOpts.push) {
7093
args.push('--push');
7194
}
95+
if (cmdOpts.sbom) {
96+
args.push('--sbom', cmdOpts.sbom);
97+
}
7298

73-
const printCmd = await this.buildx.getCommand([...args, '--print', ...(targets || [])]);
74-
return await Exec.getExecOutput(printCmd.command, printCmd.args, {
75-
cwd: workdir,
76-
ignoreReturnCode: true,
77-
silent: true
78-
}).then(res => {
99+
const printCmd = await this.buildx.getCommand([...args, '--print', ...(cmdOpts.targets || [])]);
100+
return await Exec.getExecOutput(printCmd.command, printCmd.args, execOptions).then(res => {
79101
if (res.stderr.length > 0 && res.exitCode != 0) {
80102
throw new Error(`cannot parse bake definitions: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
81103
}
82-
return <BakeDefinition>JSON.parse(res.stdout.trim());
104+
return Bake.parseDefinition(res.stdout.trim());
83105
});
84106
}
85107

108+
public static parseDefinition(dt: string): BakeDefinition {
109+
return <BakeDefinition>JSON.parse(dt);
110+
}
111+
86112
public static hasLocalExporter(def: BakeDefinition): boolean {
87113
return Inputs.hasExporterType('local', Bake.exporters(def));
88114
}

src/types/bake.ts

+2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export interface Target {
3939
platforms?: Array<string>;
4040
pull?: boolean;
4141
secret?: Array<string>;
42+
'shm-size'?: string;
4243
ssh?: Array<string>;
4344
tags?: Array<string>;
4445
target?: string;
46+
ulimits?: Array<string>;
4547
}

0 commit comments

Comments
 (0)