name: Cargo Build & Test
on:
push:
branches:
- main
pull_request:
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
jobs:
build_and_test:
name: Build, test and publish
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- run: rustup update stable && rustup default stable
- run: cargo build --verbose
- run: cargo test --verbose
- name: Check if version has changed
id: check_version
run: |
if git diff HEAD~1 HEAD -- Cargo.toml | grep '^[+-]version = '; then
echo "changed=true" >> $GITHUB_ENV
else
echo "changed=false" >> $GITHUB_ENV
fi
- name: Tag the release
if: github.ref == 'refs/heads/main' && env.changed == 'true'
run: |
version=$(grep '^version\s*=' Cargo.toml | sed -E 's/version[[:space:]]*=[[:space:]]*"(.*)"/\1/')
echo "Tagging version: $version"
git config user.name "Automated"
git config user.email "actions@users.noreply.github.com"
git tag -a v$version -m "Release v$version"
git push origin v$version
- name: Publish to crates.io
if: github.ref == 'refs/heads/main' && env.changed == 'true'
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}