actions / cache

Cache dependencies and build outputs in GitHub Actions
MIT License
4.49k stars 1.2k forks source link

Add an example enabling Rust incremental builds #1185

Open jorendorff opened 1 year ago

jorendorff commented 1 year ago

Description

Add an example of how to get the most out of Rust incremental builds.

Also, changes to rust-toolchain.toml should invalidate the cache.

Motivation and Context

Our Rust builds were taking too long. Rust has a notoriously slow compiler, and we expect many users have the same problem.

Rust's compiler has a feature to help in this situation: incremental builds. actions/cache does not directly support having each job start with the build directory produced by the previous successful build. But Rust's compiler can save a significant amount of work if you do things this way. In each build, it only has to compile the code that has actually changed.

With a typical actions/cache configuration, Rust ends up doing more and more work with each push, as the changes between the cached build and the current build accumulate. With the example added in this PR, the cached build will be the latest. Incremental builds will then kick in automatically (you don't have to pass any special flags to cargo).

The only downside is that this produces more cache entries. It creates a new cache entry after every successful run, not just when the cache is invalidated. I don't know if that's likely to make problems for anyone. If so, let's talk about it.

Adding rust-toolchain.toml is an independent, minor change. If your project has that file, it selects which version of the Rust compiler you're using and which components of Rust to install. When you change versions, Rust will recompile all your code anyway, so there's little point using the cache.

How Has This Been Tested?

We've been using this technique in @github/blackbird for months now.

Types of changes

Checklist: