Skip to content

Commit 18ea2e8

Browse files
feat: streamline npm publish workflow and add version validation
1 parent 9e31fbe commit 18ea2e8

1 file changed

Lines changed: 48 additions & 7 deletions

File tree

.github/workflows/publish.yml

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
name: Publish to npm
22

33
on:
4-
push:
5-
branches: [master]
64
release:
75
types: [published]
86
workflow_dispatch:
@@ -13,8 +11,8 @@ permissions:
1311
jobs:
1412
publish:
1513
runs-on: ubuntu-latest
16-
# Only publish if it's a push to master or a release
17-
if: github.event_name == 'release' || github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/master')
14+
# Publish only for released tags or manual dispatches.
15+
if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'
1816

1917
steps:
2018
- uses: actions/checkout@v4
@@ -29,15 +27,58 @@ jobs:
2927
- name: Install dependencies
3028
run: npm ci
3129

30+
- name: Validate version alignment
31+
run: |
32+
PACKAGE_VERSION=$(node -p "require('./package.json').version")
33+
SERVER_VERSION=$(node -p "require('./server.json').version")
34+
CONFIG_VERSION=$(node -e "const fs=require('fs'); const s=fs.readFileSync('./src/config.ts','utf8'); const m=s.match(/export const SERVER_VERSION = \"([^\"]+)\";/); if(!m){process.exit(1)}; console.log(m[1]);")
35+
36+
echo "package.json version: ${PACKAGE_VERSION}"
37+
echo "server.json version: ${SERVER_VERSION}"
38+
echo "src/config.ts version: ${CONFIG_VERSION}"
39+
40+
if [ "${PACKAGE_VERSION}" != "${SERVER_VERSION}" ] || [ "${PACKAGE_VERSION}" != "${CONFIG_VERSION}" ]; then
41+
echo "Version mismatch detected. Ensure package.json, server.json, and src/config.ts are identical."
42+
exit 1
43+
fi
44+
45+
- name: Validate release tag matches package version
46+
if: github.event_name == 'release'
47+
run: |
48+
PACKAGE_VERSION=$(node -p "require('./package.json').version")
49+
RELEASE_TAG="${{ github.event.release.tag_name }}"
50+
EXPECTED_TAG="v${PACKAGE_VERSION}"
51+
52+
echo "release tag: ${RELEASE_TAG}"
53+
echo "expected tag: ${EXPECTED_TAG}"
54+
55+
if [ "${RELEASE_TAG}" != "${EXPECTED_TAG}" ]; then
56+
echo "Release tag does not match package version."
57+
exit 1
58+
fi
59+
60+
- name: Check if version already exists on npm
61+
id: npm_check
62+
run: |
63+
PACKAGE_NAME=$(node -p "require('./package.json').name")
64+
PACKAGE_VERSION=$(node -p "require('./package.json').version")
65+
66+
if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then
67+
echo "already_published=true" >> "$GITHUB_OUTPUT"
68+
echo "Version ${PACKAGE_VERSION} is already published. Skipping publish."
69+
else
70+
echo "already_published=false" >> "$GITHUB_OUTPUT"
71+
echo "Version ${PACKAGE_VERSION} is not published yet."
72+
fi
73+
3274
- name: Build
3375
run: npm run build
3476

3577
- name: Test
3678
run: npm test
3779

3880
- name: Publish to npm
39-
run: |
40-
# Try to publish, but don't fail if the version is already published
41-
npm publish --access public || echo "Version already published or publish failed. Skipping."
81+
if: steps.npm_check.outputs.already_published == 'false'
82+
run: npm publish --access public
4283
env:
4384
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)