name: Cargo Build, Test & Publish
on:
push:
branches: ["main"]
tags: ["*"]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
toolchain: [stable, beta, nightly]
os: [windows-latest, macos-latest, ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Rust
run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- name: Cache Cargo files
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: cargo-${{ matrix.toolchain }}-${{ runner.os }}-${{ github.sha }}
restore-keys: cargo-${{ matrix.toolchain }}-${{ runner.os }}-
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
publish:
name: Publish project to crates.io
needs: build
if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
environment: crates-io
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
run: rustup update stable && rustup default stable
- name: Publish
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}