Skip to content

Commit e673b2a

Browse files
ci: properly derive npm tag from git tag
Only the latest git tag should be published as latest to npm. Other tags will be published with either pre-vX.Y or lts-vX.Y as their npm tagname. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch master # Your branch is up to date with 'origin/master'. # # Changes to be committed: # modified: .github/workflows/publish.yml #
1 parent 1d20383 commit e673b2a

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

.github/workflows/publish.yml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- uses: actions/checkout@v6
12-
with:
13-
filter: blob:limit=2m
12+
- name: Ensure tag and package version match
13+
run: |
14+
TAG_VERSION=${GITHUB_REF_NAME#v}
15+
NPM_VERSION=$(jq -r .version package.json)
16+
if [[ "$TAG_VERSION" != "$NPM_VERSION" ]]; then
17+
echo "Tag $TAG_VERSION does not match package.json#.version $NPM_VERSION; aborting."
18+
exit 1
19+
fi
1420
- uses: actions/setup-node@v6
1521
with:
1622
node-version: "20"
@@ -33,6 +39,10 @@ jobs:
3339
runs-on: ubuntu-latest
3440
needs: [build-package]
3541
steps:
42+
- uses: actions/checkout@v6
43+
with:
44+
fetch-tags: true
45+
filter: "blob:none"
3646
- uses: actions/setup-node@v6
3747
with:
3848
node-version: "24"
@@ -41,7 +51,18 @@ jobs:
4151
uses: actions/download-artifact@v5
4252
with:
4353
name: node-package
44-
- run: npm publish "${{ github.workspace }}/package.tgz" --access public --provenance
54+
- name: Publish pre-release to npm registry
55+
if: ${{ contains(github.ref_name, '-') }}
56+
run: npm publish "${{ github.workspace }}/package.tgz" --tag "pre-${GITHUB_REF_NAME%.*-*}" --access public --provenance
57+
- name: Publish release to npm registry
58+
if: ${{ !contains(github.ref_name, '-') }}
59+
run: |
60+
if [[ "${{ github.ref_name }}" == "$(git tag | grep -v - - | sort -V -r | head -n 1)" ]]; then
61+
TAG_NAME="latest"
62+
else
63+
TAG_NAME="lts-${GITHUB_REF_NAME%.*}"
64+
fi
65+
npm publish "${{ github.workspace }}/package.tgz" --tag "$TAG_NAME" --access public --provenance
4566
4667
create-release:
4768
name: Create the GitHub Release

0 commit comments

Comments
 (0)