name: Release to crates.io
on:
push:
tags:
- 'v*'
env:
CARGO_TERM_COLOR: always
jobs:
pre-publish:
name: Pre-publish validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Check version matches tag
run: |
TAG_VERSION=${GITHUB_REF#refs/tags/v}
CARGO_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
echo "Tag version: $TAG_VERSION"
echo "Cargo.toml version: $CARGO_VERSION"
if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
echo "Error: Version mismatch!"
exit 1
fi
- name: Run tests
run: cargo test --all-features
- name: Run clippy
run: cargo clippy --all-features -- -D warnings
- name: Check formatting
run: cargo fmt -- --check
- name: Dry-run publish
run: cargo publish --dry-run
publish:
name: Publish to crates.io
needs: pre-publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish
create-release:
name: Create GitHub Release
needs: publish
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Extract version from tag
id: version
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
- name: Extract changelog
id: changelog
run: |
VERSION=${{ steps.version.outputs.VERSION }}
# Extract section between current version and previous version
CHANGELOG=$(awk "/## \[$VERSION\]/,/## \[/" CHANGELOG.md | head -n -1 | tail -n +2)
if [ -z "$CHANGELOG" ]; then
CHANGELOG="See full changelog in CHANGELOG.md"
fi
echo "NOTES<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Release
uses: softprops/action-gh-release@v1
with:
name: Release v${{ steps.version.outputs.VERSION }}
body: |
## 🚀 Release v${{ steps.version.outputs.VERSION }}
${{ steps.changelog.outputs.NOTES }}
---
### 📦 Installation
```toml
[dependencies]
openigtlink-rust = "${{ steps.version.outputs.VERSION }}"
```
### 📚 Resources
- [crates.io](https://crates.io/crates/openigtlink-rust/${{ steps.version.outputs.VERSION }})
- [Documentation](https://docs.rs/openigtlink-rust/${{ steps.version.outputs.VERSION }})
- [Changelog](https://github.com/${{ github.repository }}/blob/main/CHANGELOG.md)
draft: false
prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
verify-publication:
name: Verify crates.io publication
needs: publish
runs-on: ubuntu-latest
steps:
- name: Wait for crates.io propagation
run: sleep 60
- name: Extract version from tag
id: version
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
- name: Create test project
run: |
cargo init verify-test
cd verify-test
cargo add openigtlink-rust@${{ steps.version.outputs.VERSION }}
cat > src/main.rs << 'EOF'
use openigtlink_rust::protocol::types::TransformMessage;
fn main() {
let _ = TransformMessage::identity();
println!("✅ Successfully imported openigtlink-rust");
}
EOF
cargo run