2020-10-25 15:13:43 +01:00
|
|
|
import * as handlebars from 'handlebars';
|
|
|
|
import * as moment from 'moment';
|
2020-10-25 02:25:23 +01:00
|
|
|
import {Inputs} from './context';
|
|
|
|
import {Context} from '@actions/github/lib/context';
|
|
|
|
import {ReposGetResponseData} from '@octokit/types';
|
|
|
|
|
2020-10-26 01:39:21 +01:00
|
|
|
export interface Version {
|
|
|
|
version: string | undefined;
|
|
|
|
latest: boolean;
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
export class Meta {
|
|
|
|
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-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
|
2020-10-26 01:39:21 +01:00
|
|
|
public version(): Version {
|
|
|
|
const currentDate = this.date;
|
|
|
|
const version: Version = {
|
|
|
|
version: undefined,
|
|
|
|
latest: false
|
|
|
|
};
|
|
|
|
|
2020-10-25 02:40:42 +01:00
|
|
|
if (/schedule/.test(this.context.eventName)) {
|
2020-10-26 01:39:21 +01:00
|
|
|
version.version = handlebars.compile(this.inputs.tagSchedule)({
|
|
|
|
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-10-27 00:57:32 +01:00
|
|
|
version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
|
|
|
|
if (this.inputs.tagMatch) {
|
2020-10-27 02:32:26 +01:00
|
|
|
let tagMatch;
|
|
|
|
const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
|
|
|
|
if (isRegEx) {
|
|
|
|
tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
|
|
|
|
} else {
|
|
|
|
tagMatch = version.version.match(this.inputs.tagMatch);
|
|
|
|
}
|
2020-10-27 00:57:32 +01:00
|
|
|
if (tagMatch) {
|
2020-10-27 02:32:26 +01:00
|
|
|
version.version = tagMatch[this.inputs.tagMatchGroup];
|
2020-10-27 00:57:32 +01:00
|
|
|
version.latest = this.inputs.tagMatchLatest;
|
2020-10-26 01:39:21 +01:00
|
|
|
}
|
2020-10-28 18:25:31 +01:00
|
|
|
} else {
|
2020-10-31 19:52:55 +01:00
|
|
|
version.latest = this.inputs.tagMatchLatest;
|
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-10-26 01:39:21 +01:00
|
|
|
version.version = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
|
|
|
|
if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === version.version) {
|
|
|
|
version.version = 'edge';
|
|
|
|
}
|
2020-10-25 02:40:42 +01:00
|
|
|
} else if (/^refs\/pull\//.test(this.context.ref)) {
|
2020-10-26 01:39:21 +01:00
|
|
|
version.version = `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
|
|
|
|
|
|
|
return version;
|
2020-10-25 02:40:42 +01:00
|
|
|
}
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
public tags(): Array<string> {
|
2020-10-26 01:39:21 +01:00
|
|
|
const version: Version = this.version();
|
|
|
|
if (!version.version) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
let tags: Array<string> = [];
|
|
|
|
for (const image of this.inputs.images) {
|
2020-10-26 01:39:21 +01:00
|
|
|
tags.push(`${image}:${version.version}`);
|
|
|
|
if (version.latest) {
|
|
|
|
tags.push(`${image}:latest`);
|
2020-10-25 02:25:23 +01:00
|
|
|
}
|
|
|
|
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 || ''}`,
|
2020-10-31 20:16:51 +01:00
|
|
|
`org.opencontainers.image.source=${this.repo.html_url || ''}`,
|
2020-10-26 01:39:21 +01:00
|
|
|
`org.opencontainers.image.version=${this.version().version || ''}`,
|
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 || ''}`
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|