Merge pull request #95 from crazy-max/fix-bake-def-parse

bake: missing overrides when parsing definition
This commit is contained in:
CrazyMax 2023-04-18 16:52:12 +02:00 committed by GitHub
commit 3ebf172e37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 4 deletions

View File

@ -33,17 +33,25 @@ describe('parseDefinitions', () => {
[
[path.join(fixturesDir, 'bake-01.hcl')],
['validate'],
[],
path.join(fixturesDir, 'bake-01-validate.json')
],
[
[path.join(fixturesDir, 'bake-02.hcl')],
['build'],
[],
path.join(fixturesDir, 'bake-02-build.json')
],
[
[path.join(fixturesDir, 'bake-01.hcl')],
['image'],
['*.output=type=docker', '*.platform=linux/amd64'],
path.join(fixturesDir, 'bake-01-overrides.json')
]
])('given %p', async (sources: string[], targets: string[], out: string) => {
])('given %p', async (sources: string[], targets: string[], overrides: string[], out: string) => {
const bake = new Bake();
const expectedDef = <BakeDefinition>JSON.parse(fs.readFileSync(out, {encoding: 'utf-8'}).trim())
expect(await bake.parseDefinitions(sources, targets)).toEqual(expectedDef);
expect(await bake.parseDefinitions(sources, targets, overrides)).toEqual(expectedDef);
});
});

View File

@ -0,0 +1,29 @@
{
"group": {
"default": {
"targets": [
"image"
]
}
},
"target": {
"image": {
"context": ".",
"dockerfile": "Dockerfile",
"args": {
"BUILDKIT_CONTEXT_KEEP_GIT_DIR": "1",
"GO_VERSION": "1.20"
},
"tags": [
"docker/buildx-bin:local"
],
"target": "binaries",
"platforms": [
"linux/amd64"
],
"output": [
"type=docker"
]
}
}
}

View File

@ -32,7 +32,7 @@ export class Bake {
this.buildx = opts?.buildx || new Buildx();
}
public async parseDefinitions(sources: Array<string>, targets: Array<string>, workdir?: string): Promise<BakeDefinition> {
public async parseDefinitions(sources: Array<string>, targets?: Array<string>, overrides?: Array<string>, load?: boolean, push?: boolean, workdir?: string): Promise<BakeDefinition> {
const args = ['bake'];
let remoteDef;
@ -58,8 +58,19 @@ export class Bake {
for (const file of files) {
args.push('--file', file);
}
if (overrides) {
for (const override of overrides) {
args.push('--set', override);
}
}
if (load) {
args.push('--load');
}
if (push) {
args.push('--push');
}
const printCmd = await this.buildx.getCommand([...args, '--print', ...targets]);
const printCmd = await this.buildx.getCommand([...args, '--print', ...(targets || [])]);
return await Exec.getExecOutput(printCmd.command, printCmd.args, {
cwd: workdir,
ignoreReturnCode: true,