annotations support

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-11-27 11:39:37 +01:00
parent 2a4836ac76
commit f0ad8701de
6 changed files with 444 additions and 47 deletions

View File

@ -74,19 +74,38 @@ actionsToolkit.run(
setOutput('labels', labels.join(inputs.sepLabels));
});
// Annotations
const alevels = process.env.DOCKER_METADATA_ANNOTATIONS_LEVELS || 'manifest';
if (labels.length > 0) {
await core.group(`Annotations`, async () => {
const annotations: Array<string> = [];
for (const level of alevels.split(',')) {
annotations.push(
...labels.map(label => {
const v = `${level}:${label}`;
core.info(v);
return v;
})
);
}
setOutput(`annotations`, annotations.join(inputs.sepLabels));
});
}
// JSON
const jsonOutput = meta.getJSON();
const jsonOutput = meta.getJSON(alevels.split(','));
await core.group(`JSON output`, async () => {
core.info(JSON.stringify(jsonOutput, null, 2));
setOutput('json', JSON.stringify(jsonOutput));
});
// Bake files
for (const kind of ['tags', 'labels']) {
for (const kind of ['tags', 'labels', 'annotations:' + alevels]) {
const outputName = kind.split(':')[0];
const bakeFile: string = meta.getBakeFile(kind);
await core.group(`Bake file definition (${kind})`, async () => {
await core.group(`Bake file definition (${outputName})`, async () => {
core.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput(`bake-file-${kind}`, bakeFile);
setOutput(`bake-file-${outputName}`, bakeFile);
});
}

View File

@ -480,7 +480,11 @@ export class Meta {
.map(([key, value]) => `${key}=${value}`);
}
public getJSON(): unknown {
public getJSON(alevels: string[]): unknown {
const annotations: Array<string> = [];
for (const level of alevels) {
annotations.push(...this.getLabels().map(label => `${level}:${label}`));
}
return {
tags: this.getTags(),
labels: this.getLabels().reduce((res, label) => {
@ -490,34 +494,42 @@ export class Meta {
}
res[matches[1]] = matches[2];
return res;
}, {})
}, {}),
annotations: annotations
};
}
public getBakeFile(kind: string): string {
switch (kind) {
case 'tags':
return this.generateBakeFile(kind, {
tags: this.getTags(),
args: {
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
});
case 'labels':
return this.generateBakeFile(kind, {
labels: this.getLabels().reduce((res, label) => {
const matches = label.match(/([^=]*)=(.*)/);
if (!matches) {
return res;
}
res[matches[1]] = matches[2];
if (kind == 'tags') {
return this.generateBakeFile(kind, {
tags: this.getTags(),
args: {
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;
}, {})
});
default:
throw new Error(`Unknown bake file type: ${kind}`);
}
res[matches[1]] = matches[2];
return res;
}, {})
});
} else if (kind.startsWith('annotations:')) {
const name = kind.split(':')[0];
const annotations: Array<string> = [];
for (const level of kind.split(':')[1].split(',')) {
annotations.push(...this.getLabels().map(label => `${level}:${label}`));
}
return this.generateBakeFile(name, {
annotations: annotations
});
}
throw new Error(`Unknown bake file type: ${kind}`);
}
public getBakeFileTagsLabels(): string {