11name : Publish Package to npm
22
3- # Trigger this workflow whenever a new release is published
43on :
5- release :
6- types : [published]
7-
8- # Grant write permissions to the repository contents so we can push version updates
9- permissions :
10- contents : write
4+ push :
5+ tags :
6+ - ' v*' # Trigger only when pushing tags like v1.0.0
117
128jobs :
139 publish :
10+ name : Publish to NPM
1411 runs-on : ubuntu-latest
15-
12+
1613 steps :
17- # Step 1: Check out the repository’s code at the default branch
18- # This makes your code available for subsequent steps like installing dependencies and running tests.
19- - uses : actions/checkout@v4
20- with :
21- token : ${{ secrets.GITHUB_TOKEN }}
22- ref : ${{ github.event.repository.default_branch }}
14+ - name : Checkout repository
15+ uses : actions/checkout@v4
2316
24- # Step 2: Set up a Node.js environment (Node 20.x) and configure npm to use the official registry
25- # This ensures we have the right Node.js version and a proper registry URL for installs and publishing.
2617 - name : Setup Node.js
2718 uses : actions/setup-node@v4
2819 with :
29- node-version : ' 20.x '
30- registry-url : ' https://registry.npmjs.org'
20+ node-version : 18
21+ registry-url : ' https://registry.npmjs.org/ '
3122
32- # Step 3: Install dependencies using npm ci
33- # This ensures a clean, reproducible installation based on package-lock.json.
3423 - name : Install dependencies
35- run : npm ci
24+ run : npm install
3625
37- # Step 4: Run your test suite (using the "test" script from package.json)
38- # If tests fail, the workflow will stop here and not publish a broken version.
3926 # - name: Run tests
4027 # run: npm test
4128
42- # Step 5: Update package.json to match the release tag
43- # The release tag (e.g., v1.0.1) is extracted, and npm version sets package.json version accordingly.
44- # The --no-git-tag-version flag ensures npm doesn't create its own tags.
45- # This step keeps package.json's version aligned with the release tag you just created.
46- - name : Update package.json with release tag
47- run : |
48- TAG="${{ github.event.release.tag_name }}"
49- echo "Updating package.json version to $TAG"
50- npm version "$TAG" --no-git-tag-version
51-
52- # Step 6: Commit and push the updated package.json and package-lock.json back to the repo
53- # This ensures your repository always reflects the exact version published.
54- # We use the GITHUB_TOKEN to authenticate and the granted write permissions to push changes.
55- - name : Commit and push version update
56- run : |
57- TAG="${{ github.event.release.tag_name }}"
58- git config user.name "github-actions"
59- git config user.email "github-actions@github.com"
60- git add package.json package-lock.json
61- git commit -m "Update package.json to version $TAG"
62- git push origin ${{ github.event.repository.default_branch }}
63- env :
64- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
29+ - name : Build package
30+ run : npm run build
6531
66- # Step 7: Publish the new version to npm
67- # The NODE_AUTH_TOKEN is your npm access token stored as a secret.
68- # npm publish --access public makes the package available to anyone on npm.
69- - name : Publish to npm
32+ - name : Publish to NPM
7033 run : npm publish --access public
7134 env :
72- NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
35+ NODE_AUTH_TOKEN : ${{ secrets.NPM_TOKEN }}
36+
37+ - name : Create GitHub Release
38+ uses : softprops/action-gh-release@v2
39+ with :
40+ tag_name : ${{ github.ref }}
41+ generate_release_notes : true
0 commit comments