Skip to content

Commit f1b040e

Browse files
committed
ci: add automated test publishing on push
Update the npm-publish workflow to trigger on all branch pushes and publish test versions to npm with a 'test' tag. This includes a new helper script to calculate unique pre-release versions based on the branch name and existing registry versions.
1 parent 25ab10e commit f1b040e

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

.github/workflows/npm-publish.yml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name: Node.js build and publish package
33
on:
44
release:
55
types: [ created ]
6+
push:
7+
branches:
8+
- '**'
9+
workflow_dispatch:
610

711
jobs:
812
build:
@@ -24,6 +28,27 @@ jobs:
2428
node-version: 20
2529
registry-url: https://registry.npmjs.org/
2630
- run: npm i
27-
- run: npm publish
31+
32+
- name: Publish Release
33+
if: github.event_name == 'release'
34+
run: npm publish
2835
env:
29-
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
36+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
37+
38+
- name: Publish Test Version
39+
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
40+
env:
41+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
42+
run: |
43+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
44+
echo "Branch: $BRANCH_NAME"
45+
46+
# Determine the new version
47+
NEW_VERSION=$(node scripts/bump-version.js "$BRANCH_NAME")
48+
echo "New Version: $NEW_VERSION"
49+
50+
# Set the version in package.json
51+
npm version $NEW_VERSION --no-git-tag-version
52+
53+
# Publish with 'test' tag
54+
npm publish --tag test

scripts/bump-version.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { execSync } = require('child_process');
2+
const packageJson = require('../package.json');
3+
4+
const branchName = process.argv[2];
5+
if (!branchName) {
6+
console.error('Branch name is required');
7+
process.exit(1);
8+
}
9+
10+
const sanitizedBranch = branchName.replace(/[^a-zA-Z0-9]/g, '-').replace(/^-+|-+$/g, '');
11+
const baseVersion = packageJson.version;
12+
const versionPrefix = `${baseVersion}-${sanitizedBranch}`;
13+
14+
console.error(`Prefix: ${versionPrefix}`);
15+
16+
let versions = [];
17+
try {
18+
const stdout = execSync(`npm view ${packageJson.name} versions --json`, { stdio: ['pipe', 'pipe', 'ignore'] }).toString();
19+
versions = JSON.parse(stdout);
20+
if (!Array.isArray(versions)) {
21+
versions = [versions];
22+
}
23+
} catch (e) {
24+
console.error('Package not found or no versions. Starting from 1.');
25+
}
26+
27+
let maxIncrement = 0;
28+
29+
versions.forEach(v => {
30+
if (v.startsWith(versionPrefix)) {
31+
const suffix = v.slice(versionPrefix.length);
32+
if (suffix.startsWith('.')) {
33+
const numStr = suffix.substring(1);
34+
const num = parseInt(numStr, 10);
35+
if (!isNaN(num) && num > maxIncrement) {
36+
maxIncrement = num;
37+
}
38+
}
39+
}
40+
});
41+
42+
const nextVersion = `${versionPrefix}.${maxIncrement + 1}`;
43+
console.log(nextVersion);

0 commit comments

Comments
 (0)