2021-10-05 17:19:01 +00:00
|
|
|
// test
|
2021-08-08 01:28:27 +00:00
|
|
|
const { execSync, spawn } = require('child_process');
|
2021-08-07 23:35:03 +00:00
|
|
|
const { existsSync } = require('fs');
|
2021-08-08 01:28:27 +00:00
|
|
|
const { EOL } = require('os');
|
2021-08-07 23:35:03 +00:00
|
|
|
const path = require('path');
|
2019-10-26 19:06:00 +00:00
|
|
|
|
2020-01-13 14:04:50 +00:00
|
|
|
// Change working directory if user defined PACKAGEJSON_DIR
|
|
|
|
if (process.env.PACKAGEJSON_DIR) {
|
2021-05-20 20:54:50 +00:00
|
|
|
process.env.GITHUB_WORKSPACE = `${process.env.GITHUB_WORKSPACE}/${process.env.PACKAGEJSON_DIR}`;
|
|
|
|
process.chdir(process.env.GITHUB_WORKSPACE);
|
2020-01-13 14:04:50 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 23:35:03 +00:00
|
|
|
const workspace = process.env.GITHUB_WORKSPACE;
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const pkg = getPackageJson();
|
2021-08-08 01:28:27 +00:00
|
|
|
const event = process.env.GITHUB_EVENT_PATH ? require(process.env.GITHUB_EVENT_PATH) : {};
|
2019-10-26 19:06:00 +00:00
|
|
|
|
2020-07-23 14:08:46 +00:00
|
|
|
if (!event.commits) {
|
2021-05-20 20:54:50 +00:00
|
|
|
console.log("Couldn't find any commits in this event, incrementing patch version...");
|
2020-07-23 14:08:46 +00:00
|
|
|
}
|
2020-09-06 10:24:19 +00:00
|
|
|
|
2021-07-01 20:10:38 +00:00
|
|
|
const tagPrefix = process.env['INPUT_TAG-PREFIX'] || '';
|
2022-05-31 18:53:50 +00:00
|
|
|
console.log('tagPrefix:', tagPrefix);
|
2021-05-20 20:54:50 +00:00
|
|
|
const messages = event.commits ? event.commits.map((commit) => commit.message + '\n' + commit.body) : [];
|
2019-10-26 19:06:00 +00:00
|
|
|
|
2021-05-20 20:54:50 +00:00
|
|
|
const commitMessage = process.env['INPUT_COMMIT-MESSAGE'] || 'ci: version bump to {{version}}';
|
2021-07-01 20:10:38 +00:00
|
|
|
console.log('commit messages:', messages);
|
2022-01-13 20:25:09 +00:00
|
|
|
|
|
|
|
const bumpPolicy = process.env['INPUT_BUMP-POLICY'] || 'all';
|
2021-07-01 20:10:38 +00:00
|
|
|
const commitMessageRegex = new RegExp(commitMessage.replace(/{{version}}/g, `${tagPrefix}\\d+\\.\\d+\\.\\d+`), 'ig');
|
2022-01-13 20:25:09 +00:00
|
|
|
|
|
|
|
let isVersionBump = false;
|
|
|
|
|
|
|
|
if (bumpPolicy === 'all') {
|
|
|
|
isVersionBump = messages.find((message) => commitMessageRegex.test(message)) !== undefined;
|
|
|
|
} else if (bumpPolicy === 'last-commit') {
|
|
|
|
isVersionBump = messages.length > 0 && commitMessageRegex.test(messages[messages.length - 1]);
|
|
|
|
} else if (bumpPolicy === 'ignore') {
|
|
|
|
console.log('Ignoring any version bumps in commits...');
|
|
|
|
} else {
|
|
|
|
console.warn(`Unknown bump policy: ${bumpPolicy}`);
|
|
|
|
}
|
2021-03-02 21:52:52 +00:00
|
|
|
|
2019-10-26 19:06:00 +00:00
|
|
|
if (isVersionBump) {
|
2021-08-07 23:35:03 +00:00
|
|
|
exitSuccess('No action necessary because we found a previous bump!');
|
2021-05-20 20:54:50 +00:00
|
|
|
return;
|
2019-10-26 19:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 18:17:11 +00:00
|
|
|
// input wordings for MAJOR, MINOR, PATCH, PRE-RELEASE
|
2021-05-20 20:54:50 +00:00
|
|
|
const majorWords = process.env['INPUT_MAJOR-WORDING'].split(',');
|
|
|
|
const minorWords = process.env['INPUT_MINOR-WORDING'].split(',');
|
2021-07-01 20:10:38 +00:00
|
|
|
// patch is by default empty, and '' would always be true in the includes(''), thats why we handle it separately
|
|
|
|
const patchWords = process.env['INPUT_PATCH-WORDING'] ? process.env['INPUT_PATCH-WORDING'].split(',') : null;
|
2021-11-22 12:54:01 +00:00
|
|
|
const preReleaseWords = process.env['INPUT_RC-WORDING'] ? process.env['INPUT_RC-WORDING'].split(',') : null;
|
2020-10-06 15:50:52 +00:00
|
|
|
|
2021-07-01 20:10:38 +00:00
|
|
|
console.log('config words:', { majorWords, minorWords, patchWords, preReleaseWords });
|
|
|
|
|
2021-07-01 18:17:11 +00:00
|
|
|
// get default version bump
|
2021-05-20 21:41:43 +00:00
|
|
|
let version = process.env.INPUT_DEFAULT;
|
2021-05-20 20:54:50 +00:00
|
|
|
let foundWord = null;
|
2021-07-01 18:17:11 +00:00
|
|
|
// get the pre-release prefix specified in action
|
2021-03-10 09:34:41 +00:00
|
|
|
let preid = process.env.INPUT_PREID;
|
2021-07-01 10:08:46 +00:00
|
|
|
|
2021-07-01 18:17:11 +00:00
|
|
|
// case: if wording for MAJOR found
|
2021-05-20 20:54:50 +00:00
|
|
|
if (
|
|
|
|
messages.some(
|
|
|
|
(message) => /^([a-zA-Z]+)(\(.+\))?(\!)\:/.test(message) || majorWords.some((word) => message.includes(word)),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
version = 'major';
|
2021-07-01 18:17:11 +00:00
|
|
|
}
|
|
|
|
// case: if wording for MINOR found
|
|
|
|
else if (messages.some((message) => minorWords.some((word) => message.includes(word)))) {
|
2021-05-20 20:54:50 +00:00
|
|
|
version = 'minor';
|
2021-07-01 18:17:11 +00:00
|
|
|
}
|
|
|
|
// case: if wording for PATCH found
|
2021-07-01 20:10:38 +00:00
|
|
|
else if (patchWords && messages.some((message) => patchWords.some((word) => message.includes(word)))) {
|
2021-07-01 12:16:01 +00:00
|
|
|
version = 'patch';
|
2021-07-01 18:17:11 +00:00
|
|
|
}
|
|
|
|
// case: if wording for PRE-RELEASE found
|
|
|
|
else if (
|
2021-11-22 12:54:01 +00:00
|
|
|
preReleaseWords &&
|
2021-05-20 20:54:50 +00:00
|
|
|
messages.some((message) =>
|
|
|
|
preReleaseWords.some((word) => {
|
|
|
|
if (message.includes(word)) {
|
|
|
|
foundWord = word;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
preid = foundWord.split('-')[1];
|
|
|
|
version = 'prerelease';
|
2020-10-09 16:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 20:10:38 +00:00
|
|
|
console.log('version action after first waterfall:', version);
|
2021-07-01 18:36:01 +00:00
|
|
|
|
2021-07-01 18:26:14 +00:00
|
|
|
// case: if default=prerelease,
|
|
|
|
// rc-wording is also set
|
|
|
|
// and does not include any of rc-wording
|
|
|
|
// then unset it and do not run
|
2021-07-01 20:10:38 +00:00
|
|
|
if (
|
|
|
|
version === 'prerelease' &&
|
2021-11-22 12:54:01 +00:00
|
|
|
preReleaseWords &&
|
2021-07-01 20:10:38 +00:00
|
|
|
!messages.some((message) => preReleaseWords.some((word) => message.includes(word)))
|
|
|
|
) {
|
2021-07-01 18:17:11 +00:00
|
|
|
version = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// case: if default=prerelease, but rc-wording is NOT set
|
2021-07-01 13:01:06 +00:00
|
|
|
if (version === 'prerelease' && preid) {
|
2021-07-01 10:27:08 +00:00
|
|
|
version = 'prerelease';
|
2021-05-20 20:54:50 +00:00
|
|
|
version = `${version} --preid=${preid}`;
|
2021-03-10 09:34:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 20:10:38 +00:00
|
|
|
console.log('version action after final decision:', version);
|
|
|
|
|
2021-07-01 18:17:11 +00:00
|
|
|
// case: if nothing of the above matches
|
2022-01-27 16:01:25 +00:00
|
|
|
if (!version) {
|
2021-08-07 23:35:03 +00:00
|
|
|
exitSuccess('No version keywords found, skipping bump.');
|
2021-05-20 20:54:50 +00:00
|
|
|
return;
|
2019-10-26 19:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 21:43:33 +00:00
|
|
|
// case: if user sets push to false, to skip pushing new tag/package.json
|
2021-08-07 23:35:03 +00:00
|
|
|
const push = process.env['INPUT_PUSH'];
|
|
|
|
if (push === 'false' || push === false) {
|
|
|
|
exitSuccess('User requested to skip pushing new tag and package.json. Finished.');
|
2021-07-13 21:38:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:17:11 +00:00
|
|
|
// GIT logic
|
2019-10-26 19:06:00 +00:00
|
|
|
try {
|
2021-05-20 20:54:50 +00:00
|
|
|
const current = pkg.version.toString();
|
2019-10-26 19:42:48 +00:00
|
|
|
// set git user
|
2021-08-07 23:35:03 +00:00
|
|
|
await runInWorkspace('git', ['config', 'user.name', `"${process.env.GITHUB_USER || 'Automated Version Bump'}"`]);
|
|
|
|
await runInWorkspace('git', [
|
2021-05-20 20:54:50 +00:00
|
|
|
'config',
|
|
|
|
'user.email',
|
|
|
|
`"${process.env.GITHUB_EMAIL || 'gh-action-bump-version@users.noreply.github.com'}"`,
|
|
|
|
]);
|
2019-10-26 19:42:48 +00:00
|
|
|
|
2022-05-31 18:34:44 +00:00
|
|
|
const refsRegExResult = /refs\/[a-zA-Z]+\/(.*)/.exec(process.env.GITHUB_REF);
|
|
|
|
let currentBranch = refsRegExResult ? refsRegExResult[1] : undefined;
|
2021-05-20 20:54:50 +00:00
|
|
|
let isPullRequest = false;
|
2020-09-26 12:13:48 +00:00
|
|
|
if (process.env.GITHUB_HEAD_REF) {
|
2020-09-26 12:34:59 +00:00
|
|
|
// Comes from a pull request
|
2021-05-20 20:54:50 +00:00
|
|
|
currentBranch = process.env.GITHUB_HEAD_REF;
|
|
|
|
isPullRequest = true;
|
2020-09-26 12:13:48 +00:00
|
|
|
}
|
2020-11-25 14:47:18 +00:00
|
|
|
if (process.env['INPUT_TARGET-BRANCH']) {
|
|
|
|
// We want to override the branch that we are pulling / pushing to
|
2021-05-20 20:54:50 +00:00
|
|
|
currentBranch = process.env['INPUT_TARGET-BRANCH'];
|
2020-11-25 04:15:10 +00:00
|
|
|
}
|
2021-05-20 20:54:50 +00:00
|
|
|
console.log('currentBranch:', currentBranch);
|
2022-05-31 18:34:44 +00:00
|
|
|
|
|
|
|
if (!currentBranch) {
|
|
|
|
exitFailure('No branch found');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-27 19:16:16 +00:00
|
|
|
// do it in the current checked out github branch (DETACHED HEAD)
|
|
|
|
// important for further usage of the package.json version
|
2021-08-07 23:35:03 +00:00
|
|
|
await runInWorkspace('npm', ['version', '--allow-same-version=true', '--git-tag-version=false', current]);
|
2022-05-31 18:53:50 +00:00
|
|
|
console.log('current 1:', current, '/', 'version:', version);
|
2021-05-20 21:07:53 +00:00
|
|
|
let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, '');
|
2021-07-01 20:10:38 +00:00
|
|
|
newVersion = `${tagPrefix}${newVersion}`;
|
2021-11-30 14:55:02 +00:00
|
|
|
if (process.env['INPUT_SKIP-COMMIT'] !== 'true') {
|
|
|
|
await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]);
|
|
|
|
}
|
2019-10-27 19:07:29 +00:00
|
|
|
|
2019-10-27 19:16:16 +00:00
|
|
|
// now go to the actual branch to perform the same versioning
|
2020-09-30 17:41:15 +00:00
|
|
|
if (isPullRequest) {
|
|
|
|
// First fetch to get updated local version of branch
|
2021-08-07 23:35:03 +00:00
|
|
|
await runInWorkspace('git', ['fetch']);
|
2020-09-30 17:41:15 +00:00
|
|
|
}
|
2021-08-07 23:35:03 +00:00
|
|
|
await runInWorkspace('git', ['checkout', currentBranch]);
|
|
|
|
await runInWorkspace('npm', ['version', '--allow-same-version=true', '--git-tag-version=false', current]);
|
2022-05-31 18:53:50 +00:00
|
|
|
console.log('current 2:', current, '/', 'version:', version);
|
|
|
|
console.log('execute npm version now with the new version:', version);
|
2021-05-20 21:07:53 +00:00
|
|
|
newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, '');
|
2021-07-01 20:10:38 +00:00
|
|
|
newVersion = `${tagPrefix}${newVersion}`;
|
2022-05-31 18:53:50 +00:00
|
|
|
console.log(`newVersion: ${newVersion}`);
|
2021-05-20 20:54:50 +00:00
|
|
|
console.log(`::set-output name=newTag::${newVersion}`);
|
2020-04-03 23:39:59 +00:00
|
|
|
try {
|
|
|
|
// to support "actions/checkout@v1"
|
2021-11-30 14:55:02 +00:00
|
|
|
if (process.env['INPUT_SKIP-COMMIT'] !== 'true') {
|
|
|
|
await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]);
|
|
|
|
}
|
2020-04-03 23:39:59 +00:00
|
|
|
} catch (e) {
|
2021-05-20 20:54:50 +00:00
|
|
|
console.warn(
|
|
|
|
'git commit failed because you are using "actions/checkout@v2"; ' +
|
|
|
|
'but that doesnt matter because you dont need that git commit, thats only for "actions/checkout@v1"',
|
|
|
|
);
|
2020-04-03 23:39:59 +00:00
|
|
|
}
|
2020-04-03 11:04:35 +00:00
|
|
|
|
2021-05-20 20:54:50 +00:00
|
|
|
const remoteRepo = `https://${process.env.GITHUB_ACTOR}:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`;
|
2020-09-06 11:13:52 +00:00
|
|
|
if (process.env['INPUT_SKIP-TAG'] !== 'true') {
|
2021-08-07 23:35:03 +00:00
|
|
|
await runInWorkspace('git', ['tag', newVersion]);
|
2021-12-09 03:20:01 +00:00
|
|
|
if (process.env['INPUT_SKIP-PUSH'] !== 'true') {
|
|
|
|
await runInWorkspace('git', ['push', remoteRepo, '--follow-tags']);
|
|
|
|
await runInWorkspace('git', ['push', remoteRepo, '--tags']);
|
|
|
|
}
|
2020-10-19 09:05:25 +00:00
|
|
|
} else {
|
2021-12-09 03:20:01 +00:00
|
|
|
if (process.env['INPUT_SKIP-PUSH'] !== 'true') {
|
|
|
|
await runInWorkspace('git', ['push', remoteRepo]);
|
|
|
|
}
|
2020-09-06 11:13:52 +00:00
|
|
|
}
|
2019-10-26 19:06:00 +00:00
|
|
|
} catch (e) {
|
2021-08-08 01:28:27 +00:00
|
|
|
logError(e);
|
2021-08-07 23:35:03 +00:00
|
|
|
exitFailure('Failed to bump version');
|
|
|
|
return;
|
2019-10-26 19:06:00 +00:00
|
|
|
}
|
2021-08-07 23:35:03 +00:00
|
|
|
exitSuccess('Version bumped!');
|
|
|
|
})();
|
|
|
|
|
|
|
|
function getPackageJson() {
|
|
|
|
const pathToPackage = path.join(workspace, 'package.json');
|
|
|
|
if (!existsSync(pathToPackage)) throw new Error("package.json could not be found in your project's root.");
|
2021-08-08 01:28:27 +00:00
|
|
|
return require(pathToPackage);
|
2021-08-07 23:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function exitSuccess(message) {
|
2021-08-08 01:28:27 +00:00
|
|
|
console.info(`✔ success ${message}`);
|
2021-08-07 23:35:03 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exitFailure(message) {
|
2021-08-08 01:28:27 +00:00
|
|
|
logError(message);
|
2021-08-07 23:35:03 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2021-08-08 01:28:27 +00:00
|
|
|
function logError(error) {
|
|
|
|
console.error(`✖ fatal ${error.stack || error}`);
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:35:03 +00:00
|
|
|
function runInWorkspace(command, args) {
|
2021-08-08 01:28:27 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const child = spawn(command, args, { cwd: workspace });
|
|
|
|
let isDone = false;
|
|
|
|
const errorMessages = [];
|
|
|
|
child.on('error', (error) => {
|
|
|
|
if (!isDone) {
|
|
|
|
isDone = true;
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
child.stderr.on('data', (chunk) => errorMessages.push(chunk));
|
|
|
|
child.on('exit', (code) => {
|
|
|
|
if (!isDone) {
|
|
|
|
if (code === 0) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(`${errorMessages.join('')}${EOL}${command} exited with code ${code}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//return execa(command, args, { cwd: workspace });
|
2021-08-07 23:35:03 +00:00
|
|
|
}
|