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: Release
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
env:
CARGO_TERM_COLOR: always
jobs:
release:
name: Create Release and Publish
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-release-${{ hashFiles('**/Cargo.lock') }}
- name: Verify version matches tag
run: |
CARGO_VERSION=$(grep -m1 '^version' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
TAG_VERSION=${GITHUB_REF#refs/tags/v}
if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
echo "Error: Cargo.toml version ($CARGO_VERSION) does not match tag version ($TAG_VERSION)"
exit 1
fi
echo "Version verified: $CARGO_VERSION"
- name: Build release
run: cargo build --release --verbose
- name: Run tests
run: cargo test --release --verbose
- name: Extract changelog for this version
id: changelog
run: |
VERSION=${GITHUB_REF#refs/tags/v}
# Extract the section for this version from CHANGELOG.md
CHANGELOG=$(awk -v ver="$VERSION" '
/^## \[/ {
if (found) exit
if ($0 ~ "\\[" ver "\\]") found=1
}
found && !/^## \['"$VERSION"'\]/ { print }
' CHANGELOG.md)
# If no changelog found, use a default message
if [ -z "$CHANGELOG" ]; then
CHANGELOG="Release v$VERSION"
fi
# Write to file for GitHub release body
echo "$CHANGELOG" > release_notes.md
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
body_path: release_notes.md
generate_release_notes: false
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish to crates.io
run: cargo publish --allow-dirty
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}