1
1
name : Publish to npm on Version Bump
2
2
3
- # Trigger the workflow on push events to the main branch
3
+ # Trigger the workflow on push events to the master branch
4
4
on :
5
5
push :
6
6
branches :
@@ -38,23 +38,20 @@ jobs:
38
38
CURRENT_VERSION=$(node -p "require('./package.json').version")
39
39
echo "current_version=$CURRENT_VERSION" >> $GITHUB_ENV
40
40
41
- # 5. Get the previous version from the last commit's package.json
42
- - name : Get previous version
43
- id : previous_version
41
+ # 5. Get the latest published version from npm
42
+ - name : Get latest published version from npm
43
+ id : latest_version
44
44
run : |
45
- # Check if there is a previous commit
46
- if git rev-parse HEAD~1 >/dev/null 2>&1; then
47
- PREVIOUS_VERSION=$(git show HEAD~1:package.json | node -p "require('./package.json').version")
48
- else
49
- PREVIOUS_VERSION=$CURRENT_VERSION
50
- fi
51
- echo "previous_version=$PREVIOUS_VERSION" >> $GITHUB_ENV
45
+ PACKAGE_NAME=$(node -p "require('./package.json').name")
46
+ # Fetch the latest version from npm registry
47
+ LATEST_VERSION=$(npm view "$PACKAGE_NAME" version || echo "0.0.0")
48
+ echo "latest_version=$LATEST_VERSION" >> $GITHUB_ENV
52
49
53
50
# 6. Compare versions to detect a bump
54
51
- name : Check for version bump
55
52
id : version_check
56
53
run : |
57
- if [ "$current_version" != "$previous_version " ]; then
54
+ if [ "$(printf '%s\n' "$LATEST_VERSION" "$CURRENT_VERSION" | sort -V | tail -n1)" = "$CURRENT_VERSION" ] && [ "$CURRENT_VERSION" != "$LATEST_VERSION " ]; then
58
55
echo "version_bumped=true" >> $GITHUB_ENV
59
56
else
60
57
echo "version_bumped=false" >> $GITHUB_ENV
70
67
# 8. Notify if no version bump detected (optional)
71
68
- name : Notify no version bump
72
69
if : env.version_bumped != 'true'
73
- run : echo "No version bump detected. Skipping npm publish."
70
+ run : echo "No version bump detected. Skipping npm publish."
0 commit comments