Skip to content

Commit 2b3dab8

Browse files
committed
feat(ci): auto bump version on merge
1 parent 8a7959a commit 2b3dab8

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

.github/workflows/bump_version.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Bump Package Version
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
types: [closed]
7+
8+
jobs:
9+
bump-version:
10+
if: github.event.pull_request.merged == true
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Setup Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version: 20
25+
26+
- name: Install dependencies
27+
run: yarn install
28+
29+
- name: Bump version
30+
run: yarn bump-version
31+
32+
- name: Commit and push changes
33+
run: |
34+
git config --global user.name "github-actions[bot]"
35+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
36+
git add package.json
37+
git diff --cached --quiet && echo "No version change." || git commit -m "chore(release): bump version [skip ci]"
38+
git push

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"prepare": "yarn ncc build src/ActionMain.ts -o dist",
1313
"changelog": "ts-node scripts/generateChangelog.ts",
1414
"build:dist": "ncc build src/ActionMain.ts -o dist",
15-
"check-version": "ts-node scripts/checkVersionTag.ts"
15+
"check-version": "ts-node scripts/checkVersionTag.ts",
16+
"bump-version": "ts-node scripts/bumpVersion.ts"
1617
},
1718
"keywords": [
1819
"github-action",

scripts/bumpVersion.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import fs from 'fs';
2+
3+
/**
4+
* Increment the patch component of a semantic version string.
5+
*
6+
* @param version - Existing semantic version (e.g., "1.2.3").
7+
* @returns New version string with the patch number incremented.
8+
*/
9+
function bumpPatchVersion(version: string): string {
10+
const [major, minor, patch] = version.split('.').map(Number);
11+
return `${major}.${minor}.${patch + 1}`;
12+
}
13+
14+
/**
15+
* Read package.json, update its version field, and persist the change.
16+
*
17+
* @param filePath - Path to the package.json file.
18+
*/
19+
function updatePackageVersion(filePath: string): void {
20+
const pkg = JSON.parse(fs.readFileSync(filePath, 'utf8')) as { version: string };
21+
const newVersion = bumpPatchVersion(pkg.version);
22+
23+
// Update version in memory
24+
pkg.version = newVersion;
25+
26+
// Write updated package.json back to disk, preserving formatting
27+
fs.writeFileSync(filePath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
28+
console.log(`\u2705 Updated version to ${newVersion}`);
29+
}
30+
31+
(function main() {
32+
updatePackageVersion('package.json');
33+
})();

0 commit comments

Comments
 (0)