custom annotations support

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-11-30 15:03:24 +01:00
parent f19c3691d5
commit cb0becceaf
9 changed files with 299 additions and 149 deletions

View File

@ -10,8 +10,10 @@ export interface Inputs {
tags: string[];
flavor: string[];
labels: string[];
annotations: string[];
sepTags: string;
sepLabels: string;
sepAnnotations: string;
bakeTarget: string;
githubToken: string;
}
@ -23,8 +25,10 @@ export function getInputs(): Inputs {
tags: Util.getInputList('tags', {ignoreComma: true, comment: '#'}),
flavor: Util.getInputList('flavor', {ignoreComma: true, comment: '#'}),
labels: Util.getInputList('labels', {ignoreComma: true, comment: '#'}),
annotations: Util.getInputList('annotations', {ignoreComma: true, comment: '#'}),
sepTags: core.getInput('sep-tags', {trimWhitespace: false}) || `\n`,
sepLabels: core.getInput('sep-labels', {trimWhitespace: false}) || `\n`,
sepAnnotations: core.getInput('sep-annotations', {trimWhitespace: false}) || `\n`,
bakeTarget: core.getInput('bake-target') || `docker-metadata-action`,
githubToken: core.getInput('github-token')
};

View File

@ -71,32 +71,31 @@ actionsToolkit.run(
});
// 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));
});
}
const annotationsRaw: Array<string> = meta.getAnnotations();
const annotationsLevels = process.env.DOCKER_METADATA_ANNOTATIONS_LEVELS || 'manifest';
await core.group(`Annotations`, async () => {
const annotations: Array<string> = [];
for (const level of annotationsLevels.split(',')) {
annotations.push(
...annotationsRaw.map(label => {
const v = `${level}:${label}`;
core.info(v);
return v;
})
);
}
setOutput(`annotations`, annotations.join(inputs.sepAnnotations));
});
// JSON
const jsonOutput = meta.getJSON(alevels.split(','));
const jsonOutput = meta.getJSON(annotationsLevels.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', 'annotations:' + alevels]) {
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
const outputName = kind.split(':')[0];
const bakeFile: string = meta.getBakeFile(kind);
await core.group(`Bake file definition (${outputName})`, async () => {

View File

@ -468,7 +468,15 @@ export class Meta {
}
public getLabels(): Array<string> {
const labels: Array<string> = [
return this.getOCIAnnotationsWithCustoms(this.inputs.labels);
}
public getAnnotations(): Array<string> {
return this.getOCIAnnotationsWithCustoms(this.inputs.annotations);
}
private getOCIAnnotationsWithCustoms(extra: string[]): Array<string> {
const res: Array<string> = [
`org.opencontainers.image.title=${this.repo.name || ''}`,
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
@ -478,11 +486,11 @@ export class Meta {
`org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`
];
labels.push(...this.inputs.labels);
res.push(...extra);
return Array.from(
new Map<string, string>(
labels
res
.map(label => label.split('='))
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.filter(([_key, ...values]) => values.length > 0)
@ -496,7 +504,7 @@ export class Meta {
public getJSON(alevels: string[]): unknown {
const annotations: Array<string> = [];
for (const level of alevels) {
annotations.push(...this.getLabels().map(label => `${level}:${label}`));
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
}
return {
tags: this.getTags(),
@ -536,7 +544,7 @@ export class Meta {
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}`));
annotations.push(...this.getAnnotations().map(label => `${level}:${label}`));
}
return this.generateBakeFile(name, {
annotations: annotations