mirror of
https://github.com/docker/metadata-action.git
synced 2025-06-24 03:37:59 +02:00
Allow to templatize schedule tag (#1)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@ -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')
|
||||
|
@ -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`);
|
||||
|
24
src/meta.ts
24
src/meta.ts
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user