forked from TheTrunk/groestl-hash-js
-
Notifications
You must be signed in to change notification settings - Fork 0
70 lines (60 loc) · 2.19 KB
/
publish.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
name: Publish to npm on Version Bump
# Trigger the workflow on push events to the master branch
on:
push:
branches:
- master
# Optional: Run only when package.json is modified
# paths:
# - 'package.json'
jobs:
publish:
runs-on: ubuntu-latest
steps:
# 1. Checkout the repository code
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for version comparison
# 2. Setup Node.js environment
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20' # Specify your Node.js version
registry-url: 'https://registry.npmjs.org/'
# 3. Install dependencies
- name: Install dependencies
run: npm install
# 4. Get the current version from package.json
- name: Get current version
id: current_version
run: |
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "current_version=$CURRENT_VERSION" >> $GITHUB_ENV
# 5. Get the latest published version from npm
- name: Get latest published version from npm
id: latest_version
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
# Fetch the latest version from npm registry
LATEST_VERSION=$(npm view "$PACKAGE_NAME" version || echo "0.0.0")
echo "latest_version=$LATEST_VERSION" >> $GITHUB_ENV
# 6. Compare versions to detect a bump
- name: Check for version bump
id: version_check
run: |
if [ "${{env.latest_version}}" != "${{env.current_version}}" ]; then
echo "version_bumped=true" >> $GITHUB_ENV
else
echo "version_bumped=false" >> $GITHUB_ENV
fi
# 7. Publish to npm if version is bumped
- name: Publish to npm
if: env.version_bumped == 'true'
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# 8. Notify if no version bump detected (optional)
- name: Notify no version bump
if: env.version_bumped != 'true'
run: echo "No version bump detected. Skipping npm publish."