name: CI
on:
push:
branches: [main]
tags: ["v*"]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo build --all-targets
- run: cargo test
publish:
if: (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) && success()
needs: [test]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: dtolnay/rust-toolchain@stable
- name: Verify tag is on main
if: startsWith(github.ref, 'refs/tags/v')
run: |
git fetch origin main
if ! git merge-base --is-ancestor HEAD origin/main; then
echo "tag is not on main — refusing to publish"
exit 1
fi
- name: Create tag from Cargo.toml version
if: github.ref == 'refs/heads/main'
run: |
VERSION=$(grep '^version = ' Cargo.toml | cut -d'"' -f2)
TAG="v$VERSION"
if git tag -l "$TAG" | grep -q .; then
echo "Tag $TAG already exists — skipping creation"
else
git config user.name "github-actions"
git config user.email "actions@github.com"
git tag "$TAG"
echo "Created local tag $TAG"
fi
- run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Push tag after publish
if: github.ref == 'refs/heads/main' && success()
run: |
VERSION=$(grep '^version = ' Cargo.toml | cut -d'"' -f2)
TAG="v$VERSION"
git push origin "$TAG"