name: Publish to crates.io
on:
release:
types:
- published
workflow_dispatch:
inputs:
release_tag:
description: "Release tag to publish (for example: v0.1.0)"
required: true
type: string
permissions:
contents: read
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo registry and build artifacts
uses: Swatinem/rust-cache@v2
- name: Resolve release version
id: release
shell: bash
run: |
TAG_NAME="${{ github.event.release.tag_name }}"
if [ -z "$TAG_NAME" ]; then
TAG_NAME="${{ inputs.release_tag }}"
fi
VERSION="${TAG_NAME#v}"
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.][0-9A-Za-z.-]+)?$ ]]; then
echo "Unsupported release tag format: $TAG_NAME"
exit 1
fi
echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Apply release version to Cargo.toml
shell: bash
run: |
VERSION="${{ steps.release.outputs.version }}"
sed -i -E "0,/^version = \".*\"$/s//version = \"$VERSION\"/" Cargo.toml
grep '^version = ' Cargo.toml | head -n1
- name: Refresh Cargo.lock for release version
run: cargo check
- name: Run tests
run: cargo test --locked
- name: Publish crate
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --locked --allow-dirty