bake: refactor generateBakeFile

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-12-20 14:48:27 +01:00
parent 74fa878183
commit 36ea4ee555
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
1 changed files with 46 additions and 50 deletions

View File

@ -522,70 +522,66 @@ export class Meta {
public getBakeFile(kind: string): string { public getBakeFile(kind: string): string {
if (kind == 'tags') { if (kind == 'tags') {
return this.generateBakeFile(kind, { return this.generateBakeFile(
tags: this.getTags(), {
args: { tags: this.getTags(),
DOCKER_META_IMAGES: this.getImageNames().join(','), args: {
DOCKER_META_VERSION: this.version.main DOCKER_META_IMAGES: this.getImageNames().join(','),
} DOCKER_META_VERSION: this.version.main
});
} else if (kind == 'labels') {
return this.generateBakeFile(kind, {
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
} }
res[matches[1]] = matches[2]; },
return res; kind
}, {}) );
}); } else if (kind == 'labels') {
return this.generateBakeFile(
{
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
}
res[matches[1]] = matches[2];
return res;
}, {})
},
kind
);
} else if (kind.startsWith('annotations:')) { } else if (kind.startsWith('annotations:')) {
const name = kind.split(':')[0]; const name = kind.split(':')[0];
const annotations: Array<string> = []; const annotations: Array<string> = [];
for (const level of kind.split(':')[1].split(',')) { for (const level of kind.split(':')[1].split(',')) {
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`)); annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
} }
return this.generateBakeFile(name, { return this.generateBakeFile(
annotations: annotations {
}); annotations: annotations
},
name
);
} }
throw new Error(`Unknown bake file type: ${kind}`); throw new Error(`Unknown bake file type: ${kind}`);
} }
public getBakeFileTagsLabels(): string { public getBakeFileTagsLabels(): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json'); return this.generateBakeFile({
fs.writeFileSync( tags: this.getTags(),
bakeFile, labels: this.getLabels().reduce((res, label) => {
JSON.stringify( const matches = label.match(/([^=]*)=(.*)/);
{ if (!matches) {
target: { return res;
[this.inputs.bakeTarget]: { }
tags: this.getTags(), res[matches[1]] = matches[2];
labels: this.getLabels().reduce((res, label) => { return res;
const matches = label.match(/([^=]*)=(.*)/); }, {}),
if (!matches) { args: {
return res; DOCKER_META_IMAGES: this.getImageNames().join(','),
} DOCKER_META_VERSION: this.version.main
res[matches[1]] = matches[2]; }
return res; });
}, {}),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
}
}
},
null,
2
)
);
return bakeFile;
} }
private generateBakeFile(name: string, dt): string { private generateBakeFile(dt, suffix?: string): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake-${name}.json`); const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake${suffix ? `-${suffix}` : ''}.json`);
fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2)); fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2));
return bakeFile; return bakeFile;
} }