GitHub Actions Workflow Template for Rust
A complete, caching-enabled GitHub Actions workflow for Rust. Drop it in .github/workflows/ci.yml and adjust.
This template builds and tests a Rust project with dependency caching and sensible defaults. It caches the cargo registry and target dir.
The workflow
.github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: cargo-${{ hashFiles('**/Cargo.lock') }}
- run: cargo fmt --check
- run: cargo clippy -- -D warnings
- run: cargo testWhat it does
- Caches cargo registry + target
- Runs fmt check, clippy, build, and test
Run it cheaper
Change runs-on: ubuntu-latest to a Latchkey label to run this same workflow at roughly 69% lower per-minute cost, with self-healing for transient failures.
Related guides
How to Speed Up GitHub Actions: 9 High-Impact TacticsMake GitHub Actions faster: caching, parallelization, test splitting, Docker layer caching, slimmer images, a…
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…