Initial commit

This commit is contained in:
CrazyMax
2020-10-25 02:25:23 +01:00
commit 90ec551e12
42 changed files with 14168 additions and 0 deletions

38
src/context.ts Normal file
View File

@ -0,0 +1,38 @@
import * as core from '@actions/core';
export interface Inputs {
images: string[];
tagSha: boolean;
tagEdge: string;
sepTags: string;
sepLabels: string;
githubToken: string;
}
export function getInputs(): Inputs {
return {
images: getInputList('images'),
tagSha: /true/i.test(core.getInput('tag-sha')),
tagEdge: core.getInput('tag-edge'),
sepTags: core.getInput('sep-tags') || `\n`,
sepLabels: core.getInput('sep-labels') || `\n`,
githubToken: core.getInput('github-token')
};
}
export function getInputList(name: string): string[] {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce<string[]>((acc, line) => acc.concat(line.split(',').filter(x => x)).map(pat => pat.trim()), []);
}
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};

18
src/github.ts Normal file
View File

@ -0,0 +1,18 @@
import * as github from '@actions/github';
import {Context} from '@actions/github/lib/context';
import {ReposGetResponseData} from '@octokit/types';
export function context(): Context {
return github.context;
}
export async function repo(token: string): Promise<ReposGetResponseData> {
const octokit = github.getOctokit(token);
const repo = await octokit.repos.get({
...github.context.repo
});
if (!repo?.data) {
throw new Error('Cannot get GitHub repository');
}
return repo.data;
}

47
src/main.ts Normal file
View File

@ -0,0 +1,47 @@
import {getInputs, Inputs} from './context';
import * as github from './github';
import {Meta} from './meta';
import * as core from '@actions/core';
import {Context} from '@actions/github/lib/context';
import {ReposGetResponseData} from '@octokit/types';
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: ReposGetResponseData = await github.repo(inputs.githubToken);
core.startGroup(`Context info`);
core.info(`repo: ${context.repo}`);
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 tags: Array<string> = meta.tags();
core.startGroup(`Generated Docker tags`);
core.info(JSON.stringify(tags));
core.endGroup();
core.setOutput('tags', tags.join(inputs.sepTags));
const labels: Array<string> = meta.labels();
core.startGroup(`Generated Docker labels`);
core.info(JSON.stringify(labels));
core.endGroup();
core.setOutput('labels', labels.join(inputs.sepTags));
} catch (error) {
core.setFailed(error.message);
}
}
run();

96
src/meta.ts Normal file
View File

@ -0,0 +1,96 @@
import * as semver from 'semver';
import {Inputs} from './context';
import * as core from '@actions/core';
import {Context} from '@actions/github/lib/context';
import {ReposGetResponseData} from '@octokit/types';
export class Meta {
private readonly inputs: Inputs;
private readonly context: Context;
private readonly repo: ReposGetResponseData;
constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) {
this.inputs = inputs;
if (!this.inputs.tagEdge) {
this.inputs.tagEdge = repo.default_branch;
}
this.context = context;
this.repo = repo;
}
public tags(): Array<string> {
let tags: Array<string> = [];
for (const image of this.inputs.images) {
if (/schedule/.test(this.context.eventName)) {
tags.push.apply(tags, Meta.eventSchedule(image));
} else if (/^refs\/tags\//.test(this.context.ref)) {
tags.push.apply(tags, this.eventTag(image));
} else if (/^refs\/heads\//.test(this.context.ref)) {
tags.push.apply(tags, this.eventBranch(image));
} else if (/^refs\/pull\//.test(this.context.ref)) {
tags.push.apply(tags, this.eventPullRequest(image));
} else {
core.warning(`Unknown event "${this.context.eventName}" with ref "${this.context.ref}"`);
}
if (this.context.sha && this.inputs.tagSha) {
tags.push(`${image}:sha-${this.context.sha.substr(0, 7)}`);
}
}
return tags;
}
public labels(): Array<string> {
return [
`org.opencontainers.image.title=${this.repo.name || ''}`,
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.clone_url || ''}`,
`org.opencontainers.image.version=${this.labelVersion() || ''}`,
`org.opencontainers.image.created=${new Date().toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`
];
}
private static eventSchedule(image: string): Array<string> {
return [`${image}:nightly`];
}
private eventTag(image: string): Array<string> {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const version = semver.clean(tag);
if (version) {
return [`${image}:${version}`, `${image}:latest`];
}
return [`${image}:${tag}`];
}
private eventBranch(image: string): Array<string> {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
if (this.inputs.tagEdge === branch) {
return [`${image}:edge`];
}
return [`${image}:${branch}`];
}
private eventPullRequest(image: string): Array<string> {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return [`${image}:pr-${pr}`];
}
private labelVersion(): string | undefined {
if (/schedule/.test(this.context.eventName)) {
return 'nightly';
} else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
return sver ? sver : tag;
} else if (/^refs\/heads\//.test(this.context.ref)) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
return this.inputs.tagEdge === branch ? 'edge' : branch;
} else if (/^refs\/pull\//.test(this.context.ref)) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return `pr-${pr}`;
}
}
}