Coerces Git tag to semver (#3)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2020-10-26 01:39:21 +01:00
committed by GitHub
parent 8ec02f11ef
commit 5bc3bf6dce
13 changed files with 785 additions and 443 deletions

115
dist/index.js generated vendored
View File

@ -25,6 +25,7 @@ function getInputs() {
tagSha: /true/i.test(core.getInput('tag-sha') || 'false'),
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
tagEdgeBranch: core.getInput('tag-edge-branch'),
tagCoerceTag: core.getInput('tag-coerce-tag'),
tagSchedule: core.getInput('tag-schedule') || 'nightly',
sepTags: core.getInput('sep-tags') || `\n`,
sepLabels: core.getInput('sep-labels') || `\n`,
@ -129,9 +130,9 @@ function run() {
const meta = new meta_1.Meta(inputs, context, repo);
const version = meta.version();
core.startGroup(`Docker image version`);
core.info(`${version}`);
core.info(version.version || '');
core.endGroup();
core.setOutput('version', version || '');
core.setOutput('version', version.version || '');
const tags = meta.tags();
core.startGroup(`Docker tags`);
for (let tag of tags) {
@ -167,7 +168,6 @@ exports.Meta = void 0;
const handlebars = __webpack_require__(7492);
const moment = __webpack_require__(9623);
const semver = __webpack_require__(1383);
const core = __webpack_require__(2186);
class Meta {
constructor(inputs, context, repo) {
this.inputs = inputs;
@ -179,40 +179,64 @@ class Meta {
this.date = new Date();
}
version() {
const currentDate = this.date;
const version = {
version: undefined,
latest: false
};
if (/schedule/.test(this.context.eventName)) {
return handlebars.compile(this.inputs.tagSchedule)(this.scheduleTplContext());
version.version = handlebars.compile(this.inputs.tagSchedule)({
date: function (format) {
return moment(currentDate).utc().format(format);
}
});
}
else if (/^refs\/tags\//.test(this.context.ref)) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const sver = semver.clean(tag);
return sver ? sver : tag;
}
else if (/^refs\/heads\//.test(this.context.ref)) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
return this.inputs.tagEdge && this.inputs.tagEdgeBranch === branch ? 'edge' : branch;
}
else if (/^refs\/pull\//.test(this.context.ref)) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return `pr-${pr}`;
}
}
tags() {
let tags = [];
for (const image of this.inputs.images) {
if (/schedule/.test(this.context.eventName)) {
tags.push.apply(tags, this.eventSchedule(image));
if (this.inputs.tagCoerceTag) {
const coerce = semver.coerce(tag);
if (coerce) {
version.version = handlebars.compile(this.inputs.tagCoerceTag)(coerce);
version.latest = true;
}
else if (sver) {
version.version = sver;
version.latest = true;
}
else {
version.version = tag;
}
}
else if (/^refs\/tags\//.test(this.context.ref)) {
tags.push.apply(tags, this.eventTag(image));
}
else if (/^refs\/heads\//.test(this.context.ref)) {
tags.push.apply(tags, this.eventBranch(image));
}
else if (/^refs\/pull\//.test(this.context.ref)) {
tags.push.apply(tags, this.eventPullRequest(image));
else if (sver) {
version.version = sver;
version.latest = true;
}
else {
core.warning(`Unknown event "${this.context.eventName}" with ref "${this.context.ref}"`);
version.version = tag;
}
}
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';
}
}
else if (/^refs\/pull\//.test(this.context.ref)) {
version.version = `pr-${this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '')}`;
}
return version;
}
tags() {
const version = this.version();
if (!version.version) {
return [];
}
let tags = [];
for (const image of this.inputs.images) {
tags.push(`${image}:${version.version}`);
if (version.latest) {
tags.push(`${image}:latest`);
}
if (this.context.sha && this.inputs.tagSha) {
tags.push(`${image}:sha-${this.context.sha.substr(0, 7)}`);
@ -227,43 +251,12 @@ class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`,
`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.version=${this.version().version || ''}`,
`org.opencontainers.image.created=${this.date.toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${((_a = this.repo.license) === null || _a === void 0 ? void 0 : _a.spdx_id) || ''}`
];
}
eventSchedule(image) {
const schedule = handlebars.compile(this.inputs.tagSchedule)(this.scheduleTplContext());
return [`${image}:${schedule}`];
}
eventTag(image) {
const tag = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
const version = semver.clean(tag);
if (version) {
return [`${image}:${version}`, `${image}:latest`];
}
return [`${image}:${tag}`];
}
eventBranch(image) {
const branch = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === branch) {
return [`${image}:edge`];
}
return [`${image}:${branch}`];
}
eventPullRequest(image) {
const pr = this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '');
return [`${image}:pr-${pr}`];
}
scheduleTplContext() {
const currentDate = this.date;
return {
date: function (format) {
return moment(currentDate).utc().format(format);
}
};
}
}
exports.Meta = Meta;
//# sourceMappingURL=meta.js.map