11name : Publish Package to npm
22
3- # Trigger this workflow whenever a new release is published
43on :
54 push :
65 branches :
76 - main
8- release :
9- types : [published]
107
11- # Grant write permissions to the repository contents so we can push version updates
128permissions :
139 contents : write
1410
@@ -17,49 +13,38 @@ jobs:
1713 runs-on : ubuntu-latest
1814
1915 steps :
20- # Step 1: Check out the repository’s code at the default branch
21- # This makes your code available for subsequent steps like installing dependencies and running tests.
2216 - uses : actions/checkout@v4
23- with :
24- token : ${{ secrets.GITHUB_TOKEN }}
25- ref : ${{ github.event.repository.default_branch }}
2617
27- # Step 2: Set up a Node.js environment (Node 20.x) and configure npm to use the official registry
28- # This ensures we have the right Node.js version and a proper registry URL for installs and publishing.
2918 - name : Setup Node.js
3019 uses : actions/setup-node@v4
3120 with :
3221 node-version : ' 20.x'
3322 registry-url : ' https://registry.npmjs.org'
3423
35- # Step 5: Update package.json to match the release tag
36- # The release tag (e.g., v1.0.1) is extracted, and npm version sets package.json version accordingly.
37- # The --no-git-tag-version flag ensures npm doesn't create its own tags.
38- # This step keeps package.json's version aligned with the release tag you just created.
39- - name : Update package.json with release tag
24+ # Extract the version from package.json
25+ - name : Extract version from package.json
26+ id : extract_version
27+ run : |
28+ VERSION=$(node -p "require('./package.json').version")
29+ echo "VERSION=$VERSION" >> $GITHUB_ENV
30+
31+ # Update package.json with the extracted version
32+ - name : Update package.json with version
4033 run : |
41- TAG="${{ github.event.release.tag_name }}"
42- echo "Updating package.json version to $TAG"
43- npm version "$TAG" --no-git-tag-version
44-
45- # Step 6: Commit and push the updated package.json and package-lock.json back to the repo
46- # This ensures your repository always reflects the exact version published.
47- # We use the GITHUB_TOKEN to authenticate and the granted write permissions to push changes.
34+ echo "Updating package.json version to $VERSION"
35+ npm version "$VERSION" --no-git-tag-version
36+
4837 - name : Commit and push version update
4938 run : |
50- TAG="${{ github.event.release.tag_name }}"
5139 git config user.name "github-actions"
5240 git config user.email "[email protected] " 5341 git add package.json package-lock.json
54- git commit -m "Update package.json to version $TAG "
55- git push origin ${{ github.event.repository.default_branch }}
42+ git commit -m "Update package.json to version $VERSION "
43+ git push origin main
5644 env :
5745 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
5846
59- # Step 7: Publish the new version to npm
60- # The NODE_AUTH_TOKEN is your npm access token stored as a secret.
61- # npm publish --access public makes the package available to anyone on npm.
6247 - name : Publish to npm
6348 run : npm publish --access public
6449 env :
65- NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
50+ NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
0 commit comments