-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
45 lines (36 loc) · 1.58 KB
/
index.mjs
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
import core from '@actions/core'
const branchVersionRegex = /^.*?\/(?<version>\d+\.\d+\.\d+)(?:\/(?:.*)+)?$/;
const versionTagRegex = /^v(?<version>\d+\.\d+\.\d+)$/;
function getBranchInfoNumber(refName) {
return refName.replaceAll(/[\/_]/g, '-').replaceAll(/[^0-9A-Za-z-]/g, '');
}
function extractVersionNumber() {
const refName = process.env.GITHUB_REF_NAME;
const runNumber = process.env.GITHUB_RUN_NUMBER;
const result = branchVersionRegex.exec(refName) ?? versionTagRegex.exec(refName);
const mainVersion = core.getInput('main_version');
let version;
if (result === null) {
version = mainVersion;
if(refName !== 'master') {
core.info('Not on master branch, adding branch name to build name');
version += "-" + getBranchInfoNumber(refName);
}
core.info(`Version number was not found in branch name, assuming latest. Got RefName: '${refName}'. Falling back to main version: '${version}'`);
} else {
version = result.groups.version;
if(!refName.startsWith('release/')) {
core.info('not on a strict release branch, adding more info to build number')
version += "-" + getBranchInfoNumber(refName);
}
core.info(`Version was extracted from refname without issues: '${version}'`);
}
const fullVersionNumber = `${version}-${runNumber}`;
core.setOutput('version', fullVersionNumber);
core.setOutput('release_version', result?.groups?.version ?? mainVersion);
}
try {
extractVersionNumber();
} catch(error) {
core.setFailed(error.message);
}