Skip to content

Commit 41e1fe3

Browse files
authored
Merge pull request #370 from crazy-max/bake-cwd
bake: set cwd:// prefix for bake files path
2 parents 9dc751f + 48cce1c commit 41e1fe3

File tree

5 files changed

+81
-54
lines changed

5 files changed

+81
-54
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ jobs:
424424
name: Set up Docker Buildx
425425
uses: docker/setup-buildx-action@v3
426426
with:
427-
version: v0.12.0-rc1
427+
version: latest
428428
-
429429
name: Build
430430
uses: docker/bake-action@v4
@@ -455,3 +455,29 @@ jobs:
455455
-
456456
name: Print envs
457457
run: env|sort
458+
459+
bake-cwd:
460+
runs-on: ubuntu-latest
461+
steps:
462+
-
463+
name: Checkout
464+
uses: actions/checkout@v4
465+
-
466+
name: Set up Docker Buildx
467+
uses: docker/setup-buildx-action@v3
468+
with:
469+
version: latest
470+
-
471+
name: Docker meta
472+
id: docker_meta
473+
uses: ./
474+
-
475+
name: Build
476+
uses: docker/bake-action@v4
477+
with:
478+
files: |
479+
./test/docker-bake.hcl
480+
${{ steps.docker_meta.outputs.bake-file-tags }}
481+
${{ steps.docker_meta.outputs.bake-file-labels }}
482+
targets: |
483+
release

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,22 @@ actionsToolkit.run(
9494
setOutput('json', JSON.stringify(jsonOutput));
9595
});
9696

97+
// Specifying local and remote bake files is supported since Buildx 0.12.0.
98+
// Set cwd:// prefix for local bake files to avoid ambiguity with remote
99+
// https://github.com/docker/buildx/pull/1838
100+
const bakeFileCwdPrefix = (await toolkit.buildx.versionSatisfies('>=0.12.0').catch(() => false)) ? 'cwd://' : '';
101+
97102
// Bake files
98103
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
99104
const outputName = kind.split(':')[0];
100105
const bakeFile: string = meta.getBakeFile(kind);
101106
await core.group(`Bake file definition (${outputName})`, async () => {
102107
core.info(fs.readFileSync(bakeFile, 'utf8'));
103-
setOutput(`bake-file-${outputName}`, bakeFile);
108+
setOutput(`bake-file-${outputName}`, `${bakeFileCwdPrefix}${bakeFile}`);
104109
});
105110
}
106111

107112
// Bake file with tags and labels
108-
setOutput(`bake-file`, meta.getBakeFileTagsLabels());
113+
setOutput(`bake-file`, `${bakeFileCwdPrefix}${meta.getBakeFileTagsLabels()}`);
109114
}
110115
);

src/meta.ts

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -522,70 +522,66 @@ export class Meta {
522522

523523
public getBakeFile(kind: string): string {
524524
if (kind == 'tags') {
525-
return this.generateBakeFile(kind, {
526-
tags: this.getTags(),
527-
args: {
528-
DOCKER_META_IMAGES: this.getImageNames().join(','),
529-
DOCKER_META_VERSION: this.version.main
530-
}
531-
});
525+
return this.generateBakeFile(
526+
{
527+
tags: this.getTags(),
528+
args: {
529+
DOCKER_META_IMAGES: this.getImageNames().join(','),
530+
DOCKER_META_VERSION: this.version.main
531+
}
532+
},
533+
kind
534+
);
532535
} else if (kind == 'labels') {
533-
return this.generateBakeFile(kind, {
534-
labels: this.getLabels().reduce((res, label) => {
535-
const matches = label.match(/([^=]*)=(.*)/);
536-
if (!matches) {
536+
return this.generateBakeFile(
537+
{
538+
labels: this.getLabels().reduce((res, label) => {
539+
const matches = label.match(/([^=]*)=(.*)/);
540+
if (!matches) {
541+
return res;
542+
}
543+
res[matches[1]] = matches[2];
537544
return res;
538-
}
539-
res[matches[1]] = matches[2];
540-
return res;
541-
}, {})
542-
});
545+
}, {})
546+
},
547+
kind
548+
);
543549
} else if (kind.startsWith('annotations:')) {
544550
const name = kind.split(':')[0];
545551
const annotations: Array<string> = [];
546552
for (const level of kind.split(':')[1].split(',')) {
547553
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
548554
}
549-
return this.generateBakeFile(name, {
550-
annotations: annotations
551-
});
555+
return this.generateBakeFile(
556+
{
557+
annotations: annotations
558+
},
559+
name
560+
);
552561
}
553562
throw new Error(`Unknown bake file type: ${kind}`);
554563
}
555564

556565
public getBakeFileTagsLabels(): string {
557-
const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json');
558-
fs.writeFileSync(
559-
bakeFile,
560-
JSON.stringify(
561-
{
562-
target: {
563-
[this.inputs.bakeTarget]: {
564-
tags: this.getTags(),
565-
labels: this.getLabels().reduce((res, label) => {
566-
const matches = label.match(/([^=]*)=(.*)/);
567-
if (!matches) {
568-
return res;
569-
}
570-
res[matches[1]] = matches[2];
571-
return res;
572-
}, {}),
573-
args: {
574-
DOCKER_META_IMAGES: this.getImageNames().join(','),
575-
DOCKER_META_VERSION: this.version.main
576-
}
577-
}
578-
}
579-
},
580-
null,
581-
2
582-
)
583-
);
584-
return bakeFile;
566+
return this.generateBakeFile({
567+
tags: this.getTags(),
568+
labels: this.getLabels().reduce((res, label) => {
569+
const matches = label.match(/([^=]*)=(.*)/);
570+
if (!matches) {
571+
return res;
572+
}
573+
res[matches[1]] = matches[2];
574+
return res;
575+
}, {}),
576+
args: {
577+
DOCKER_META_IMAGES: this.getImageNames().join(','),
578+
DOCKER_META_VERSION: this.version.main
579+
}
580+
});
585581
}
586582

587-
private generateBakeFile(name: string, dt): string {
588-
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake-${name}.json`);
583+
private generateBakeFile(dt, suffix?: string): string {
584+
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake${suffix ? `-${suffix}` : ''}.json`);
589585
fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2));
590586
return bakeFile;
591587
}

0 commit comments

Comments
 (0)