Skip to content

Commit e7962b3

Browse files
committed
ci(gh-actions): run publish package only when a new tag published
1 parent f211bed commit e7962b3

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
name: Node.js CI
22

33
on:
4-
push:
5-
branches: ['main']
4+
release:
5+
types: [published]
66

77
jobs:
88
publish-npm:
99
runs-on: ubuntu-latest
1010

1111
steps:
12-
- uses: actions/checkout@v3
13-
- uses: actions/setup-node@v3
12+
- name: Git checkout
13+
uses: actions/checkout@v3
14+
15+
- name: Setup Node.js (v20.x)
16+
uses: actions/setup-node@v3
1417
with:
1518
node-version: 20.x
1619
registry-url: https://registry.npmjs.org/
17-
- run: npm ci
18-
- run: npm run build --if-present
20+
21+
- name: Build package
22+
run: |
23+
npm ci
24+
npm run build --if-present
25+
26+
- name: Bump package version
27+
run: |
28+
git config --global user.name="GitHub"
29+
git config --global user.email="<noreply@github.com>"
30+
RELEASE=${{ github.event.release.name}}
31+
NEXT_VERSION=${RELEASE} node ./scripts/tasks/bump-version.js
32+
git add package.json
33+
git commit -m "chore: bump package version to ${RELEASE}"
34+
1935
- run: npm publish --access=public
2036
env:
2137
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}

scripts/tasks/bump-version.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { exec } = require('child_process');
2+
const package = require('../../package.json');
3+
4+
const version = {
5+
old: package.version,
6+
next: null,
7+
};
8+
9+
if (process.env.NEXT_VERSION === null) {
10+
console.log('NEXT_VERSION could not be `null`.');
11+
process.exit(1);
12+
}
13+
14+
version.next = process.env.NEXT_VERSION;
15+
16+
const cmd = `sed -i "s/${version.old}/${version.next}/" ./package.json`;
17+
18+
exec(cmd, (error, stdout) => {
19+
if (error) {
20+
console.log(error.message);
21+
process.exit(error.code);
22+
}
23+
console.log(stdout);
24+
process.exit(0);
25+
});

0 commit comments

Comments
 (0)