-
Notifications
You must be signed in to change notification settings - Fork 2
85 lines (71 loc) · 2.56 KB
/
update-version.yml
File metadata and controls
85 lines (71 loc) · 2.56 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
name: Update Package Version
on:
pull_request_target:
types:
- closed
jobs:
update-version:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install jq (JSON processor)
run: sudo apt-get install -y jq
- name: Detect version bump type
id: detect_version
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
# Default to patch version bump
VERSION_BUMP="patch"
# Check the PR title to determine the version type
if echo "$PR_TITLE" | grep -iq "BREAKING CHANGE"; then
VERSION_BUMP="major"
elif echo "$PR_TITLE" | grep -iq "^feat"; then
VERSION_BUMP="minor"
fi
echo "Version bump type: $VERSION_BUMP"
echo "version_bump=$VERSION_BUMP" >> $GITHUB_OUTPUT
- name: Update package version
env:
VERSION_BUMP: ${{ steps.detect_version.outputs.version_bump }}
run: |
# Get the current version from package.json
CURRENT_VERSION=$(jq -r '.version' package.json)
# Split version into major, minor, patch
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
# Determine the new version based on the bump type
if [ "$VERSION_BUMP" = "major" ]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif [ "$VERSION_BUMP" = "minor" ]; then
MINOR=$((MINOR + 1))
PATCH=0
else
PATCH=$((PATCH + 1))
fi
# Create new version string
NEW_VERSION="$MAJOR.$MINOR.$PATCH"
# Update the version in package.json
jq --arg new_version "$NEW_VERSION" '.version = $new_version' package.json > tmp.json && mv tmp.json package.json
# Commit the updated package.json if there are changes
if [ -n "$(git status --porcelain)" ]; then
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add package.json
git commit -m "Bump package version to $NEW_VERSION"
git push origin master
else
echo "No changes to commit"
fi