GitHub Actions Workflow for Cache Cargo Builds
Cache Cargo so Rust builds skip recompiling unchanged crates.
This workflow uses rust-cache to persist the Cargo registry and target dir across runs, cutting compile time.
The workflow
.github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with: { components: clippy, rustfmt }
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --check
- run: cargo clippy -- -D warnings
- run: cargo testNotes
Swatinem/rust-cachekeys the cache off Cargo.lock and the toolchain.- It prunes stale entries so the cache stays small.
- Pair with clippy and fmt for a full quality gate.
Run it cheaper
Point runs-on: at a Latchkey label to run this workflow at roughly 69% lower per-minute cost, with self-healing for transient failures.
Related guides
GitHub Actions Workflow Template for RustA ready-to-use GitHub Actions workflow for Rust with caching and best practices - copy, commit, and run faste…
How to Cache Dependencies in GitHub Actions (Every Ecosystem)Cache dependencies in GitHub Actions with actions/cache - correct keys and paths for npm, yarn, pnpm, pip, Ma…
How to Speed Up GitHub Actions: 9 High-Impact TacticsMake GitHub Actions faster: caching, parallelization, test splitting, Docker layer caching, slimmer images, a…