Skip to content

Commit c4eaba2

Browse files
committed
fix(publish): update GitHub Actions workflow to auto-bump version and publish to NPM
1 parent 60ad570 commit c4eaba2

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

.github/workflows/publish.yml

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ jobs:
1212
name: Publish to NPM
1313
runs-on: ubuntu-latest
1414
permissions:
15-
contents: read
15+
contents: write
1616
id-token: write
1717

1818
steps:
1919
- name: Checkout code
2020
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
token: ${{ secrets.GITHUB_TOKEN }}
2124

2225
- name: Install pnpm
2326
uses: pnpm/action-setup@v2
@@ -31,6 +34,70 @@ jobs:
3134
cache: 'pnpm'
3235
registry-url: 'https://registry.npmjs.org'
3336

37+
- name: Configure Git
38+
run: |
39+
git config --global user.name "github-actions[bot]"
40+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
41+
42+
- name: Check and bump version if needed
43+
id: version_check
44+
run: |
45+
# Get current version from package.json
46+
CURRENT_VERSION=$(node -p "require('./package.json').version")
47+
echo "Current package.json version: $CURRENT_VERSION"
48+
49+
# Get published version from npm (suppress errors if package doesn't exist)
50+
PUBLISHED_VERSION=$(npm view @toneflix/paystack-cli version 2>/dev/null || echo "0.0.0")
51+
echo "Published npm version: $PUBLISHED_VERSION"
52+
53+
# Function to compare versions
54+
compare_versions() {
55+
if [ "$1" = "$2" ]; then
56+
echo "equal"
57+
else
58+
# Use sort -V for version comparison
59+
SORTED=$(printf "%s\n%s" "$1" "$2" | sort -V | head -n1)
60+
if [ "$SORTED" = "$1" ]; then
61+
echo "less"
62+
else
63+
echo "greater"
64+
fi
65+
fi
66+
}
67+
68+
COMPARISON=$(compare_versions "$CURRENT_VERSION" "$PUBLISHED_VERSION")
69+
echo "Version comparison: $COMPARISON"
70+
71+
if [ "$COMPARISON" = "equal" ] || [ "$COMPARISON" = "less" ]; then
72+
echo "Version conflict detected. Auto-bumping version..."
73+
74+
# Parse version components
75+
IFS='.' read -r MAJOR MINOR PATCH <<< "$PUBLISHED_VERSION"
76+
77+
# Bump patch version
78+
NEW_PATCH=$((PATCH + 1))
79+
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
80+
81+
echo "New version: $NEW_VERSION"
82+
83+
# Update package.json
84+
npm version "$NEW_VERSION" --no-git-tag-version
85+
86+
echo "version_bumped=true" >> $GITHUB_OUTPUT
87+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
88+
else
89+
echo "Version is valid. Proceeding with current version."
90+
echo "version_bumped=false" >> $GITHUB_OUTPUT
91+
echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
92+
fi
93+
94+
- name: Commit and push version bump
95+
if: steps.version_check.outputs.version_bumped == 'true'
96+
run: |
97+
git add package.json
98+
git commit -m "chore: bump version to ${{ steps.version_check.outputs.new_version }} [skip ci]"
99+
git push origin HEAD:main
100+
34101
- name: Install dependencies
35102
run: pnpm install --frozen-lockfile
36103

@@ -47,3 +114,9 @@ jobs:
47114
run: pnpm publish --no-git-checks
48115
env:
49116
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
117+
118+
- name: Create release notes
119+
if: steps.version_check.outputs.version_bumped == 'true'
120+
run: |
121+
echo "Published version ${{ steps.version_check.outputs.new_version }} to npm" >> $GITHUB_STEP_SUMMARY
122+
echo "Auto-bumped from published version" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)