2019-10-26 19:06:00 +00:00
|
|
|
const { Toolkit } = require('actions-toolkit')
|
|
|
|
const { execSync } = require('child_process')
|
|
|
|
|
2020-01-13 14:04:50 +00:00
|
|
|
// Change working directory if user defined PACKAGEJSON_DIR
|
|
|
|
if (process.env.PACKAGEJSON_DIR) {
|
2020-02-18 09:53:44 +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
|
|
|
}
|
|
|
|
|
2019-10-26 19:06:00 +00:00
|
|
|
// Run your GitHub Action!
|
|
|
|
Toolkit.run(async tools => {
|
|
|
|
const pkg = tools.getPackageJSON()
|
|
|
|
const event = tools.context.payload
|
|
|
|
|
|
|
|
const messages = event.commits.map(commit => commit.message + '\n' + commit.body)
|
|
|
|
|
|
|
|
const commitMessage = 'version bump to'
|
|
|
|
const isVersionBump = messages.map(message => message.toLowerCase().includes(commitMessage)).includes(true)
|
|
|
|
if (isVersionBump) {
|
2019-10-26 21:00:38 +00:00
|
|
|
tools.exit.success('No action necessary!')
|
2019-10-26 19:06:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let version = 'patch'
|
2020-02-13 19:36:18 +00:00
|
|
|
if (messages.map(message => message.includes('BREAKING CHANGE') || message.includes('major')).includes(true)) {
|
2019-10-26 19:06:00 +00:00
|
|
|
version = 'major'
|
2020-02-13 19:36:18 +00:00
|
|
|
} else if (messages.map(
|
|
|
|
message => message.toLowerCase().startsWith('feat') || message.toLowerCase().includes('minor')).includes(true)) {
|
2019-10-26 19:06:00 +00:00
|
|
|
version = 'minor'
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const current = pkg.version.toString()
|
2019-10-26 19:42:48 +00:00
|
|
|
// set git user
|
2020-04-28 16:53:02 +00:00
|
|
|
await tools.runInWorkspace('git', ['config', 'user.name', `"${process.env.GITHUB_USER || 'Automated Version Bump'}"`])
|
|
|
|
await tools.runInWorkspace('git', ['config', 'user.email', `"${process.env.GITHUB_EMAIL || 'gh-action-bump-version@users.noreply.github.com'}"`])
|
2019-10-26 19:42:48 +00:00
|
|
|
|
2019-10-26 23:54:32 +00:00
|
|
|
const currentBranch = /refs\/[a-zA-Z]+\/(.*)/.exec(process.env.GITHUB_REF)[1]
|
2020-04-03 10:48:15 +00:00
|
|
|
console.log('currentBranch:', currentBranch)
|
2019-10-27 19:07:29 +00:00
|
|
|
|
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
|
2019-10-27 19:07:29 +00:00
|
|
|
await tools.runInWorkspace('npm',
|
|
|
|
['version', '--allow-same-version=true', '--git-tag-version=false', current])
|
|
|
|
console.log('current:', current, '/', 'version:', version)
|
|
|
|
let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim()
|
2020-01-17 12:07:27 +00:00
|
|
|
await tools.runInWorkspace('git', ['commit', '-a', '-m', `ci: ${commitMessage} ${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
|
2019-10-26 23:52:27 +00:00
|
|
|
await tools.runInWorkspace('git', ['checkout', currentBranch])
|
2019-10-26 19:26:43 +00:00
|
|
|
await tools.runInWorkspace('npm',
|
2019-10-26 19:35:29 +00:00
|
|
|
['version', '--allow-same-version=true', '--git-tag-version=false', current])
|
2019-10-26 19:06:00 +00:00
|
|
|
console.log('current:', current, '/', 'version:', version)
|
2019-10-27 19:07:29 +00:00
|
|
|
newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim()
|
2019-11-20 15:00:55 +00:00
|
|
|
newVersion = `${process.env['INPUT_TAG-PREFIX']}${newVersion}`
|
2019-10-26 19:06:00 +00:00
|
|
|
console.log('new version:', newVersion)
|
2020-04-03 23:39:59 +00:00
|
|
|
try {
|
|
|
|
// to support "actions/checkout@v1"
|
|
|
|
await tools.runInWorkspace('git', ['commit', '-a', '-m', `ci: ${commitMessage} ${newVersion}`])
|
|
|
|
} catch (e) {
|
|
|
|
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 11:04:35 +00:00
|
|
|
|
2019-10-26 19:06:00 +00:00
|
|
|
const remoteRepo = `https://${process.env.GITHUB_ACTOR}:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`
|
2019-10-27 00:00:31 +00:00
|
|
|
// console.log(Buffer.from(remoteRepo).toString('base64'))
|
2019-10-26 19:59:37 +00:00
|
|
|
await tools.runInWorkspace('git', ['tag', newVersion])
|
2019-10-26 20:25:37 +00:00
|
|
|
await tools.runInWorkspace('git', ['push', remoteRepo, '--follow-tags'])
|
|
|
|
await tools.runInWorkspace('git', ['push', remoteRepo, '--tags'])
|
2019-10-26 19:06:00 +00:00
|
|
|
} catch (e) {
|
2019-10-26 19:26:43 +00:00
|
|
|
tools.log.fatal(e)
|
|
|
|
tools.exit.failure('Failed to bump version')
|
2019-10-26 19:06:00 +00:00
|
|
|
}
|
|
|
|
tools.exit.success('Version bumped!')
|
|
|
|
})
|