This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
forked from ChmaraX/gh-action-bump-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (69 loc) · 3.51 KB
/
index.js
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
const { Toolkit } = require('actions-toolkit')
const { execSync } = require('child_process')
// Change working directory if user defined PACKAGEJSON_DIR
if (process.env.PACKAGEJSON_DIR) {
process.env.GITHUB_WORKSPACE = `${process.env.GITHUB_WORKSPACE}/${process.env.PACKAGEJSON_DIR}`
process.chdir(process.env.GITHUB_WORKSPACE)
}
// 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) {
tools.exit.success('No action necessary!')
return
}
let version = 'patch'
if (messages.map(message => message.includes('MAJOR-RELEASE')).includes(true)) {
version = 'major'
} else if (messages.map(
message => message.includes('MINOR-RELEASE')).includes(true)) {
version = 'minor'
}
try {
const current = pkg.version.toString()
// set git user
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 || '[email protected]'}"`])
const currentBranch = /refs\/[a-zA-Z]+\/(.*)/.exec(process.env.GITHUB_REF)[1]
console.log('currentBranch:', currentBranch)
// do it in the current checked out github branch (DETACHED HEAD)
// important for further usage of the package.json version
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()
await tools.runInWorkspace('git', ['commit', '-a', '-m', `ci: ${commitMessage} ${newVersion}`])
// now go to the actual branch to perform the same versioning
await tools.runInWorkspace('git', ['checkout', currentBranch])
await tools.runInWorkspace('npm',
['version', '--allow-same-version=true', '--git-tag-version=false', current])
console.log('current:', current, '/', 'version:', version)
newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim()
newVersion = `${process.env['INPUT_TAG-PREFIX']}${newVersion}`
console.log('new version:', newVersion)
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"')
}
const remoteRepo = `https://${process.env.GITHUB_ACTOR}:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`
// console.log(Buffer.from(remoteRepo).toString('base64'))
if (version !== 'patch') {
await tools.runInWorkspace('git', ['tag', newVersion])
await tools.runInWorkspace('git', ['push', remoteRepo, '--follow-tags'])
await tools.runInWorkspace('git', ['push', remoteRepo, '--tags'])
} else {
await tools.runInWorkspace('git', ['push', remoteRepo])
}
} catch (e) {
tools.log.fatal(e)
tools.exit.failure('Failed to bump version')
}
tools.exit.success('Version bumped!')
})