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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
name: Version Bump
on:
workflow_dispatch:
inputs:
bump_type:
description: "Release bump (auto follows conventional commits)"
type: choice
default: auto
options:
- auto
- patch
- minor
- major
confirm_breaking_changes:
description: "I verified the public compatibility gate and migration notes for a breaking release"
type: boolean
default: false
env:
CARGO_TERM_COLOR: always
jobs:
version-bump:
name: Analyze and Bump Version
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Install cargo-edit
run: cargo install cargo-edit --locked
- name: Analyze commits and determine version bump
id: version
run: |
# Get current version
CURRENT_VERSION=$(cargo metadata --format-version 1 --no-deps | jq -r '.packages[0].version')
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
# Get commits since last tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
COMMITS=$(git log --oneline)
else
COMMITS=$(git log ${LAST_TAG}..HEAD --oneline)
fi
echo "Analyzing commits since $LAST_TAG:"
echo "$COMMITS"
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
BREAKING=false
if git log ${LAST_TAG:+${LAST_TAG}..HEAD} --format='%s%n%b' | grep -E '(^|[[:space:]])(BREAKING[ -]CHANGE:|[[:alnum:]_-]+(\([^)]+\))?!:)' >/dev/null; then
BREAKING=true
fi
if [ "$BREAKING" = true ] && [ "${{ inputs.confirm_breaking_changes }}" != "true" ]; then
echo "::error::Breaking commits require confirm_breaking_changes=true after compatibility and migration review"
exit 1
fi
REQUESTED_BUMP="${{ inputs.bump_type }}"
BUMP_TYPE="patch"
if [ "$BREAKING" = true ]; then
if [ "$MAJOR" -eq 0 ]; then
BUMP_TYPE="minor"
else
BUMP_TYPE="major"
fi
elif grep -E "^[a-f0-9]+ feat(\(.+\))?:" <<< "$COMMITS" >/dev/null; then
BUMP_TYPE="minor"
elif grep -E "^[a-f0-9]+ (fix|perf|refactor|style|docs|test|chore)(\(.+\))?:" <<< "$COMMITS" >/dev/null; then
BUMP_TYPE="patch"
fi
if [ "$REQUESTED_BUMP" != "auto" ]; then
if [ "$BREAKING" = true ]; then
if [ "$MAJOR" -eq 0 ] && [ "$REQUESTED_BUMP" = "patch" ]; then
echo "::error::Requested bump '$REQUESTED_BUMP' is too small for detected breaking changes"
exit 1
elif [ "$MAJOR" -gt 0 ] && [ "$REQUESTED_BUMP" != "major" ]; then
echo "::error::Requested bump '$REQUESTED_BUMP' is too small for detected breaking changes"
exit 1
fi
fi
BUMP_TYPE="$REQUESTED_BUMP"
fi
echo "bump_type=$BUMP_TYPE" >> $GITHUB_OUTPUT
# Calculate new version
case $BUMP_TYPE in
major)
NEW_VERSION="$((MAJOR + 1)).0.0"
;;
minor)
NEW_VERSION="${MAJOR}.$((MINOR + 1)).0"
;;
patch)
NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))"
;;
esac
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Bumping from $CURRENT_VERSION to $NEW_VERSION ($BUMP_TYPE)"
- name: Bump version
if: steps.version.outputs.current_version != steps.version.outputs.new_version
run: |
cargo set-version ${{ steps.version.outputs.new_version }}
- name: Update CHANGELOG
if: steps.version.outputs.current_version != steps.version.outputs.new_version
run: |
NEW_VERSION="${{ steps.version.outputs.new_version }}"
DATE=$(date +%Y-%m-%d)
# Get commits for changelog
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
# Build changelog entry
ENTRY="## [$NEW_VERSION] - $DATE\n\n"
# Categorize commits
if [ -n "$LAST_TAG" ]; then
ADDED=$(git log ${LAST_TAG}..HEAD --oneline --grep="^feat" 2>/dev/null | sed 's/^[a-f0-9]* /- /' || true)
FIXED=$(git log ${LAST_TAG}..HEAD --oneline --grep="^fix" 2>/dev/null | sed 's/^[a-f0-9]* /- /' || true)
CHANGED=$(git log ${LAST_TAG}..HEAD --oneline --grep="^refactor\|^perf\|^style" 2>/dev/null | sed 's/^[a-f0-9]* /- /' || true)
fi
if [ -n "$ADDED" ]; then
ENTRY="${ENTRY}### Added\n${ADDED}\n\n"
fi
if [ -n "$FIXED" ]; then
ENTRY="${ENTRY}### Fixed\n${FIXED}\n\n"
fi
if [ -n "$CHANGED" ]; then
ENTRY="${ENTRY}### Changed\n${CHANGED}\n\n"
fi
# Insert after [Unreleased] section
sed -i "/## \[Unreleased\]/a\\
\\
$(echo -e "$ENTRY" | sed 's/$/\\n/' | tr -d '\n')" CHANGELOG.md
- name: Commit and push version bump
if: steps.version.outputs.current_version != steps.version.outputs.new_version
run: |
git add Cargo.toml Cargo.lock CHANGELOG.md
git commit -m "chore(release): bump version to ${{ steps.version.outputs.new_version }}"
git push
- name: Create Release Tag
if: steps.version.outputs.current_version != steps.version.outputs.new_version
run: |
git tag -a "v${{ steps.version.outputs.new_version }}" -m "Release v${{ steps.version.outputs.new_version }}"
git push origin "v${{ steps.version.outputs.new_version }}"
- name: Summary
run: |
echo "## Version Bump Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Field | Value |" >> $GITHUB_STEP_SUMMARY
echo "|-------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| Previous Version | ${{ steps.version.outputs.current_version }} |" >> $GITHUB_STEP_SUMMARY
echo "| New Version | ${{ steps.version.outputs.new_version }} |" >> $GITHUB_STEP_SUMMARY
echo "| Bump Type | ${{ steps.version.outputs.bump_type }} |" >> $GITHUB_STEP_SUMMARY