CI Config for Rust
Git-side CI configuration keeps Rust quality gates close to version control: .github/workflows/ci.yml mirrors what reviewers expect before merge.
Recipe
name: ci
on:
pull_request:
push:
branches: [main]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.97.0
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --all -- --check
- run: cargo clippy --all-targets --all-features -- -D warnings
- run: cargo test --all-features
- run: cargo build --release --locked
env:
SQLX_OFFLINE: trueWhen to reach for this:
- New Rust repo bootstrap
- CI slower than 15 minutes without cache
- Flaky tests from missing offline sqlx data
Working Example
jobs:
msrv:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@1.97.0
- run: cargo test --locked
miri:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: miri
- run: cargo +nightly miri test -p unsafe-crateWhat this demonstrates:
- MSRV job validates minimum toolchain
- Optional Miri job for
unsafecrates SQLX_OFFLINEavoids live DB in PR CI
Deep Dive
Pre-commit Parity
# .pre-commit-config.yaml hook
- repo: local
hooks:
- id: cargo-fmt
entry: cargo fmt --all -- --check
language: system
pass_filenames: falseHooks should match CI commands exactly.
Gotchas
- No
--locked- nondeterministic CI. Fix: fail when lockfile out of date. - Caching
targetwithout lock key - stale artifacts. Fix: rust-cache handles keys. - Integration tests need postgres - use service container or mark
#[ignore]for CI split. - Clippy without
-D warnings- warnings accumulate. - Nightly only on main - PRs should not require nightly unless documented.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
cargo nextest | Faster test reporting | Minimal test count |
| self-hosted runners | Large workspace | Simple OSS |
| GitLab includes | Org standard | GitHub repo |
FAQs
Where does deploy CI live?
Separate release.yml on tags; keep PR ci.yml fast.
How to cache sqlx data?
Commit .sqlx/; set SQLX_OFFLINE=true in workflow env.
Workspace CI?
Run from repo root; cargo test --workspace --all-features.
Related
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.