Handle semver tags (#14)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2020-11-17 23:31:03 +01:00
committed by GitHub
parent 009f84cd69
commit e8ce48988f
7 changed files with 2573 additions and 84 deletions

View File

@ -5,6 +5,7 @@ export interface Inputs {
tagSha: boolean;
tagEdge: boolean;
tagEdgeBranch: string;
tagSemver: string[];
tagMatch: string;
tagMatchGroup: number;
tagMatchLatest: boolean;
@ -20,6 +21,7 @@ export function getInputs(): Inputs {
tagSha: /true/i.test(core.getInput('tag-sha') || 'false'),
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
tagEdgeBranch: core.getInput('tag-edge-branch'),
tagSemver: getInputList('tag-semver'),
tagMatch: core.getInput('tag-match'),
tagMatchGroup: Number(core.getInput('tag-match-group')) || 0,
tagMatchLatest: /true/i.test(core.getInput('tag-match-latest') || 'true'),

View File

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

View File

@ -1,11 +1,13 @@
import * as handlebars from 'handlebars';
import * as moment from 'moment';
import * as semver from 'semver';
import {Inputs} from './context';
import {Context} from '@actions/github/lib/context';
import {ReposGetResponseData} from '@octokit/types';
export interface Version {
version: string | undefined;
main: string | undefined;
partial: string[];
latest: boolean;
}
@ -28,40 +30,56 @@ export class Meta {
public version(): Version {
const currentDate = this.date;
const version: Version = {
version: undefined,
main: undefined,
partial: [],
latest: false
};
if (/schedule/.test(this.context.eventName)) {
version.version = handlebars.compile(this.inputs.tagSchedule)({
version.main = handlebars.compile(this.inputs.tagSchedule)({
date: function (format) {
return moment(currentDate).utc().format(format);
}
});
} else if (/^refs\/tags\//.test(this.context.ref)) {
version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
if (this.inputs.tagMatch) {
version.main = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
if (this.inputs.tagSemver.length > 0 && semver.valid(version.main)) {
const sver = semver.parse(version.main, {
includePrerelease: true
});
version.latest = !semver.prerelease(version.main);
version.main = handlebars.compile(this.inputs.tagSemver[0])(sver);
if (version.latest) {
for (const semverTpl of this.inputs.tagSemver) {
const partial = handlebars.compile(semverTpl)(sver);
if (partial == version.main) {
continue;
}
version.partial.push(partial);
}
}
} else if (this.inputs.tagMatch) {
let tagMatch;
const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
if (isRegEx) {
tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
tagMatch = version.main.match(new RegExp(isRegEx[1], isRegEx[2]));
} else {
tagMatch = version.version.match(this.inputs.tagMatch);
tagMatch = version.main.match(this.inputs.tagMatch);
}
if (tagMatch) {
version.version = tagMatch[this.inputs.tagMatchGroup];
version.main = tagMatch[this.inputs.tagMatchGroup];
version.latest = this.inputs.tagMatchLatest;
}
} else {
version.latest = this.inputs.tagMatchLatest;
}
} else if (/^refs\/heads\//.test(this.context.ref)) {
version.version = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === version.version) {
version.version = 'edge';
version.main = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === version.main) {
version.main = 'edge';
}
} else if (/^refs\/pull\//.test(this.context.ref)) {
version.version = `pr-${this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '')}`;
version.main = `pr-${this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '')}`;
}
return version;
@ -69,13 +87,16 @@ export class Meta {
public tags(): Array<string> {
const version: Version = this.version();
if (!version.version) {
if (!version.main) {
return [];
}
let tags: Array<string> = [];
for (const image of this.inputs.images) {
tags.push(`${image}:${version.version}`);
tags.push(`${image}:${version.main}`);
for (const partial of version.partial) {
tags.push(`${image}:${partial}`);
}
if (version.latest) {
tags.push(`${image}:latest`);
}
@ -92,7 +113,7 @@ export class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.html_url || ''}`,
`org.opencontainers.image.version=${this.version().version || ''}`,
`org.opencontainers.image.version=${this.version().main || ''}`,
`org.opencontainers.image.created=${this.date.toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`