Allow to templatize schedule tag (#1)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2020-10-25 15:13:43 +01:00
committed by GitHub
parent 88d487154b
commit 3b38d53d94
10 changed files with 14262 additions and 49 deletions

View File

@ -4,6 +4,7 @@ export interface Inputs {
images: string[];
tagSha: boolean;
tagEdge: string;
tagSchedule: string;
sepTags: string;
sepLabels: string;
githubToken: string;
@ -14,6 +15,7 @@ export function getInputs(): Inputs {
images: getInputList('images'),
tagSha: /true/i.test(core.getInput('tag-sha')),
tagEdge: core.getInput('tag-edge'),
tagSchedule: core.getInput('tag-schedule') || 'nightly',
sepTags: core.getInput('sep-tags') || `\n`,
sepLabels: core.getInput('sep-labels') || `\n`,
githubToken: core.getInput('github-token')

View File

@ -31,7 +31,7 @@ async function run() {
core.startGroup(`Docker image version`);
core.info(`${version}`);
core.endGroup();
core.setOutput('version', version);
core.setOutput('version', version || '');
const tags: Array<string> = meta.tags();
core.startGroup(`Docker tags`);

View File

@ -1,3 +1,5 @@
import * as handlebars from 'handlebars';
import * as moment from 'moment';
import * as semver from 'semver';
import {Inputs} from './context';
import * as core from '@actions/core';
@ -8,6 +10,7 @@ export class Meta {
private readonly inputs: Inputs;
private readonly context: Context;
private readonly repo: ReposGetResponseData;
private readonly date: Date;
constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) {
this.inputs = inputs;
@ -16,11 +19,12 @@ export class Meta {
}
this.context = context;
this.repo = repo;
this.date = new Date();
}
public version(): string | undefined {
if (/schedule/.test(this.context.eventName)) {
return 'nightly';
return handlebars.compile(this.inputs.tagSchedule)(this.scheduleTplContext());
} else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
@ -38,7 +42,7 @@ export class Meta {
let tags: Array<string> = [];
for (const image of this.inputs.images) {
if (/schedule/.test(this.context.eventName)) {
tags.push.apply(tags, Meta.eventSchedule(image));
tags.push.apply(tags, this.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)) {
@ -62,14 +66,15 @@ export class Meta {
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.clone_url || ''}`,
`org.opencontainers.image.version=${this.version() || ''}`,
`org.opencontainers.image.created=${new Date().toISOString()}`,
`org.opencontainers.image.created=${this.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 eventSchedule(image: string): Array<string> {
const schedule = handlebars.compile(this.inputs.tagSchedule)(this.scheduleTplContext());
return [`${image}:${schedule}`];
}
private eventTag(image: string): Array<string> {
@ -93,4 +98,13 @@ export class Meta {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return [`${image}:pr-${pr}`];
}
private scheduleTplContext(): any {
const currentDate = this.date;
return {
date: function (format) {
return moment(currentDate).utc().format(format);
}
};
}
}