name: Publish
on:
push:
tags:
- 'v*'
env:
CARGO_TERM_COLOR: always
jobs:
publish:
name: Publish to crates.io and create GitHub release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-publish-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-publish-
- name: Extract version from tag
id: version
run: |
TAG=${GITHUB_REF#refs/tags/}
VERSION=${TAG#v}
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "Tag: $TAG, Version: $VERSION"
- name: Verify version matches Cargo.toml
run: |
CARGO_VERSION=$(grep '^version = ' Cargo.toml | cut -d'"' -f2)
if [ "$CARGO_VERSION" != "${{ steps.version.outputs.version }}" ]; then
echo "Error: Cargo.toml version ($CARGO_VERSION) does not match tag version (${{ steps.version.outputs.version }})"
exit 1
fi
echo "Version verification passed: $CARGO_VERSION"
- name: Check formatting
run: cargo fmt --all -- --check
- name: Run clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Run tests
run: cargo test --lib --verbose --all-features
- name: Run doc tests
run: cargo test --doc --verbose --all-features
- name: Build
run: cargo build --verbose --all-features
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --verbose
- name: Generate release notes
id: release_notes
run: |
TAG="${{ steps.version.outputs.tag }}"
VERSION="${{ steps.version.outputs.version }}"
# Try to extract changelog entry for this version
if [ -f CHANGELOG.md ]; then
# Extract the changelog section for this version
# This is a simple approach - you might want to enhance it
NOTES=$(awk "/^## \[$VERSION\]/,/^## \[/" CHANGELOG.md | head -n -1)
if [ -z "$NOTES" ]; then
NOTES="Release $TAG"$'\n\n'"See [CHANGELOG.md](https://github.com/${{ github.repository }}/blob/$TAG/CHANGELOG.md) for details."
fi
else
NOTES="Release $TAG"
fi
# Save to output (multiline)
{
echo "body<<RELEASE_NOTES"
echo "$NOTES"
echo "RELEASE_NOTES"
} >> $GITHUB_OUTPUT
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
name: Release ${{ steps.version.outputs.tag }}
body: ${{ steps.release_notes.outputs.body }}
draft: false
prerelease: false