mirror of
https://github.com/docker/metadata-action.git
synced 2025-07-05 09:48:24 +02:00
switch to actions-toolkit implementation
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@ -1,10 +1,5 @@
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
import {parse} from 'csv-parse/sync';
|
||||
|
||||
let _tmpDir: string;
|
||||
import {Util} from '@docker/actions-toolkit/lib/util';
|
||||
|
||||
export interface Inputs {
|
||||
images: string[];
|
||||
@ -17,58 +12,15 @@ export interface Inputs {
|
||||
githubToken: string;
|
||||
}
|
||||
|
||||
export function tmpDir(): string {
|
||||
if (!_tmpDir) {
|
||||
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-metadata-action-')).split(path.sep).join(path.posix.sep);
|
||||
}
|
||||
return _tmpDir;
|
||||
}
|
||||
|
||||
export function getInputs(): Inputs {
|
||||
return {
|
||||
images: getInputList('images', true),
|
||||
tags: getInputList('tags', true),
|
||||
flavor: getInputList('flavor', true),
|
||||
labels: getInputList('labels', true),
|
||||
images: Util.getInputList('images', {ignoreComma: true}),
|
||||
tags: Util.getInputList('tags', {ignoreComma: true}),
|
||||
flavor: Util.getInputList('flavor', {ignoreComma: true}),
|
||||
labels: Util.getInputList('labels', {ignoreComma: true}),
|
||||
sepTags: core.getInput('sep-tags', {trimWhitespace: false}) || `\n`,
|
||||
sepLabels: core.getInput('sep-labels', {trimWhitespace: false}) || `\n`,
|
||||
bakeTarget: core.getInput('bake-target') || `docker-metadata-action`,
|
||||
githubToken: core.getInput('github-token')
|
||||
};
|
||||
}
|
||||
|
||||
export function getInputList(name: string, ignoreComma?: boolean): string[] {
|
||||
const res: Array<string> = [];
|
||||
|
||||
const items = core.getInput(name);
|
||||
if (items == '') {
|
||||
return res;
|
||||
}
|
||||
|
||||
const records = parse(items, {
|
||||
columns: false,
|
||||
relaxQuotes: true,
|
||||
comment: '#',
|
||||
relaxColumnCount: true,
|
||||
skipEmptyLines: true
|
||||
});
|
||||
|
||||
for (const record of records as Array<string[]>) {
|
||||
if (record.length == 1) {
|
||||
res.push(record[0]);
|
||||
continue;
|
||||
} else if (!ignoreComma) {
|
||||
res.push(...record);
|
||||
continue;
|
||||
}
|
||||
res.push(record.join(','));
|
||||
}
|
||||
|
||||
return res.filter(item => item).map(pat => pat.trim());
|
||||
}
|
||||
|
||||
export const asyncForEach = async (array, callback) => {
|
||||
for (let index = 0; index < array.length; index++) {
|
||||
await callback(array[index], index, array);
|
||||
}
|
||||
};
|
||||
|
@ -1,16 +0,0 @@
|
||||
import * as github from '@actions/github';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {components as OctoOpenApiTypes} from '@octokit/openapi-types';
|
||||
|
||||
export type ReposGetResponseData = OctoOpenApiTypes['schemas']['repository'];
|
||||
|
||||
export function context(): Context {
|
||||
return github.context;
|
||||
}
|
||||
|
||||
export async function repo(token: string): Promise<ReposGetResponseData> {
|
||||
return github
|
||||
.getOctokit(token)
|
||||
.rest.repos.get({...github.context.repo})
|
||||
.then(response => response.data as ReposGetResponseData);
|
||||
}
|
106
src/main.ts
106
src/main.ts
@ -1,34 +1,45 @@
|
||||
import * as fs from 'fs';
|
||||
import {getInputs, Inputs} from './context';
|
||||
import * as github from './github';
|
||||
import {Meta, Version} from './meta';
|
||||
import * as core from '@actions/core';
|
||||
import * as actionsToolkit from '@docker/actions-toolkit';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
||||
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
import {getInputs, Inputs} from './context';
|
||||
import {Meta, Version} from './meta';
|
||||
|
||||
function setOutput(name: string, value: string) {
|
||||
core.setOutput(name, value);
|
||||
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
|
||||
}
|
||||
|
||||
actionsToolkit.run(
|
||||
// main
|
||||
async () => {
|
||||
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);
|
||||
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 toolkit = new Toolkit({githubToken: inputs.githubToken});
|
||||
const context: Context = GitHub.context;
|
||||
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}`);
|
||||
});
|
||||
|
||||
if (core.isDebug()) {
|
||||
core.startGroup(`Webhook payload`);
|
||||
core.info(JSON.stringify(context.payload, null, 2));
|
||||
core.endGroup();
|
||||
await core.group(`Webhook payload`, async () => {
|
||||
core.info(JSON.stringify(context.payload, null, 2));
|
||||
});
|
||||
}
|
||||
|
||||
const meta: Meta = new Meta(inputs, context, repo);
|
||||
@ -37,9 +48,9 @@ async function run() {
|
||||
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();
|
||||
await core.group(`Docker image version`, async () => {
|
||||
core.info(version.main || '');
|
||||
});
|
||||
}
|
||||
setOutput('version', version.main || '');
|
||||
|
||||
@ -48,44 +59,35 @@ async function run() {
|
||||
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();
|
||||
await core.group(`Docker tags`, async () => {
|
||||
for (const tag of tags) {
|
||||
core.info(tag);
|
||||
}
|
||||
});
|
||||
}
|
||||
setOutput('tags', tags.join(inputs.sepTags));
|
||||
|
||||
// Docker labels
|
||||
const labels: Array<string> = meta.getLabels();
|
||||
core.startGroup(`Docker labels`);
|
||||
for (const label of labels) {
|
||||
core.info(label);
|
||||
}
|
||||
core.endGroup();
|
||||
setOutput('labels', labels.join(inputs.sepLabels));
|
||||
await core.group(`Docker labels`, async () => {
|
||||
for (const label of labels) {
|
||||
core.info(label);
|
||||
}
|
||||
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', JSON.stringify(jsonOutput));
|
||||
await core.group(`JSON output`, async () => {
|
||||
core.info(JSON.stringify(jsonOutput, null, 2));
|
||||
setOutput('json', JSON.stringify(jsonOutput));
|
||||
});
|
||||
|
||||
// Bake file definition
|
||||
const bakeFile: string = meta.getBakeFile();
|
||||
core.startGroup(`Bake file definition`);
|
||||
core.info(fs.readFileSync(bakeFile, 'utf8'));
|
||||
core.endGroup();
|
||||
setOutput('bake-file', bakeFile);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
await core.group(`Bake file definition`, async () => {
|
||||
core.info(fs.readFileSync(bakeFile, 'utf8'));
|
||||
setOutput('bake-file', bakeFile);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function setOutput(name: string, value: string) {
|
||||
core.setOutput(name, value);
|
||||
core.exportVariable(`DOCKER_METADATA_OUTPUT_${name.replace(/\W/g, '_').toUpperCase()}`, value);
|
||||
}
|
||||
|
||||
run();
|
||||
);
|
||||
|
16
src/meta.ts
16
src/meta.ts
@ -4,13 +4,15 @@ import * as path from 'path';
|
||||
import moment from 'moment-timezone';
|
||||
import * as pep440 from '@renovate/pep440';
|
||||
import * as semver from 'semver';
|
||||
import {Inputs, tmpDir} from './context';
|
||||
import {ReposGetResponseData} from './github';
|
||||
import * as core from '@actions/core';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {Context as ToolkitContext} from '@docker/actions-toolkit/lib/context';
|
||||
import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github';
|
||||
|
||||
import {Inputs} from './context';
|
||||
import * as icl from './image';
|
||||
import * as tcl from './tag';
|
||||
import * as fcl from './flavor';
|
||||
import * as core from '@actions/core';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
|
||||
export interface Version {
|
||||
main: string | undefined;
|
||||
@ -23,13 +25,13 @@ export class Meta {
|
||||
|
||||
private readonly inputs: Inputs;
|
||||
private readonly context: Context;
|
||||
private readonly repo: ReposGetResponseData;
|
||||
private readonly repo: GitHubRepo;
|
||||
private readonly images: icl.Image[];
|
||||
private readonly tags: tcl.Tag[];
|
||||
private readonly flavor: fcl.Flavor;
|
||||
private readonly date: Date;
|
||||
|
||||
constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) {
|
||||
constructor(inputs: Inputs, context: Context, repo: GitHubRepo) {
|
||||
// Needs to override Git reference with pr ref instead of upstream branch ref
|
||||
// for pull_request_target event
|
||||
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
|
||||
@ -498,7 +500,7 @@ export class Meta {
|
||||
}
|
||||
|
||||
public getBakeFile(): string {
|
||||
const bakeFile = path.join(tmpDir(), 'docker-metadata-action-bake.json').split(path.sep).join(path.posix.sep);
|
||||
const bakeFile = path.join(ToolkitContext.tmpDir(), 'docker-metadata-action-bake.json');
|
||||
fs.writeFileSync(
|
||||
bakeFile,
|
||||
JSON.stringify(
|
||||
|
Reference in New Issue
Block a user