Skip to content

Commit 4ef34a8

Browse files
committed
bake: set cwd:// prefix for bake files path
Signed-off-by: CrazyMax <[email protected]>
1 parent 74fa878 commit 4ef34a8

File tree

2 files changed

+58
-51
lines changed

2 files changed

+58
-51
lines changed

src/main.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ actionsToolkit.run(
3636
});
3737
}
3838

39-
const meta: Meta = new Meta(inputs, context, repo);
39+
const bakeFileCwdPrefix = await toolkit.buildx
40+
.versionSatisfies('>=0.12.0')
41+
.then(ok => {
42+
return ok ? 'cwd://' : '';
43+
})
44+
.catch(() => {
45+
return '';
46+
});
47+
48+
const meta: Meta = new Meta(inputs, context, repo, bakeFileCwdPrefix);
4049

4150
const version: Version = meta.version;
4251
if (meta.version.main == undefined || meta.version.main.length == 0) {

src/meta.ts

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export class Meta {
3030
private readonly tags: tcl.Tag[];
3131
private readonly flavor: fcl.Flavor;
3232
private readonly date: Date;
33+
private readonly bakeFileCwdPrefix: string;
3334

34-
constructor(inputs: Inputs, context: Context, repo: GitHubRepo) {
35+
constructor(inputs: Inputs, context: Context, repo: GitHubRepo, bakeFileCwdPrefix?: string) {
3536
this.inputs = inputs;
3637
this.context = context;
3738
this.repo = repo;
@@ -40,6 +41,7 @@ export class Meta {
4041
this.flavor = fcl.Transform(inputs.flavor);
4142
this.date = new Date();
4243
this.version = this.getVersion();
44+
this.bakeFileCwdPrefix = bakeFileCwdPrefix || '';
4345
}
4446

4547
private getVersion(): Version {
@@ -522,70 +524,66 @@ export class Meta {
522524

523525
public getBakeFile(kind: string): string {
524526
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-
});
527+
return this.generateBakeFile(
528+
{
529+
tags: this.getTags(),
530+
args: {
531+
DOCKER_META_IMAGES: this.getImageNames().join(','),
532+
DOCKER_META_VERSION: this.version.main
533+
}
534+
},
535+
kind
536+
);
532537
} else if (kind == 'labels') {
533-
return this.generateBakeFile(kind, {
534-
labels: this.getLabels().reduce((res, label) => {
535-
const matches = label.match(/([^=]*)=(.*)/);
536-
if (!matches) {
538+
return this.generateBakeFile(
539+
{
540+
labels: this.getLabels().reduce((res, label) => {
541+
const matches = label.match(/([^=]*)=(.*)/);
542+
if (!matches) {
543+
return res;
544+
}
545+
res[matches[1]] = matches[2];
537546
return res;
538-
}
539-
res[matches[1]] = matches[2];
540-
return res;
541-
}, {})
542-
});
547+
}, {})
548+
},
549+
kind
550+
);
543551
} else if (kind.startsWith('annotations:')) {
544552
const name = kind.split(':')[0];
545553
const annotations: Array<string> = [];
546554
for (const level of kind.split(':')[1].split(',')) {
547555
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
548556
}
549-
return this.generateBakeFile(name, {
550-
annotations: annotations
551-
});
557+
return this.generateBakeFile(
558+
{
559+
annotations: annotations
560+
},
561+
name
562+
);
552563
}
553564
throw new Error(`Unknown bake file type: ${kind}`);
554565
}
555566

556567
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;
568+
return this.generateBakeFile({
569+
tags: this.getTags(),
570+
labels: this.getLabels().reduce((res, label) => {
571+
const matches = label.match(/([^=]*)=(.*)/);
572+
if (!matches) {
573+
return res;
574+
}
575+
res[matches[1]] = matches[2];
576+
return res;
577+
}, {}),
578+
args: {
579+
DOCKER_META_IMAGES: this.getImageNames().join(','),
580+
DOCKER_META_VERSION: this.version.main
581+
}
582+
});
585583
}
586584

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

0 commit comments

Comments
 (0)