gh-action-bump-version/index.js

52 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-10-26 19:06:00 +00:00
const { Toolkit } = require('actions-toolkit')
const { execSync } = require('child_process')
// 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'
if (messages.map(message => message.includes('BREAKING CHANGE')).includes(true)) {
version = 'major'
} else if (messages.map(message => message.toLowerCase().startsWith('feat')).includes(true)) {
version = 'minor'
}
try {
const current = pkg.version.toString()
2019-10-26 19:42:48 +00:00
// set git user
2019-10-26 20:32:18 +00:00
await tools.runInWorkspace('git', ['config', 'user.name', '"Automated Version Bump"'])
2019-10-26 19:42:48 +00:00
await tools.runInWorkspace('git', ['config', 'user.email', '"gh-action-bump-version@users.noreply.github.com"'])
2019-10-26 23:54:32 +00:00
const currentBranch = /refs\/[a-zA-Z]+\/(.*)/.exec(process.env.GITHUB_REF)[1]
2019-10-26 23:52:27 +00:00
console.log('currentBranch:', currentBranch)
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-26 19:53:54 +00:00
const newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim()
2019-10-26 19:06:00 +00:00
console.log('new version:', newVersion)
2019-10-26 19:35:29 +00:00
await tools.runInWorkspace('git', ['commit', '-a', '-m', `"ci: ${commitMessage} ${newVersion}"`])
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!')
})