55 branches :
66 - master
77 - v2.0.0-alpha
8+ tags :
9+ - ' v*'
810 pull_request :
911 branches : [master]
1012 workflow_dispatch :
@@ -25,75 +27,47 @@ jobs:
2527
2628 publish :
2729 needs : test
28- if : github.event_name == 'push'
30+ if : startsWith( github.ref, 'refs/tags/v')
2931 runs-on : ubuntu-latest
3032 permissions :
31- contents : write
33+ contents : read
3234 id-token : write
3335 steps :
3436 - uses : actions/checkout@v4
35- with :
36- token : ${{ secrets.GITHUB_TOKEN }}
37-
3837 - uses : actions/setup-node@v4
3938 with :
4039 node-version : 20
4140 registry-url : https://registry.npmjs.org
42-
4341 - run : npm ci
4442
45- # Determine dist-tag from branch
43+ # Determine dist-tag from version string
44+ # v2.0.0-alpha.3 → alpha, v2.0.0-beta.1 → beta, v2.0.0 → latest
4645 - name : Determine npm tag
4746 id : tag
4847 run : |
49- if [ "${{ github.ref }}" = "refs/heads/master" ]; then
50- echo "dist_tag=latest" >> $GITHUB_OUTPUT
51- else
48+ VERSION=${GITHUB_REF#refs/tags/v}
49+ if echo "$VERSION" | grep -q "alpha"; then
5250 echo "dist_tag=alpha" >> $GITHUB_OUTPUT
51+ elif echo "$VERSION" | grep -q "beta"; then
52+ echo "dist_tag=beta" >> $GITHUB_OUTPUT
53+ elif echo "$VERSION" | grep -q "rc"; then
54+ echo "dist_tag=next" >> $GITHUB_OUTPUT
55+ else
56+ echo "dist_tag=latest" >> $GITHUB_OUTPUT
5357 fi
58+ echo "version=$VERSION" >> $GITHUB_OUTPUT
5459
55- # Auto-increment version if it already exists on npm
56- - name : Bump version if needed
60+ # Verify package.json version matches the tag
61+ - name : Verify version
5762 run : |
58- CURRENT=$(node -p "require('./package.json').version")
59- TAG=${{ steps.tag.outputs.dist_tag }}
60-
61- # Check if this exact version is already published
62- if npm view "allow2@${CURRENT}" version 2>/dev/null; then
63- echo "Version ${CURRENT} already published, bumping prerelease..."
64- # Get the highest published alpha version
65- LATEST_ALPHA=$(npm view allow2 versions --json 2>/dev/null | node -p "
66- JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'))
67- .filter(v => v.startsWith('2.0.0-alpha'))
68- .sort((a,b) => {
69- const na = parseInt(a.split('.').pop()) || 0;
70- const nb = parseInt(b.split('.').pop()) || 0;
71- return nb - na;
72- })[0] || '2.0.0-alpha.0'
73- ")
74- # Increment: 2.0.0-alpha.N → 2.0.0-alpha.(N+1)
75- NEXT=$(node -p "
76- const parts = '${LATEST_ALPHA}'.split('.');
77- const n = parseInt(parts.pop()) || 0;
78- parts.concat([n + 1]).join('.')
79- ")
80- echo "Bumping to ${NEXT}"
81- npm version "${NEXT}" --no-git-tag-version
82- else
83- echo "Version ${CURRENT} not yet published, using as-is"
63+ PKG_VERSION=$(node -p "require('./package.json').version")
64+ TAG_VERSION=${{ steps.tag.outputs.version }}
65+ if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then
66+ echo "::error::package.json version ($PKG_VERSION) does not match tag (v$TAG_VERSION)"
67+ exit 1
8468 fi
8569
8670 - name : Publish to npm
8771 run : npm publish --access public --tag ${{ steps.tag.outputs.dist_tag }}
8872 env :
8973 NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
90-
91- # Commit the version bump back so next run knows the current version
92- - name : Push version bump
93- run : |
94- PUBLISHED=$(node -p "require('./package.json').version")
95- git config user.name "github-actions[bot]"
96- git config user.email "github-actions[bot]@users.noreply.github.com"
97- git add package.json package-lock.json
98- git diff --cached --quiet || git commit -m "chore: bump version to ${PUBLISHED} [skip ci]"
99- git push
0 commit comments