SNOWSTEAD DOCS Home Dashboard

Set up sccache

Point sccache at your cache, and every machine reuses objects someone already compiled instead of compiling them again. It wraps rustc and your C and C++ compilers, and your code doesn't change.

Create a cache in the dashboard and turn on sccache, or turn it on in an existing cache's Settings. Caches and tokens are made in the dashboard.

This machine
Your cache is
You build with
The steps below change to match.

A machine that reads downloads what the cache already holds and uploads nothing. A machine that reads and writes also uploads what it builds, so other machines skip that work and use what it uploaded. Writing always needs a token that can write. You choose which machines get one.

1. Install sccache

Install sccache 0.16 or later on every machine that builds, including CI. Earlier versions ignore read-only mode. It talks to your cache over WebDAV, so use a build with WebDAV support.

shell
cargo install sccache --locked
sccache --version

2. Point sccache at your cache

Create a token under Tokens in the dashboard that reads this cache, and store it as SNOWSTEAD_TOKEN. Read-only mode stops uploads, whatever the token allows.

Put these lines in your shell profile or the project's .envrc. This example uses organization acme and cache main. Replace them with your own slugs. The endpoint is your cache's address followed by /sccache/, with the trailing slash. Don't set SCCACHE_WEBDAV_KEY_PREFIX: the endpoint already picks the cache.

shell
export SCCACHE_WEBDAV_ENDPOINT=https://acme.snowstead.com/main/sccache/
export SCCACHE_WEBDAV_TOKEN="$SNOWSTEAD_TOKEN"
export SCCACHE_WEBDAV_RW_MODE=READ_ONLY
export RUSTC_WRAPPER=sccache
export CARGO_INCREMENTAL=0
cargo build --release

These are read when the sccache server starts. After changing them, run sccache --stop-server, and the next build starts a fresh one.

3. Use it in CI

Store the token as the SNOWSTEAD_TOKEN repository secret. GitHub gives workflows from fork pull requests no secrets, so those runs have no token and can't read a private cache: this workflow builds them without it.

.github/workflows/build.yml
on:
  push:
    branches: [main]
  pull_request:
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      SCCACHE_WEBDAV_ENDPOINT: https://acme.snowstead.com/main/sccache/
      SCCACHE_WEBDAV_TOKEN: ${{ secrets.SNOWSTEAD_TOKEN }}
      SCCACHE_WEBDAV_RW_MODE: READ_ONLY
      RUSTC_WRAPPER: sccache
      CARGO_INCREMENTAL: "0"
    steps:
      - uses: actions/checkout@v4
      - name: Build with the cache
        if: ${{ !github.event.pull_request.head.repo.fork }}
        run: |
          cargo install sccache --locked
          cargo build --release
          sccache --show-stats
      - name: Build fork pull requests without the cache
        if: ${{ github.event.pull_request.head.repo.fork }}
        env:
          RUSTC_WRAPPER: ""
        run: |
          cargo build --release

What makes a hit

Machines share only what they build the same way. The keys sccache uses include all of these:

  • The same OS and CPU. A Mac's builds never match Linux CI's, so Macs share with each other and Linux machines with each other.
  • The same compiler version, target, and flags.
  • CARGO_INCREMENTAL=0 for Cargo builds. Incremental compilation can't be cached.
  • The same checkout path on every machine, since sccache's keys include absolute paths. Check out to the same directory on laptops and in CI.
Readers trust whoever can write
The cache matches entries by a hash of their inputs, so it can't tell a harmful build from a good one. Every machine that reads uses whatever was uploaded with a token that can write. Revoking a token stops new writes; clearing the product's entries removes what it already wrote.

Whatever a build outputs is uploaded. Keys are hashes, so environment variables and tokens aren't sent, but a secret compiled into the output, through Rust's env!() or a build.rs or CMake step that writes it into code, is stored like any other output. Keep secrets out of builds, and keep the cache private when in doubt.

4. Check that it works

Run sccache --show-stats after a build. A clean build on a second machine should show mostly hits.

sccache --show-stats
Compile requests                    1342
Compile requests executed           1290
Cache hits                          1251
Cache hits (Rust)                   1251
Cache misses                          39
Cache misses (Rust)                   39
Cache write errors                     0
Non-cacheable calls                   52
  • Cache hits at zero: nothing has been written yet, or this machine builds with a different compiler, flags, or checkout path than the machine that wrote it. See What makes a hit.
  • Cache write errors above zero: the token can only read, an object was larger than 64 MiB, or the cache was busy for a moment. Busy writes succeed on a later build.
  • Many Non-cacheable calls: incremental compilation is on. Set CARGO_INCREMENTAL=0 for builds that should use the cache.

Retention

Entries last seven days from their first upload; reading one doesn't extend it. The first upload of an entry wins: writing it again doesn't replace it, even if the bytes differ. Objects over 64 MiB stay on the machine that built them. Clearing a cache's sccache entries in the dashboard leaves its Nix paths and Go entries alone.

For Nix setup, see Getting started; for Go, see Set up the Go cache.