Skip to content

Commit 00fe6f2

Browse files
Automatically publish dev builds after PR merges and tag builds (#158)
* Automatically publish dev builds after PR merges and tag builds * Review
1 parent c065dc7 commit 00fe6f2

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

.github/workflows/publish.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Publish to npm
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
tags:
7+
- 'v*.*.*'
8+
9+
permissions:
10+
contents: read
11+
id-token: write
12+
13+
jobs:
14+
publish:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v6
19+
20+
- name: Use Node.js
21+
uses: actions/setup-node@v6
22+
with:
23+
node-version: 24.x
24+
registry-url: https://registry.npmjs.org
25+
package-manager-cache: false
26+
27+
- run: npm ci
28+
- run: npm run build
29+
- run: npm run test
30+
31+
- name: Configure publish version
32+
id: publish
33+
run: |
34+
PACKAGE_NAME=$(node -p "require('./package.json').name")
35+
BASE_VERSION=$(node -p "require('./package.json').version")
36+
37+
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
38+
if [[ ! "$GITHUB_REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
39+
echo "::error::Release tags must match vX.X.X, got $GITHUB_REF_NAME."
40+
exit 1
41+
fi
42+
43+
PACKAGE_VERSION="${GITHUB_REF_NAME#v}"
44+
if [[ "$PACKAGE_VERSION" != "$BASE_VERSION" ]]; then
45+
echo "::error::Tag $GITHUB_REF_NAME does not match package.json version $BASE_VERSION."
46+
exit 1
47+
fi
48+
49+
DIST_TAG=latest
50+
else
51+
PACKAGE_VERSION="${BASE_VERSION}-dev.${GITHUB_RUN_NUMBER}.${GITHUB_RUN_ATTEMPT}"
52+
npm pkg set version="$PACKAGE_VERSION"
53+
DIST_TAG=dev
54+
fi
55+
56+
echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
57+
echo "package_version=$PACKAGE_VERSION" >> "$GITHUB_OUTPUT"
58+
echo "dist_tag=$DIST_TAG" >> "$GITHUB_OUTPUT"
59+
60+
if npm view "$PACKAGE_NAME@$PACKAGE_VERSION" version >/dev/null 2>&1; then
61+
echo "already_published=true" >> "$GITHUB_OUTPUT"
62+
else
63+
echo "already_published=false" >> "$GITHUB_OUTPUT"
64+
fi
65+
66+
- name: Publish to npm
67+
if: ${{ steps.publish.outputs.already_published == 'false' }}
68+
run: npm publish --provenance --access public --tag "${{ steps.publish.outputs.dist_tag }}"
69+
70+
- name: Skip already published version
71+
if: ${{ steps.publish.outputs.already_published == 'true' }}
72+
run: |
73+
echo "${{ steps.publish.outputs.package_name }}@${{ steps.publish.outputs.package_version }} is already published; skipping npm publish."

0 commit comments

Comments
 (0)