This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
92 lines (76 loc) · 2.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import format from 'date-fns/format';
/**
* Parse and create tags
*
* @param semver Semantic version string
* @param suffix Suffix string to append to trailing of each tags
* @param additionalTagsStr Comma separated string
* @returns Array of tags
*/
function parseSemVer(semver: string, suffix: string, additionalTagsStr: string): Array<string> {
// https://regex101.com/r/sxGQtU/2
const match: RegExpMatchArray | null = semver.match(/^([0-9]+)\.([0-9]+)\.([0-9]+)(-RC[0-9]*)?$/);
if (match == null) {
throw new Error(`Param 'semver' is invalid: ${semver}`);
}
const all = match[0];
const major = match[1];
const minor = match[2];
const patch = match[3];
const rc = match[4];
core.debug(`all=${all}`);
core.debug(`major=${major}`);
core.debug(`minor=${minor}`);
core.debug(`patch=${patch}`);
core.debug(`rc=${rc}`);
const tags: Array<string> = [];
if (rc != null) {
const date: string = format(new Date(), 'yyyyMMddhhmmss');
tags.push(all);
tags.push(`${all}.${date}`);
}
else {
tags.push(`${major}.${minor}.${patch}${suffix}`);
tags.push(`${major}.${minor}${suffix}`);
tags.push(`${major}${suffix}`);
}
// append additional tags
additionalTagsStr.split(',').forEach((splitted) => {
const tag = splitted.trim();
if (tag.length > 0) {
tags.push(`${tag}${suffix}`);
}
});
return tags;
}
async function run() {
try {
// check docker command
await exec('docker', ['-v']);
const source: string = core.getInput('source', { required: true });
const target: string = core.getInput('target', { required: true });
const semver: string = core.getInput('semver', { required: true });
const suffix: string = core.getInput('suffix'); // default: ''
const additionalTagsStr: string = core.getInput('additional-tags') // default: '';
const isPublish: boolean = core.getInput('publish') === 'true'; // default: undefined
// generate tags
const tags: Array<string> = parseSemVer(semver, suffix, additionalTagsStr);
for (const tag of tags) {
const name = `${target}:${tag}`;
core.debug(`processing '${name}'`);
// exec 'docker tag'
await exec('docker', ['tag', source, name]);
// exec 'docker push
if (isPublish) {
await exec('docker', ['push', name]);
}
}
core.setOutput('tags', tags.join(', '));
}
catch (error) {
core.setFailed(error.message);
}
}
run();