metadata-action/src/main.ts

81 lines
2.4 KiB
TypeScript
Raw Normal View History

import * as fs from 'fs';
import {getInputs, Inputs, setOutput} from './context';
2020-10-25 09:25:23 +08:00
import * as github from './github';
import {Meta, Version} from './meta';
2020-10-25 09:25:23 +08:00
import * as core from '@actions/core';
import {Context} from '@actions/github/lib/context';
async function run() {
try {
const inputs: Inputs = await getInputs();
if (inputs.images.length == 0) {
throw new Error(`images input required`);
}
const context: Context = github.context();
const repo: github.ReposGetResponseData = await github.repo(inputs.githubToken);
2020-10-25 09:25:23 +08:00
core.startGroup(`Context info`);
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}`);
core.endGroup();
const meta: Meta = new Meta(inputs, context, repo);
const version: Version = meta.version;
if (meta.version.main == undefined || meta.version.main.length == 0) {
core.warning(`No Docker image version has been generated. Check tags input.`);
} else {
core.startGroup(`Docker image version`);
core.info(version.main || '');
core.endGroup();
}
setOutput('version', version.main || '');
2020-10-25 09:40:42 +08:00
// Docker tags
const tags: Array<string> = meta.getTags();
if (tags.length == 0) {
core.warning('No Docker tag has been generated. Check tags input.');
} else {
core.startGroup(`Docker tags`);
for (const tag of tags) {
core.info(tag);
}
core.endGroup();
2020-10-25 10:21:46 +08:00
}
setOutput('tags', tags.join(inputs.sepTags));
2020-10-25 09:25:23 +08:00
// Docker labels
const labels: Array<string> = meta.getLabels();
2020-10-25 09:40:42 +08:00
core.startGroup(`Docker labels`);
for (const label of labels) {
2020-10-25 10:21:46 +08:00
core.info(label);
}
2020-10-25 09:25:23 +08:00
core.endGroup();
setOutput('labels', labels.join(inputs.sepLabels));
// JSON
const jsonOutput = meta.getJSON();
core.startGroup(`JSON output`);
core.info(JSON.stringify(jsonOutput, null, 2));
core.endGroup();
setOutput('json', jsonOutput);
// Bake definition file
const bakeFile: string = meta.getBakeFile();
core.startGroup(`Bake definition file`);
core.info(fs.readFileSync(bakeFile, 'utf8'));
core.endGroup();
setOutput('bake-file', bakeFile);
2020-10-25 09:25:23 +08:00
} catch (error) {
core.setFailed(error.message);
}
}
run();