added option to skip commit after a bump

This commit is contained in:
Etienne Magnier 2021-11-30 15:55:02 +01:00
parent 473e4456bc
commit 5687ff9bf5
No known key found for this signature in database
GPG Key ID: 0AE58D02FD0AF08B
6 changed files with 72 additions and 6 deletions

View File

@ -54,6 +54,16 @@ Remove the 'actions/setup-node@v1' step from your action.yml file
skip-tag: 'true' skip-tag: 'true'
``` ```
**skip-commit:** No commit is made after the version is bumped (optional). Example:
```yaml
- name: 'Automated Version Bump'
uses: 'phips28/gh-action-bump-version@master'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
skip-commit: 'true'
```
**default:** Set a default version bump to use (optional - defaults to patch). Example: **default:** Set a default version bump to use (optional - defaults to patch). Example:
```yaml ```yaml
- name: 'Automated Version Bump' - name: 'Automated Version Bump'

View File

@ -30,6 +30,10 @@ inputs:
description: 'Avoid to add a TAG to the version update commit' description: 'Avoid to add a TAG to the version update commit'
default: 'false' default: 'false'
required: false required: false
skip-commit:
description: 'Avoid to add a commit after the version is bumped'
default: 'false'
required: false
PACKAGEJSON_DIR: PACKAGEJSON_DIR:
description: 'Custom dir to the package' description: 'Custom dir to the package'
default: '' default: ''

View File

@ -146,7 +146,9 @@ const workspace = process.env.GITHUB_WORKSPACE;
console.log('current:', current, '/', 'version:', version); console.log('current:', current, '/', 'version:', version);
let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, ''); let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, '');
newVersion = `${tagPrefix}${newVersion}`; newVersion = `${tagPrefix}${newVersion}`;
if (process.env['INPUT_SKIP-COMMIT'] !== 'true') {
await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]); await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]);
}
// now go to the actual branch to perform the same versioning // now go to the actual branch to perform the same versioning
if (isPullRequest) { if (isPullRequest) {
@ -161,7 +163,9 @@ const workspace = process.env.GITHUB_WORKSPACE;
console.log(`::set-output name=newTag::${newVersion}`); console.log(`::set-output name=newTag::${newVersion}`);
try { try {
// to support "actions/checkout@v1" // to support "actions/checkout@v1"
if (process.env['INPUT_SKIP-COMMIT'] !== 'true') {
await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]); await runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]);
}
} catch (e) { } catch (e) {
console.warn( console.warn(
'git commit failed because you are using "actions/checkout@v2"; ' + 'git commit failed because you are using "actions/checkout@v2"; ' +

3
package-lock.json generated
View File

@ -5,7 +5,8 @@
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"version": "9.0.7", "name": "gh-action-bump-version",
"version": "9.0.10",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/jest": "^27.0.1", "@types/jest": "^27.0.1",

View File

@ -155,6 +155,27 @@ suites:
expected: expected:
version: 4.1.3 version: 4.1.3
branch: other-branch branch: other-branch
- name: skip-commit
yaml:
name: Bump Version (Skip Commit)
on:
push:
jobs:
bump-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- id: version-bump
uses: ./action
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
skip-commit: true
tests:
- message: no keywords
expected:
version: 4.1.4
message: 'ci: version bump to 4.1.3'
actionFiles: actionFiles:
- index.js - index.js

View File

@ -99,7 +99,12 @@ async function getMostRecentWorkflowRunDate() {
return date; return date;
} }
function generateExpectationText({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) { function generateExpectationText({
version: expectedVersion,
tag: expectedTag,
branch: expectedBranch,
message: expectedMessage,
}) {
const results = [`- **Version:** ${expectedVersion}`]; const results = [`- **Version:** ${expectedVersion}`];
if (expectedTag) { if (expectedTag) {
results.push(`- **Tag:** ${expectedTag}`); results.push(`- **Tag:** ${expectedTag}`);
@ -107,10 +112,18 @@ function generateExpectationText({ version: expectedVersion, tag: expectedTag, b
if (expectedBranch) { if (expectedBranch) {
results.push(`- **Branch:** ${expectedBranch}`); results.push(`- **Branch:** ${expectedBranch}`);
} }
if (expectedMessage) {
results.push(`- **Message:** ${expectedMessage}`);
}
return results.join('\n'); return results.join('\n');
} }
async function assertExpectation({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) { async function assertExpectation({
version: expectedVersion,
tag: expectedTag,
branch: expectedBranch,
message: expectedMessage,
}) {
if (expectedTag === undefined) { if (expectedTag === undefined) {
expectedTag = expectedVersion; expectedTag = expectedVersion;
} }
@ -119,9 +132,17 @@ async function assertExpectation({ version: expectedVersion, tag: expectedTag, b
await git('checkout', expectedBranch); await git('checkout', expectedBranch);
} }
await git('pull'); await git('pull');
const [packageVersion, latestTag] = await Promise.all([getPackageJsonVersion(), getLatestTag()]); const [packageVersion, latestTag, latestMessage] = await Promise.all([
getPackageJsonVersion(),
getLatestTag(),
getLatestCommitMessage(),
]);
if (!expectedMessage) {
expectedMessage = latestMessage;
}
expect(packageVersion).toBe(expectedVersion); expect(packageVersion).toBe(expectedVersion);
expect(latestTag).toBe(expectedTag); expect(latestTag).toBe(expectedTag);
expect(latestMessage).toBe(expectedMessage);
if (expectedBranch) { if (expectedBranch) {
await git('checkout', 'main'); await git('checkout', 'main');
} }
@ -138,3 +159,8 @@ async function getLatestTag() {
const result = await git({ suppressOutput: true }, 'describe', '--tags', '--abbrev=0'); const result = await git({ suppressOutput: true }, 'describe', '--tags', '--abbrev=0');
return result.stdout; return result.stdout;
} }
async function getLatestCommitMessage() {
const result = await git({ suppressOutput: true }, 'show', '--no-patch', '--format=%s');
return result.stdout;
}