2020-12-24 03:13:41 +00:00
|
|
|
import * as fs from 'fs';
|
2020-10-25 01:25:23 +00:00
|
|
|
import * as core from '@actions/core';
|
2023-02-20 21:32:55 +00:00
|
|
|
import * as actionsToolkit from '@docker/actions-toolkit';
|
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
2025-02-26 13:26:59 +00:00
|
|
|
import {Util} from '@docker/actions-toolkit/lib/util';
|
2023-02-20 21:32:55 +00:00
|
|
|
|
2022-12-15 13:46:34 +00:00
|
|
|
import {getContext, getInputs, Inputs} from './context';
|
2023-02-20 21:32:55 +00:00
|
|
|
import {Meta, Version} from './meta';
|
2020-10-25 01:25:23 +00:00
|
|
|
|
2023-02-20 21:32:55 +00:00
|
|
|
actionsToolkit.run(
|
|
|
|
// main
|
|
|
|
async () => {
|
2022-12-15 13:46:34 +00:00
|
|
|
const inputs: Inputs = getInputs();
|
2023-02-20 21:32:55 +00:00
|
|
|
const toolkit = new Toolkit({githubToken: inputs.githubToken});
|
2024-11-13 15:39:24 +00:00
|
|
|
const context = await getContext(inputs.context, toolkit);
|
2023-02-20 21:32:55 +00:00
|
|
|
const repo = await toolkit.github.repoData();
|
2025-02-26 13:26:59 +00:00
|
|
|
const setOutput = outputEnvEnabled() ? setOutputAndEnv : core.setOutput;
|
2023-02-20 21:32:55 +00:00
|
|
|
|
|
|
|
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 15:39:24 +00:00
|
|
|
core.info(`commitDate: ${context.commitDate}`);
|
2023-02-20 21:32:55 +00:00
|
|
|
});
|
2020-10-25 01:25:23 +00:00
|
|
|
|
2022-04-25 11:42:22 +00:00
|
|
|
if (core.isDebug()) {
|
2023-02-20 21:32:55 +00:00
|
|
|
await core.group(`Webhook payload`, async () => {
|
|
|
|
core.info(JSON.stringify(context.payload, null, 2));
|
|
|
|
});
|
2022-04-25 11:42:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 01:25:23 +00:00
|
|
|
const meta: Meta = new Meta(inputs, context, repo);
|
|
|
|
|
2020-12-01 04:38:08 +00:00
|
|
|
const version: Version = meta.version;
|
2021-03-29 11:04:53 +00: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 21:32:55 +00:00
|
|
|
await core.group(`Docker image version`, async () => {
|
|
|
|
core.info(version.main || '');
|
|
|
|
});
|
2021-03-29 11:04:53 +00:00
|
|
|
}
|
2023-01-13 09:45:14 +00:00
|
|
|
setOutput('version', version.main || '');
|
2020-10-25 01:40:42 +00:00
|
|
|
|
2020-12-24 03:13:41 +00:00
|
|
|
// Docker tags
|
2021-03-29 11:04:53 +00: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 21:32:55 +00:00
|
|
|
await core.group(`Docker tags`, async () => {
|
|
|
|
for (const tag of tags) {
|
|
|
|
core.info(tag);
|
|
|
|
}
|
|
|
|
});
|
2020-10-25 02:21:46 +00:00
|
|
|
}
|
2023-01-13 09:45:14 +00:00
|
|
|
setOutput('tags', tags.join(inputs.sepTags));
|
2020-10-25 01:25:23 +00:00
|
|
|
|
2020-12-24 03:13:41 +00:00
|
|
|
// Docker labels
|
2021-03-29 11:04:53 +00:00
|
|
|
const labels: Array<string> = meta.getLabels();
|
2023-02-20 21:32:55 +00:00
|
|
|
await core.group(`Docker labels`, async () => {
|
|
|
|
for (const label of labels) {
|
|
|
|
core.info(label);
|
|
|
|
}
|
|
|
|
setOutput('labels', labels.join(inputs.sepLabels));
|
|
|
|
});
|
2020-12-24 03:13:41 +00:00
|
|
|
|
2023-11-27 10:39:37 +00:00
|
|
|
// Annotations
|
2023-11-30 14:03:24 +00: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 10:39:37 +00:00
|
|
|
|
2021-05-22 19:23:06 +00:00
|
|
|
// JSON
|
2023-11-30 14:03:24 +00:00
|
|
|
const jsonOutput = meta.getJSON(annotationsLevels.split(','));
|
2023-02-20 21:32:55 +00:00
|
|
|
await core.group(`JSON output`, async () => {
|
|
|
|
core.info(JSON.stringify(jsonOutput, null, 2));
|
|
|
|
setOutput('json', JSON.stringify(jsonOutput));
|
|
|
|
});
|
2021-05-22 19:23:06 +00:00
|
|
|
|
2023-11-22 21:46:29 +00:00
|
|
|
// Bake files
|
2023-11-30 14:03:24 +00:00
|
|
|
for (const kind of ['tags', 'labels', 'annotations:' + annotationsLevels]) {
|
2023-11-27 10:39:37 +00:00
|
|
|
const outputName = kind.split(':')[0];
|
2023-11-22 21:46:29 +00:00
|
|
|
const bakeFile: string = meta.getBakeFile(kind);
|
2023-11-27 10:39:37 +00:00
|
|
|
await core.group(`Bake file definition (${outputName})`, async () => {
|
2023-11-22 21:46:29 +00:00
|
|
|
core.info(fs.readFileSync(bakeFile, 'utf8'));
|
2024-01-31 12:48:32 +00:00
|
|
|
setOutput(`bake-file-${outputName}`, bakeFile);
|
2023-11-22 21:46:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bake file with tags and labels
|
2024-01-31 12:48:32 +00:00
|
|
|
setOutput(`bake-file`, `${meta.getBakeFileTagsLabels()}`);
|
2020-10-25 01:25:23 +00:00
|
|
|
}
|
2023-02-20 21:32:55 +00:00
|
|
|
);
|
2024-07-23 08:44:25 +00:00
|
|
|
|
2025-01-15 16:35:27 +00:00
|
|
|
function setOutputAndEnv(name: string, value: string) {
|
2024-07-23 08:44:25 +00:00
|
|
|
core.setOutput(name, value);
|
|
|
|
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
|
|
|
|
}
|
2025-02-26 13:26:59 +00:00
|
|
|
|
|
|
|
function outputEnvEnabled(): boolean {
|
|
|
|
if (process.env.DOCKER_METADATA_SET_OUTPUT_ENV) {
|
|
|
|
return Util.parseBool(process.env.DOCKER_METADATA_SET_OUTPUT_ENV);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|