-
Notifications
You must be signed in to change notification settings - Fork 0
140 lines (116 loc) · 4.5 KB
/
publish.yml
File metadata and controls
140 lines (116 loc) · 4.5 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: Publish to NPM
on:
push:
tags:
- 'v*'
release:
types: [published]
workflow_dispatch:
inputs:
version:
description: 'Version to publish (optional)'
required: false
default: ''
jobs:
publish:
name: Publish to NPM
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20.x
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: pnpm install --no-frozen-lockfile
- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Check and bump version if needed
id: version_check
if: ${{ github.event.inputs.version == '' }}
run: |
# Get current version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "Current package.json version: $CURRENT_VERSION"
# Get published version from npm (suppress errors if package doesn't exist)
PUBLISHED_VERSION=$(pnpm view @toneflix/paystack-cli version 2>/dev/null || echo "0.0.0")
echo "Published npm version: $PUBLISHED_VERSION"
# Function to compare versions
compare_versions() {
if [ "$1" = "$2" ]; then
echo "equal"
else
# Use sort -V for version comparison
SORTED=$(printf "%s\n%s" "$1" "$2" | sort -V | head -n1)
if [ "$SORTED" = "$1" ]; then
echo "less"
else
echo "greater"
fi
fi
}
COMPARISON=$(compare_versions "$CURRENT_VERSION" "$PUBLISHED_VERSION")
echo "Version comparison: $COMPARISON"
if [ "$COMPARISON" = "equal" ] || [ "$COMPARISON" = "less" ]; then
echo "Version conflict detected. Auto-bumping version..."
# Parse version components
IFS='.' read -r MAJOR MINOR PATCH <<< "$PUBLISHED_VERSION"
# Bump patch version
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
echo "New version: $NEW_VERSION"
# Update package.json
pnpm version "$NEW_VERSION" --no-git-tag-version
echo "version_bumped=true" >> $GITHUB_OUTPUT
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
else
echo "Version is valid. Proceeding with current version."
echo "version_bumped=false" >> $GITHUB_OUTPUT
echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
fi
- name: Commit and push version bump
if: steps.version_check.outputs.version_bumped == 'true'
run: |
git add package.json
git commit -m "chore: bump version to ${{ steps.version_check.outputs.new_version }} [skip ci]"
git push origin HEAD:main
- name: Set version from input
if: ${{ github.event.inputs.version != '' }}
run: |
echo "Setting version to ${{ github.event.inputs.version }}"
pnpm version "${{ github.event.inputs.version }}" --no-git-tag-version
echo "version_bumped=true" >> $GITHUB_OUTPUT
echo "new_version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
git add package.json
git commit -m "chore: set version to ${{ github.event.inputs.version }} [skip ci]"
git push origin HEAD:main
- name: Run linter
run: pnpm lint
- name: Run tests
run: pnpm test
- name: Build project
run: pnpm build
- name: Publish to NPM
run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create release notes
if: steps.version_check.outputs.version_bumped == 'true'
run: |
echo "Published version ${{ steps.version_check.outputs.new_version }} to npm" >> $GITHUB_STEP_SUMMARY
echo "Auto-bumped from published version" >> $GITHUB_STEP_SUMMARY