Using sccache in GitHub Actions

Background

I've setup an example repository naftulikay/rust-examples for a fairly self-explanatory purpose: to experiment with and demonstrate successful usage of various crates and standard library paradigms.

I wrote a shell script which installs the requested sccache binary for the operating system and architecture (only supports Linux for now, which is fine for my case), and I export RUSTC_WRAPPER to the path of sccache, which is /home/runner/.cargo/bin/sccache. It works extremely well and Rust builds are now usually bottlenecked by the download of the cache from previous builds. cargo build --release --all runs in less than a second for only source code changes (no dep changes).

This is, in essence what my actions look like:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-rust-${{ env.CACHE_VERSION }}-build-${{ hashFiles('**/Cargo.toml') }}
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ~/.cache/sccache/
            ~/.rustup/
            target/
          restore-keys: |
            ${{ runner.os }}-rust-${{ env.CACHE_VERSION }}-build-
            ${{ runner.os }}-rust-${{ env.CACHE_VERSION }}
      - run: lib/install-sccache
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo build --all --release

Once again, this works, but my question relates to something else.

If anyone sees something I should/should not be caching, would appreciate the input, though :+1:

Question

sccache uses OpenDAL under the hood for storage of cache artifacts, and sccache (and OpenDAL) support a GitHub Actions cache, documented here, and the recommended code to use in the workflow is:

- name: Configure Cache Env
  uses: actions/github-script@v6
  with:
    script: |
      core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
      core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

I'm not exactly sure what this does above and beyond what I am doing, and that's my question: what does this do, and should I be using it? I'm storing ~/cache/sccache in my cache, so I don't know what this would give me above and beyond what I am doing.

Hi, the difference here lies in the types of storage that sccache utilizes.

If no storage type is specified, sccache defaults to using the local filesystem at ~/.cache/sccache/. Your action will package all caches in this directory and retrieve them upon a cache hit.

When sccache is enabled with ghac by SCCACHE_GHA_ENABLED, it utilizes ghac as its storage backend, directly reading from and writing to ghac without any interaction with the local filesystem.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.