name: Release
on:
push:
tags:
- 'v*'
- '[0-9]+.[0-9]+.[0-9]+*'
permissions:
contents: write
env:
CARGO_TERM_COLOR: always
jobs:
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v5
- name: Normalize tag to v-prefix
id: tag
run: |
RAW_TAG=${GITHUB_REF#refs/tags/}
if [[ "$RAW_TAG" =~ ^v ]]; then
VERSION=${RAW_TAG#v}
TAG="$RAW_TAG"
else
VERSION="$RAW_TAG"
TAG="v${RAW_TAG}"
# Create the v-prefixed tag pointing at the same commit
git tag "$TAG"
git push origin "$TAG"
fi
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
- name: Delete bare semver tag if v-prefixed was created
if: ${{ !startsWith(github.ref, 'refs/tags/v') }}
run: |
RAW_TAG=${GITHUB_REF#refs/tags/}
git push origin ":refs/tags/$RAW_TAG"
git tag -d "$RAW_TAG"
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2
- name: Verify version matches tag
run: |
CARGO_VERSION=$(grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)
if [ "${{ steps.tag.outputs.version }}" != "$CARGO_VERSION" ]; then
echo "Tag version (${{ steps.tag.outputs.version }}) doesn't match Cargo.toml ($CARGO_VERSION)"
exit 1
fi
- name: Run tests
run: cargo test --all-features
- name: Publish to crates.io
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Extract changelog for release
id: changelog
run: |
VERSION="${{ steps.tag.outputs.version }}"
# Extract the section between ## [VERSION] and the next ## [
BODY=$(awk "/^## \\[${VERSION}\\]/{found=1; next} /^## \\[/{if(found) exit} found{print}" CHANGELOG.md)
if [ -z "$BODY" ]; then
BODY="Release ${VERSION}"
fi
# Use delimiter to handle multiline
{
echo "body<<CHANGELOG_EOF"
echo "$BODY"
echo "CHANGELOG_EOF"
} >> "$GITHUB_OUTPUT"
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.tag.outputs.tag }}
body: ${{ steps.changelog.outputs.body }}