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
name: Release
on:
push:
branches:
paths-ignore:
- 'docs/**'
- '*.md'
- '.github/workflows/docs.yml'
permissions:
contents: write
pull-requests: write
jobs:
release:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, 'chore(release)')"
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Cache dependencies
uses: Swatinem/rust-cache@v2
- name: Determine version bump
id: version
run: |
# 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)
LAST_TAG="v0.0.0"
else
COMMITS=$(git log ${LAST_TAG}..HEAD --oneline)
fi
echo "Last tag: $LAST_TAG"
echo "Commits since last tag:"
echo "$COMMITS"
# Determine bump type based on conventional commits
BUMP="patch"
if echo "$COMMITS" | grep -q "^[a-f0-9]* feat"; then
BUMP="minor"
fi
if echo "$COMMITS" | grep -q "BREAKING CHANGE\|^[a-f0-9]* feat!"; then
BUMP="major"
fi
# Get current version from Cargo.toml
CURRENT_VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
# Calculate new version
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
case $BUMP in
major) NEW_VERSION="$((MAJOR + 1)).0.0" ;;
minor) NEW_VERSION="$MAJOR.$((MINOR + 1)).0" ;;
patch) NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))" ;;
esac
echo "Current version: $CURRENT_VERSION"
echo "Bump type: $BUMP"
echo "New version: $NEW_VERSION"
echo "bump=$BUMP" >> $GITHUB_OUTPUT
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "last_tag=$LAST_TAG" >> $GITHUB_OUTPUT
- name: Check if release needed
run: |
if [ "${{ steps.version.outputs.version }}" = "${{ steps.version.outputs.current }}" ]; then
echo "No version bump needed. Current version: ${{ steps.version.outputs.current }}"
echo "No commits found that require a release."
exit 0
else
echo "Version bump needed: ${{ steps.version.outputs.current }} -> ${{ steps.version.outputs.version }}"
fi
- name: Generate changelog
if: steps.version.outputs.version != steps.version.outputs.current
id: changelog
run: |
LAST_TAG="${{ steps.version.outputs.last_tag }}"
NEW_VERSION="${{ steps.version.outputs.version }}"
# Generate changelog from commits
CHANGELOG=$(git log ${LAST_TAG}..HEAD --pretty=format:"- %s" --grep="^feat\|^fix\|^perf\|^refactor" | sed 's/^- \([a-f0-9]*\) /- /')
# Create release notes
cat > release_notes.md << EOF
## What's Changed
$CHANGELOG
**Full Changelog**: https://github.com/${{ github.repository }}/compare/${LAST_TAG}...v${NEW_VERSION}
EOF
# Update CHANGELOG.md
if [ ! -f CHANGELOG.md ]; then
echo "# Changelog" > CHANGELOG.md
echo "" >> CHANGELOG.md
echo "All notable changes to this project will be documented in this file." >> CHANGELOG.md
echo "" >> CHANGELOG.md
fi
# Prepend new version to changelog
{
head -n 4 CHANGELOG.md
echo "## [${NEW_VERSION}] - $(date +%Y-%m-%d)"
echo ""
echo "$CHANGELOG"
echo ""
tail -n +5 CHANGELOG.md
} > CHANGELOG.tmp && mv CHANGELOG.tmp CHANGELOG.md
- name: Update version
if: steps.version.outputs.version != steps.version.outputs.current
run: |
sed -i 's/^version = ".*"/version = "${{ steps.version.outputs.version }}"/' Cargo.toml
cargo check # Update Cargo.lock
- name: Run tests
if: steps.version.outputs.version != steps.version.outputs.current
run: |
cargo test --verbose --lib --bins --tests
cargo test --doc
- name: Run clippy
if: steps.version.outputs.version != steps.version.outputs.current
run: cargo clippy --lib --bins --tests -- -D warnings
- name: Check formatting
if: steps.version.outputs.version != steps.version.outputs.current
run: cargo fmt --check
- name: Commit version bump
if: steps.version.outputs.version != steps.version.outputs.current
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add Cargo.toml Cargo.lock CHANGELOG.md
git commit -m "chore(release): bump version to ${{ steps.version.outputs.version }}"
git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}"
git push origin main
git push origin "v${{ steps.version.outputs.version }}"
- name: Publish to crates.io
if: steps.version.outputs.version != steps.version.outputs.current
run: cargo publish --allow-dirty --token ${{ secrets.CRATES_IO_TOKEN }}
- name: Create GitHub Release
if: steps.version.outputs.version != steps.version.outputs.current
uses: softprops/action-gh-release@v3
with:
tag_name: v${{ steps.version.outputs.version }}
body_path: release_notes.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}