Skip to content

Commit 50df381

Browse files
feat: add automated npm publish workflow
Adds a GitHub Actions workflow that automatically publishes to npm when the package.json version changes on main branch pushes. Features: - Detects version changes by comparing package.json - Builds the package with bun - Publishes to npm with public access - Creates GitHub tag and release - Posts success comment on commit Required secret: NPM_TOKEN
1 parent 43c47a2 commit 50df381

2 files changed

Lines changed: 214 additions & 0 deletions

File tree

.github/workflows/npm-publish.yml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# ---
2+
# id: ci/npm-publish
3+
# category: ci
4+
# type: template
5+
# name: NPM Publish
6+
# description: Automatically publish to npm when version changes on main branch
7+
# secrets:
8+
# - name: NPM_TOKEN
9+
# description: NPM authentication token with publish access
10+
# required: true
11+
# inputs: []
12+
# triggers: [push]
13+
# variants:
14+
# - name: standard
15+
# description: Publishes when package.json version changes on main branch pushes
16+
# ---
17+
18+
name: NPM Publish
19+
20+
on:
21+
push:
22+
branches: [main]
23+
24+
jobs:
25+
publish:
26+
runs-on: ubuntu-latest
27+
if: github.repository_owner == 'OpenStaticFish'
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v4
31+
32+
- name: Setup Bun
33+
uses: oven-sh/setup-bun@v1
34+
with:
35+
bun-version: latest
36+
37+
- name: Install dependencies
38+
run: bun install
39+
40+
- name: Build package
41+
run: bun run build
42+
43+
- name: Check version change
44+
id: check
45+
run: |
46+
# Get the current version from package.json
47+
CURRENT_VERSION=$(jq -r '.version' package.json)
48+
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
49+
50+
# Get the previous version from the last commit
51+
git fetch origin main~1:main~1
52+
PREVIOUS_VERSION=$(git show main~1:package.json | jq -r '.version')
53+
echo "previous_version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
54+
55+
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
56+
echo "version_changed=true" >> $GITHUB_OUTPUT
57+
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
58+
else
59+
echo "version_changed=false" >> $GITHUB_OUTPUT
60+
echo "Version unchanged: $CURRENT_VERSION"
61+
fi
62+
63+
- name: Publish to npm
64+
if: steps.check.outputs.version_changed == 'true'
65+
env:
66+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
67+
run: |
68+
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
69+
npm publish --access public
70+
71+
- name: Create GitHub Release
72+
if: steps.check.outputs.version_changed == 'true'
73+
uses: actions/github-script@v7
74+
with:
75+
script: |
76+
const version = '${{ steps.check.outputs.current_version }}';
77+
const tag = `v${version}`;
78+
79+
await github.rest.git.createRef({
80+
owner: context.repo.owner,
81+
repo: context.repo.repo,
82+
ref: `refs/tags/${tag}`,
83+
sha: context.sha
84+
});
85+
86+
await github.rest.repos.createRelease({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
tag_name: tag,
90+
name: `Release ${tag}`,
91+
body: `Published version ${version} to npm`,
92+
draft: false,
93+
prerelease: false
94+
});
95+
96+
- name: Post success comment
97+
if: steps.check.outputs.version_changed == 'true'
98+
uses: actions/github-script@v7
99+
with:
100+
script: |
101+
const version = '${{ steps.check.outputs.current_version }}';
102+
await github.rest.repos.createCommitComment({
103+
owner: context.repo.owner,
104+
repo: context.repo.repo,
105+
commit_sha: context.sha,
106+
body: `🚀 Successfully published version ${version} to npm!\n\nPackage: @openstaticfish/actionflow@${version}`
107+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# ---
2+
# id: ci/npm-publish
3+
# category: ci
4+
# type: template
5+
# name: NPM Publish
6+
# description: Automatically publish to npm when version changes on main branch
7+
# secrets:
8+
# - name: NPM_TOKEN
9+
# description: NPM authentication token with publish access
10+
# required: true
11+
# inputs: []
12+
# triggers: [push]
13+
# variants:
14+
# - name: standard
15+
# description: Publishes when package.json version changes on main branch pushes
16+
# ---
17+
18+
name: NPM Publish
19+
20+
on:
21+
push:
22+
branches: [main]
23+
24+
jobs:
25+
publish:
26+
runs-on: ubuntu-latest
27+
if: github.repository_owner == 'OpenStaticFish'
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v4
31+
32+
- name: Setup Bun
33+
uses: oven-sh/setup-bun@v1
34+
with:
35+
bun-version: latest
36+
37+
- name: Install dependencies
38+
run: bun install
39+
40+
- name: Build package
41+
run: bun run build
42+
43+
- name: Check version change
44+
id: check
45+
run: |
46+
# Get the current version from package.json
47+
CURRENT_VERSION=$(jq -r '.version' package.json)
48+
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
49+
50+
# Get the previous version from the last commit
51+
git fetch origin main~1:main~1
52+
PREVIOUS_VERSION=$(git show main~1:package.json | jq -r '.version')
53+
echo "previous_version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT
54+
55+
if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
56+
echo "version_changed=true" >> $GITHUB_OUTPUT
57+
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
58+
else
59+
echo "version_changed=false" >> $GITHUB_OUTPUT
60+
echo "Version unchanged: $CURRENT_VERSION"
61+
fi
62+
63+
- name: Publish to npm
64+
if: steps.check.outputs.version_changed == 'true'
65+
env:
66+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
67+
run: |
68+
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
69+
npm publish --access public
70+
71+
- name: Create GitHub Release
72+
if: steps.check.outputs.version_changed == 'true'
73+
uses: actions/github-script@v7
74+
with:
75+
script: |
76+
const version = '${{ steps.check.outputs.current_version }}';
77+
const tag = `v${version}`;
78+
79+
await github.rest.git.createRef({
80+
owner: context.repo.owner,
81+
repo: context.repo.repo,
82+
ref: `refs/tags/${tag}`,
83+
sha: context.sha
84+
});
85+
86+
await github.rest.repos.createRelease({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
tag_name: tag,
90+
name: `Release ${tag}`,
91+
body: `Published version ${version} to npm`,
92+
draft: false,
93+
prerelease: false
94+
});
95+
96+
- name: Post success comment
97+
if: steps.check.outputs.version_changed == 'true'
98+
uses: actions/github-script@v7
99+
with:
100+
script: |
101+
const version = '${{ steps.check.outputs.current_version }}';
102+
await github.rest.repos.createCommitComment({
103+
owner: context.repo.owner,
104+
repo: context.repo.repo,
105+
commit_sha: context.sha,
106+
body: `🚀 Successfully published version ${version} to npm!\n\nPackage: @openstaticfish/actionflow@${version}`
107+
});

0 commit comments

Comments
 (0)