1
1
name : Publish Package to npm
2
2
3
- # Trigger this workflow whenever a new release is published
4
3
on :
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
11
7
12
8
jobs :
13
9
publish :
10
+ name : Publish to NPM
14
11
runs-on : ubuntu-latest
15
-
12
+
16
13
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
23
16
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.
26
17
- name : Setup Node.js
27
18
uses : actions/setup-node@v4
28
19
with :
29
- node-version : ' 20.x '
30
- registry-url : ' https://registry.npmjs.org'
20
+ node-version : 18
21
+ registry-url : ' https://registry.npmjs.org/ '
31
22
32
- # Step 3: Install dependencies using npm ci
33
- # This ensures a clean, reproducible installation based on package-lock.json.
34
23
- name : Install dependencies
35
- run : npm ci
24
+ run : npm install
36
25
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.
39
26
# - name: Run tests
40
27
# run: npm test
41
28
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 "[email protected] "
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
65
31
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
70
33
run : npm publish --access public
71
34
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