2020-10-25 15:13:43 +01:00
|
|
|
import * as handlebars from 'handlebars';
|
2020-12-23 22:09:38 +01:00
|
|
|
import moment from 'moment';
|
2020-11-17 23:31:03 +01:00
|
|
|
import * as semver from 'semver';
|
2020-10-25 02:25:23 +01:00
|
|
|
import {Inputs} from './context';
|
2020-12-01 05:50:39 +01:00
|
|
|
import * as core from '@actions/core';
|
2020-10-25 02:25:23 +01:00
|
|
|
import {Context} from '@actions/github/lib/context';
|
|
|
|
import {ReposGetResponseData} from '@octokit/types';
|
|
|
|
|
2020-10-26 01:39:21 +01:00
|
|
|
export interface Version {
|
2020-11-17 23:31:03 +01:00
|
|
|
main: string | undefined;
|
|
|
|
partial: string[];
|
2020-10-26 01:39:21 +01:00
|
|
|
latest: boolean;
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
export class Meta {
|
2020-12-01 05:38:08 +01:00
|
|
|
public readonly version: Version;
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
private readonly inputs: Inputs;
|
|
|
|
private readonly context: Context;
|
|
|
|
private readonly repo: ReposGetResponseData;
|
2020-10-25 15:13:43 +01:00
|
|
|
private readonly date: Date;
|
2020-10-25 02:25:23 +01:00
|
|
|
|
|
|
|
constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) {
|
|
|
|
this.inputs = inputs;
|
2020-10-25 15:32:14 +01:00
|
|
|
if (!this.inputs.tagEdgeBranch) {
|
|
|
|
this.inputs.tagEdgeBranch = repo.default_branch;
|
2020-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
this.context = context;
|
|
|
|
this.repo = repo;
|
2020-10-25 15:13:43 +01:00
|
|
|
this.date = new Date();
|
2020-12-01 05:38:08 +01:00
|
|
|
this.version = this.getVersion();
|
2020-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 05:38:08 +01:00
|
|
|
private getVersion(): Version {
|
2020-10-26 01:39:21 +01:00
|
|
|
const currentDate = this.date;
|
2020-12-04 18:12:39 +01:00
|
|
|
let version: Version = {
|
2020-11-17 23:31:03 +01:00
|
|
|
main: undefined,
|
|
|
|
partial: [],
|
2020-10-26 01:39:21 +01:00
|
|
|
latest: false
|
|
|
|
};
|
|
|
|
|
2020-10-25 02:40:42 +01:00
|
|
|
if (/schedule/.test(this.context.eventName)) {
|
2020-11-17 23:31:03 +01:00
|
|
|
version.main = handlebars.compile(this.inputs.tagSchedule)({
|
2020-10-26 01:39:21 +01:00
|
|
|
date: function (format) {
|
|
|
|
return moment(currentDate).utc().format(format);
|
|
|
|
}
|
|
|
|
});
|
2020-10-25 02:40:42 +01:00
|
|
|
} else if (/^refs\/tags\//.test(this.context.ref)) {
|
2020-11-17 23:31:03 +01:00
|
|
|
version.main = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
|
2020-12-01 05:50:39 +01:00
|
|
|
if (this.inputs.tagSemver.length > 0 && !semver.valid(version.main)) {
|
|
|
|
core.warning(`${version.main} is not a valid semver. More info: https://semver.org/`);
|
|
|
|
}
|
2020-11-17 23:31:03 +01:00
|
|
|
if (this.inputs.tagSemver.length > 0 && semver.valid(version.main)) {
|
|
|
|
const sver = semver.parse(version.main, {
|
|
|
|
includePrerelease: true
|
|
|
|
});
|
2020-11-20 23:12:14 +01:00
|
|
|
if (semver.prerelease(version.main)) {
|
|
|
|
version.main = handlebars.compile('{{version}}')(sver);
|
|
|
|
} else {
|
2020-12-01 06:29:34 +01:00
|
|
|
version.latest = this.inputs.tagLatest;
|
2020-11-20 23:12:14 +01:00
|
|
|
version.main = handlebars.compile(this.inputs.tagSemver[0])(sver);
|
2020-11-17 23:31:03 +01:00
|
|
|
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) {
|
2020-10-27 02:32:26 +01:00
|
|
|
let tagMatch;
|
|
|
|
const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
|
|
|
|
if (isRegEx) {
|
2020-11-17 23:31:03 +01:00
|
|
|
tagMatch = version.main.match(new RegExp(isRegEx[1], isRegEx[2]));
|
2020-10-27 02:32:26 +01:00
|
|
|
} else {
|
2020-11-17 23:31:03 +01:00
|
|
|
tagMatch = version.main.match(this.inputs.tagMatch);
|
2020-10-27 02:32:26 +01:00
|
|
|
}
|
2020-10-27 00:57:32 +01:00
|
|
|
if (tagMatch) {
|
2020-11-17 23:31:03 +01:00
|
|
|
version.main = tagMatch[this.inputs.tagMatchGroup];
|
2020-12-01 06:29:34 +01:00
|
|
|
version.latest = this.inputs.tagLatest;
|
2020-10-26 01:39:21 +01:00
|
|
|
}
|
2020-10-28 18:25:31 +01:00
|
|
|
} else {
|
2020-12-01 06:29:34 +01:00
|
|
|
version.latest = this.inputs.tagLatest;
|
2020-10-26 01:39:21 +01:00
|
|
|
}
|
2020-10-25 02:40:42 +01:00
|
|
|
} else if (/^refs\/heads\//.test(this.context.ref)) {
|
2020-12-08 00:01:39 +01:00
|
|
|
version.main = this.context.ref.replace(/^refs\/heads\//g, '').replace(/[^a-zA-Z0-9._-]+/g, '-');
|
2020-11-17 23:31:03 +01:00
|
|
|
if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === version.main) {
|
|
|
|
version.main = 'edge';
|
2020-10-26 01:39:21 +01:00
|
|
|
}
|
2020-10-25 02:40:42 +01:00
|
|
|
} else if (/^refs\/pull\//.test(this.context.ref)) {
|
2020-11-17 23:31:03 +01:00
|
|
|
version.main = `pr-${this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '')}`;
|
2020-10-25 02:40:42 +01:00
|
|
|
}
|
2020-10-26 01:39:21 +01:00
|
|
|
|
2020-12-04 18:12:39 +01:00
|
|
|
if (this.inputs.tagCustom.length > 0) {
|
|
|
|
if (this.inputs.tagCustomOnly) {
|
|
|
|
version = {
|
|
|
|
main: this.inputs.tagCustom.shift(),
|
|
|
|
partial: this.inputs.tagCustom,
|
|
|
|
latest: false
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
version.partial.push(...this.inputs.tagCustom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 01:10:05 +01:00
|
|
|
version.partial = version.partial.filter((item, index) => version.partial.indexOf(item) === index);
|
2020-10-26 01:39:21 +01:00
|
|
|
return version;
|
2020-10-25 02:40:42 +01:00
|
|
|
}
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
public tags(): Array<string> {
|
2020-12-01 05:38:08 +01:00
|
|
|
if (!this.version.main) {
|
2020-10-26 01:39:21 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
let tags: Array<string> = [];
|
|
|
|
for (const image of this.inputs.images) {
|
2020-11-20 16:19:08 +01:00
|
|
|
const imageLc = image.toLowerCase();
|
2020-12-01 05:38:08 +01:00
|
|
|
tags.push(`${imageLc}:${this.version.main}`);
|
|
|
|
for (const partial of this.version.partial) {
|
2020-11-20 16:19:08 +01:00
|
|
|
tags.push(`${imageLc}:${partial}`);
|
2020-11-17 23:31:03 +01:00
|
|
|
}
|
2020-12-01 05:38:08 +01:00
|
|
|
if (this.version.latest) {
|
2020-11-20 16:19:08 +01:00
|
|
|
tags.push(`${imageLc}:latest`);
|
2020-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
if (this.context.sha && this.inputs.tagSha) {
|
2020-11-20 16:19:08 +01:00
|
|
|
tags.push(`${imageLc}:sha-${this.context.sha.substr(0, 7)}`);
|
2020-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public labels(): Array<string> {
|
2020-12-23 22:09:38 +01:00
|
|
|
let labels: Array<string> = [
|
2020-10-25 02:25:23 +01:00
|
|
|
`org.opencontainers.image.title=${this.repo.name || ''}`,
|
|
|
|
`org.opencontainers.image.description=${this.repo.description || ''}`,
|
|
|
|
`org.opencontainers.image.url=${this.repo.html_url || ''}`,
|
2020-10-31 20:16:51 +01:00
|
|
|
`org.opencontainers.image.source=${this.repo.html_url || ''}`,
|
2020-12-01 05:38:08 +01:00
|
|
|
`org.opencontainers.image.version=${this.version.main || ''}`,
|
2020-10-25 15:13:43 +01:00
|
|
|
`org.opencontainers.image.created=${this.date.toISOString()}`,
|
2020-10-25 02:25:23 +01:00
|
|
|
`org.opencontainers.image.revision=${this.context.sha || ''}`,
|
|
|
|
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`
|
|
|
|
];
|
2020-12-23 22:09:38 +01:00
|
|
|
labels.push(...this.inputs.labelCustom);
|
|
|
|
return labels;
|
2020-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
}
|