name: Release to crates.io
on:
workflow_dispatch:
inputs:
dry_run:
description: 'Dry-run (cargo publish --dry-run, do not upload)'
required: false
default: 'false'
type: boolean
push:
tags:
- 'v*'
jobs:
publish:
name: Build, test, publish
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install stable Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache cargo registry + build
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Verify Cargo.toml version matches git tag
if: startsWith(github.ref, 'refs/tags/v')
run: |
TAG_VERSION="${GITHUB_REF_NAME#v}"
CARGO_VERSION=$(grep -m1 '^version' Cargo.toml | sed -E 's/version *= *"([^"]+)".*/\1/')
echo "tag=$TAG_VERSION cargo=$CARGO_VERSION"
if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
echo "::error::git tag v$TAG_VERSION does not match Cargo.toml version $CARGO_VERSION"
exit 1
fi
- name: Format check
run: cargo fmt --all -- --check
- name: Clippy (warnings as errors)
run: cargo clippy --all-targets -- -D warnings
- name: Build release
run: cargo build --release --all-targets
- name: Run unit tests
run: cargo test --lib --release
- name: Publish (dry-run)
if: github.event_name == 'workflow_dispatch' && inputs.dry_run == true
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_TOKEN }}
run: cargo publish --dry-run --token "$CARGO_REGISTRY_TOKEN"
- name: Publish to crates.io
if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'workflow_dispatch' && inputs.dry_run == false)
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_TOKEN }}
run: cargo publish --token "$CARGO_REGISTRY_TOKEN"
- name: Create GitHub Release
if: startsWith(github.ref, 'refs/tags/v')
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
draft: false
prerelease: ${{ contains(github.ref_name, '-') }}