Skip to content

Commit 88e814e

Browse files
committed
feat: add version bumping workflow and script for automated version management
1 parent c943f63 commit 88e814e

2 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Version and Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
version:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v5
19+
with:
20+
fetch-depth: 0
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
23+
- name: Setup Node.js
24+
uses: actions/setup-node@v5
25+
with:
26+
node-version: 'lts/*'
27+
28+
- name: Configure Git
29+
run: |
30+
git config user.name "github-actions[bot]"
31+
git config user.email "github-actions[bot]@users.noreply.github.com"
32+
33+
- name: Bump version
34+
id: bump
35+
run: |
36+
bash scripts/bump-version.sh
37+
38+
- name: Get new version
39+
id: version
40+
run: |
41+
NEW_VERSION=$(node -p "require('./package.json').version")
42+
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
43+
echo "New version: $NEW_VERSION"
44+
45+
- name: Check if version changed
46+
id: changed
47+
run: |
48+
if git diff --quiet package.json; then
49+
echo "changed=false" >> $GITHUB_OUTPUT
50+
echo "No version change detected"
51+
else
52+
echo "changed=true" >> $GITHUB_OUTPUT
53+
echo "Version changed detected"
54+
fi
55+
56+
- name: Commit version bump
57+
if: steps.changed.outputs.changed == 'true'
58+
run: |
59+
git add package.json package-lock.json server.json
60+
git commit -m "chore: bump version to ${{ steps.version.outputs.version }}"
61+
git push origin main
62+
63+
- name: Create and push tag
64+
if: steps.changed.outputs.changed == 'true'
65+
run: |
66+
git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}"
67+
git push origin "v${{ steps.version.outputs.version }}"
68+
69+
- name: Create GitHub Release
70+
if: steps.changed.outputs.changed == 'true'
71+
env:
72+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73+
run: |
74+
# Generate release notes from commits
75+
LAST_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
76+
if [ -z "$LAST_TAG" ]; then
77+
COMMITS=$(git log --pretty=format:"- %s (%h)" HEAD^..HEAD)
78+
else
79+
COMMITS=$(git log --pretty=format:"- %s (%h)" "$LAST_TAG"..HEAD^)
80+
fi
81+
82+
# Create release
83+
gh release create "v${{ steps.version.outputs.version }}" \
84+
--title "v${{ steps.version.outputs.version }}" \
85+
--notes "## Changes
86+
87+
$COMMITS"

scripts/bump-version.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Automated version bumping based on conventional commits
5+
# Usage: ./scripts/bump-version.sh
6+
7+
# Get the current version from package.json
8+
CURRENT_VERSION=$(node -p "require('./package.json').version")
9+
echo "Current version: $CURRENT_VERSION"
10+
11+
# Parse the version components
12+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
13+
14+
# Get commits since last tag, or all commits if no tags exist
15+
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
16+
if [ -z "$LAST_TAG" ]; then
17+
echo "No tags found, analyzing all commits"
18+
COMMITS=$(git log --pretty=format:"%s")
19+
else
20+
echo "Last tag: $LAST_TAG"
21+
COMMITS=$(git log "$LAST_TAG"..HEAD --pretty=format:"%s")
22+
fi
23+
24+
# Check if there are any commits to analyze
25+
if [ -z "$COMMITS" ]; then
26+
echo "No new commits since last tag"
27+
exit 0
28+
fi
29+
30+
echo "Analyzing commits..."
31+
32+
# Determine version bump type based on conventional commits
33+
BUMP_TYPE="patch"
34+
35+
# Check for breaking changes (BREAKING CHANGE: or feat!: or fix!:)
36+
if echo "$COMMITS" | grep -qE "^(feat|fix|chore|docs|style|refactor|perf|test)!:|BREAKING[ -]CHANGE:"; then
37+
BUMP_TYPE="major"
38+
echo "Found breaking changes, bumping major version"
39+
# Check for new features (feat:)
40+
elif echo "$COMMITS" | grep -qE "^feat(\(.*\))?:"; then
41+
BUMP_TYPE="minor"
42+
echo "Found new features, bumping minor version"
43+
# Check for fixes, chores, docs, etc. (fix:, chore:, docs:, etc.)
44+
elif echo "$COMMITS" | grep -qE "^(fix|chore|docs|style|refactor|perf|test)(\(.*\))?:"; then
45+
BUMP_TYPE="patch"
46+
echo "Found patches/fixes, bumping patch version"
47+
else
48+
echo "No conventional commits found, bumping patch version"
49+
BUMP_TYPE="patch"
50+
fi
51+
52+
# Calculate new version
53+
case "$BUMP_TYPE" in
54+
major)
55+
NEW_VERSION="$((MAJOR + 1)).0.0"
56+
;;
57+
minor)
58+
NEW_VERSION="$MAJOR.$((MINOR + 1)).0"
59+
;;
60+
patch)
61+
NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))"
62+
;;
63+
esac
64+
65+
echo "New version: $NEW_VERSION"
66+
67+
# Update package.json
68+
sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" package.json && rm package.json.bak
69+
70+
# Update server.json
71+
sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" server.json && rm server.json.bak
72+
73+
# Update package-lock.json (it has version in multiple places)
74+
sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/g" package-lock.json && rm package-lock.json.bak
75+
76+
echo "Version bumped from $CURRENT_VERSION to $NEW_VERSION"
77+
78+
# Output for GitHub Actions
79+
if [ -n "$GITHUB_OUTPUT" ]; then
80+
echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_OUTPUT"
81+
fi

0 commit comments

Comments
 (0)