Sub topic 13
Caching Builds in CI
This chapter assumes you know how Cargo builds a project, what the target/ directory holds, and how sccache works as a compiler cache. A CI runner usually starts from a clean machine, so every run compiles every dependency from scratch. Caching restores earlier work and cuts that cost.
What to cache
Cargo produces two kinds of reusable output, and they have different value.
| Item | Location | Worth caching? |
|---|---|---|
| Downloaded crate sources and registry index | `~/.cargo/registry`, `~/.cargo/git` | Yes. Saves network time. |
| Compiled dependencies | `target/debug/deps`, `target/release/deps` | Yes. This is the largest saving. |
| Your own workspace crates | `target/` | No. They change on almost every commit. |
| Installed tools | `~/.cargo/bin` | Sometimes. Saves reinstalling tools such as `cargo-llvm-lines`. |
Dependencies change rarely, so they make a good cache. Your own crates change on every commit. Caching them wastes upload time and storage.
The cache key
A CI cache is a saved archive stored under a key. The runner looks up the key at the start of a job and restores the archive on a hit. At the end of the job it saves a new archive if the key was not found.
Choose a key that changes when the dependencies change. Cargo.lock is the right input, because it pins every version. The key should also include the operating system and the Rust toolchain version, since compiled artifacts are not portable between them.
flowchart TD
A[Job starts] --> B{Cache key found?}
B -- yes --> C[Restore registry and target/]
B -- no --> D[Start with empty target/]
C --> E[cargo build and test]
D --> E
E --> F{Was the key a hit?}
F -- no --> G[Save cache under new key]
F -- yes --> H[Skip save]The block above is only a sketch. Here is the diagram in the required form.
A GitHub Actions example
The Swatinem/rust-cache action handles key construction and cleanup for you. It hashes Cargo.lock, the toolchain version, and relevant environment variables. It also removes your workspace crates from the saved target/ directory.
name: ci
on: [push, pull_request]
env:
CARGO_INCREMENTAL: 0
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo build --locked --all-targets
- run: cargo test --lockedTwo details matter here.
CARGO_INCREMENTAL: 0turns off incremental compilation. A CI run builds once from a fresh checkout, so incremental data is never reused. Writing it only adds time and enlarges the cache.--lockedmakes Cargo fail ifCargo.lockis out of date. This keeps the cache key honest, because the lockfile then really describes what was built.
Using sccache in CI
A target/ cache is all or nothing. A change to any dependency forces a miss on the whole key. sccache works per compilation unit, so a partial change reuses most of the earlier results.
To use it, set the compiler wrapper and point sccache at a storage backend that persists between runs, such as an S3 bucket or the GitHub Actions cache.
env:
RUSTC_WRAPPER: sccache
SCCACHE_GHA_ENABLED: "true"Add an installation step such as mozilla-actions/sccache-action before the build. Run sccache --show-stats at the end of the job to see the hit rate.
Common problems
- The cache never hits. The key probably contains something that changes every run, such as a timestamp or commit hash. Key on
Cargo.lockinstead. - The cache keeps growing. Each new key saves a full archive. Prune old entries, and avoid caching workspace crates.
- Pull requests cannot read or write caches. Many CI systems restrict cache access for forks. Expect cold builds there.
- Stale artifacts cause odd errors. Changing the toolchain without changing the key can restore incompatible output. Include the compiler version in the key.
- Restoring costs more than it saves. For a small project, downloading a multi-gigabyte archive can be slower than compiling. Compare both times before keeping the cache.
Exercise
Your CI cache key is ${{ runner.os }}-${{ github.sha }}. The job never restores a cache. Why, and what should the key use instead?
Show answer
github.sha changes on every commit, so the key is new each time and never matches an earlier entry. Use a hash of Cargo.lock (and the toolchain version) instead, for example ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}. The key then changes only when dependencies change.
Exercise
Why is CARGO_INCREMENTAL=0 recommended for CI builds?
Show answer
CI usually builds once from a clean checkout, so incremental data is never reused. Producing it only costs build time and makes the cache larger.