Skip to content

Commit fcb2ecd

Browse files
update git workflow
1 parent 3dcaad8 commit fcb2ecd

File tree

1 file changed

+103
-11
lines changed

1 file changed

+103
-11
lines changed

.github/workflows/npm-publish.yml

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,128 @@
1-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
1+
# 🚀 Release & Publish Workflow
2+
#
3+
# This GitHub Actions workflow automates the full release process:
4+
#
5+
# 1. On every push to the `main` branch:
6+
# - Bumps the version in `package.json` automatically (patch level: v1.0.6 → v1.0.7).
7+
# - Commits and pushes the new version + Git tag back to `main`.
8+
# - Creates a GitHub Release with the new tag.
9+
#
10+
# 2. Build Job:
11+
# - Runs `npm install` and `npm run build`.
12+
# - Archives the build output (`dist/`) as an artifact to pass to the next job.
13+
#
14+
# 3. Publish to NPM:
15+
# - Downloads the build artifact (`dist/`).
16+
# - Publishes it to the npm registry using the `NPM_AUTH_TOKEN` secret.
17+
#
18+
# 🔑 Requirements:
19+
# - `secrets.PERSONAL_TOKEN` → A GitHub Personal Access Token with `repo` scope (used to push to protected `main`).
20+
# - `secrets.NPM_AUTH_TOKEN` → An npm token with publish rights.
21+
#
22+
# ✅ This ensures every commit to `main` automatically:
23+
# → bumps the version
24+
# → creates a GitHub Release
25+
# → builds the package
26+
# → publishes the compiled output to npm.
27+
#
28+
# ------------------------------------------------------------------------
329

4-
name: Node.js Package
30+
31+
name: Release Git Package & Publish on NPM
532

633
on:
7-
release:
8-
types: [created]
34+
push:
35+
branches:
36+
- main
937

1038
jobs:
39+
release:
40+
runs-on: ubuntu-latest
41+
outputs:
42+
version: ${{ steps.version.outputs.new_version }}
43+
44+
steps:
45+
- name: Checkout code
46+
uses: actions/checkout@v4
47+
48+
- name: Setup Node.js
49+
uses: actions/setup-node@v4
50+
with:
51+
node-version: 24
52+
53+
- name: Setup Git identity
54+
run: |
55+
git config user.name "github-actions[bot]"
56+
git config user.email "github-actions[bot]@users.noreply.github.com"
57+
58+
- name: Install dependencies
59+
run: npm install
60+
61+
- name: Bump version
62+
id: version
63+
run: |
64+
NEW_VERSION=$(npm version patch -m "chore(release): v%s [skip ci]")
65+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
66+
67+
- name: Push version bump and tag
68+
uses: ad-m/github-push-action@v0.8.0
69+
with:
70+
github_token: ${{ secrets.PERSONAL_TOKEN }} # ← use PAT, since main is protected
71+
branch: main
72+
tags: true
73+
74+
- name: Create GitHub Release
75+
uses: softprops/action-gh-release@v2
76+
with:
77+
tag_name: ${{ steps.version.outputs.new_version }}
78+
name: Release ${{ steps.version.outputs.new_version }}
79+
env:
80+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81+
1182
build:
83+
needs: release
1284
runs-on: ubuntu-latest
1385
steps:
1486
- uses: actions/checkout@v4
87+
1588
- uses: actions/setup-node@v4
1689
with:
1790
node-version: 24
18-
- run: npm run build
1991

20-
- name: Change directory to dist
21-
run: cd dist
92+
- name: Install dependencies
93+
run: npm install
94+
95+
- name: Build package
96+
run: npm run build
97+
98+
- name: Archive build
99+
run: tar -czf dist.tar.gz dist
100+
- name: Upload artifact
101+
uses: actions/upload-artifact@v4
102+
with:
103+
name: dist
104+
path: dist.tar.gz
22105

23106
publish-npm:
24107
needs: build
25108
runs-on: ubuntu-latest
26109
steps:
27110
- uses: actions/checkout@v4
111+
28112
- uses: actions/setup-node@v4
29113
with:
30114
node-version: 24
31115
registry-url: https://registry.npmjs.org/
32-
116+
117+
- name: Download build artifact
118+
uses: actions/download-artifact@v4
119+
with:
120+
name: dist
121+
122+
- name: Extract build
123+
run: tar -xzf dist.tar.gz
124+
33125
- name: Publish to npm
34-
run: npm publish --access public
126+
run: npm publish dist --access public
35127
env:
36-
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
128+
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

0 commit comments

Comments
 (0)