1
0
Files
metadata-action/src/main.ts
T

112 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-12-24 04:13:41 +01:00
import * as fs from 'fs';
2020-10-25 02:25:23 +01:00
import * as core from '@actions/core';
2023-02-20 22:32:55 +01:00
import * as actionsToolkit from '@docker/actions-toolkit';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {getContext, getInputs, Inputs} from './context';
2023-02-20 22:32:55 +01:00
import {Meta, Version} from './meta';
2020-10-25 02:25:23 +01:00
2023-02-20 22:32:55 +01:00
actionsToolkit.run(
// main
async () => {
const inputs: Inputs = getInputs();
2023-02-20 22:32:55 +01:00
const toolkit = new Toolkit({githubToken: inputs.githubToken});
2024-11-13 23:39:24 +08:00
const context = await getContext(inputs.context, toolkit);
2023-02-20 22:32:55 +01:00
const repo = await toolkit.github.repoData();
await core.group(`Context info`, async () => {
core.info(`eventName: ${context.eventName}`);
core.info(`sha: ${context.sha}`);
core.info(`ref: ${context.ref}`);
core.info(`workflow: ${context.workflow}`);
core.info(`action: ${context.action}`);
core.info(`actor: ${context.actor}`);
core.info(`runNumber: ${context.runNumber}`);
core.info(`runId: ${context.runId}`);
2024-11-13 23:39:24 +08:00
core.info(`commitDate: ${context.commitDate}`);
2023-02-20 22:32:55 +01:00
});
2020-10-25 02:25:23 +01:00
if (core.isDebug()) {
2023-02-20 22:32:55 +01:00
await core.group(`Webhook payload`, async () => {
core.info(JSON.stringify(context.payload, null, 2));
});
}
2020-10-25 02:25:23 +01:00
const meta: Meta = new Meta(inputs, context, repo);
2020-12-01 05:38:08 +01:00
const version: Version = meta.version;
2021-03-29 13:04:53 +02:00
if (meta.version.main == undefined || meta.version.main.length == 0) {
core.warning(`No Docker image version has been generated. Check tags input.`);
} else {
2023-02-20 22:32:55 +01:00
await core.group(`Docker image version`, async () => {
core.info(version.main || '');
});
2021-03-29 13:04:53 +02:00
}
2023-01-13 10:45:14 +01:00
setOutput('version', version.main || '');
2020-10-25 02:40:42 +01:00
2020-12-24 04:13:41 +01:00
// Docker tags
2021-03-29 13:04:53 +02:00
const tags: Array<string> = meta.getTags();
if (tags.length == 0) {
core.warning('No Docker tag has been generated. Check tags input.');
} else {
2023-02-20 22:32:55 +01:00
await core.group(`Docker tags`, async () => {
for (const tag of tags) {
core.info(tag);
}
});
2020-10-25 03:21:46 +01:00
}
2023-01-13 10:45:14 +01:00
setOutput('tags', tags.join(inputs.sepTags));
2020-10-25 02:25:23 +01:00
2020-12-24 04:13:41 +01:00
// Docker labels
2021-03-29 13:04:53 +02:00
const labels: Array<string> = meta.getLabels();
2023-02-20 22:32:55 +01:00
await core.group(`Docker labels`, async () => {
for (const label of labels) {
core.info(label);
}
setOutput('labels', labels.join(inputs.sepLabels));
});
2020-12-24 04:13:41 +01:00
2023-11-27 11:39:37 +01:00
// Annotations
2023-11-30 15:03:24 +01:00
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));
});
2023-11-27 11:39:37 +01:00
2021-05-22 21:23:06 +02:00
// JSON
2023-11-30 15:03:24 +01:00
const jsonOutput = meta.getJSON(annotationsLevels.split(','));
2023-02-20 22:32:55 +01:00
await core.group(`JSON output`, async () => {
core.info(JSON.stringify(jsonOutput, null, 2));
setOutput('json', JSON.stringify(jsonOutput));
});
2021-05-22 21:23:06 +02:00
2023-11-22 22:46:29 +01:00
// Bake files
2023-11-30 15:03:24 +01:00
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
2023-11-27 11:39:37 +01:00
const outputName = kind.split(':')[0];
2023-11-22 22:46:29 +01:00
const bakeFile: string = meta.getBakeFile(kind);
2023-11-27 11:39:37 +01:00
await core.group(`Bake file definition (${outputName})`, async () => {
2023-11-22 22:46:29 +01:00
core.info(fs.readFileSync(bakeFile, 'utf8'));
setOutput(`bake-file-${outputName}`, bakeFile);
2023-11-22 22:46:29 +01:00
});
}
// Bake file with tags and labels
setOutput(`bake-file`, `${meta.getBakeFileTagsLabels()}`);
2020-10-25 02:25:23 +01:00
}
2023-02-20 22:32:55 +01:00
);
2024-07-23 10:44:25 +02:00
function setOutput(name: string, value: string) {
core.setOutput(name, value);
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
}