2020-09-25 22:42:39 +08:00
|
|
|
# Rust Cache Action
|
|
|
|
|
2020-10-04 00:10:54 +08:00
|
|
|
A GitHub Action that implements smart caching for rust/cargo projects with
|
|
|
|
sensible defaults.
|
2020-09-25 22:42:39 +08:00
|
|
|
|
|
|
|
## Example usage
|
|
|
|
|
|
|
|
```yaml
|
2022-07-16 18:38:38 +08:00
|
|
|
- uses: actions/checkout@v3
|
2021-09-28 22:03:51 +08:00
|
|
|
|
2020-12-08 06:56:50 +08:00
|
|
|
# selecting a toolchain either by action or manual `rustup` calls should happen
|
2022-07-16 18:38:38 +08:00
|
|
|
# before the plugin, as the cache uses the current rustc version as its cache key
|
|
|
|
- run: rustup toolchain install stable --profile minimal
|
2020-12-08 06:56:50 +08:00
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
- uses: Swatinem/rust-cache@v2
|
|
|
|
with:
|
2022-11-06 16:37:23 +08:00
|
|
|
# The prefix cache key, this can be changed to start a new cache manually.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: "v0-rust"
|
|
|
|
prefix-key: ""
|
|
|
|
|
2022-11-06 16:37:23 +08:00
|
|
|
# A cache key that is used instead of the automatic `job`-based key,
|
|
|
|
# and is stable over multiple jobs.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: empty
|
2022-07-16 18:38:38 +08:00
|
|
|
shared-key: ""
|
|
|
|
|
|
|
|
# An additional cache key that is added alongside the automatic `job`-based
|
|
|
|
# cache key and can be used to further differentiate jobs.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: empty
|
2022-07-16 18:38:38 +08:00
|
|
|
key: ""
|
|
|
|
|
|
|
|
# A whitespace separated list of env-var *prefixes* who's value contributes
|
|
|
|
# to the environment cache key.
|
|
|
|
# The env-vars are matched by *prefix*, so the default `RUST` var will
|
|
|
|
# match all of `RUSTC`, `RUSTUP_*`, `RUSTFLAGS`, `RUSTDOC_*`, etc.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: "CARGO CC CFLAGS CXX CMAKE RUST"
|
2022-07-16 18:38:38 +08:00
|
|
|
env-vars: ""
|
|
|
|
|
|
|
|
# The cargo workspaces and target directory configuration.
|
|
|
|
# These entries are separated by newlines and have the form
|
|
|
|
# `$workspace -> $target`. The `$target` part is treated as a directory
|
|
|
|
# relative to the `$workspace` and defaults to "target" if not explicitly given.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: ". -> target"
|
2022-07-16 18:38:38 +08:00
|
|
|
workspaces: ""
|
|
|
|
|
2022-11-06 16:37:23 +08:00
|
|
|
# Additional non workspace directories to be cached, separated by newlines.
|
2022-10-19 05:05:42 +08:00
|
|
|
cache-directories: ""
|
|
|
|
|
|
|
|
# Determines whether workspace `target` directories are cached.
|
2022-11-06 16:37:23 +08:00
|
|
|
# If `false`, only the cargo registry will be cached.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: "true"
|
|
|
|
cache-targets: ""
|
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
# Determines if the cache should be saved even when the workflow has failed.
|
2022-10-19 05:05:42 +08:00
|
|
|
# default: "false"
|
2022-07-16 18:38:38 +08:00
|
|
|
cache-on-failure: ""
|
2022-11-07 02:15:00 +08:00
|
|
|
|
2023-05-12 05:15:09 +08:00
|
|
|
# Determines which crates are cached.
|
|
|
|
# If `true` all crates will be cached, otherwise only dependent crates will be cached.
|
|
|
|
# Useful if additional crates are used for CI tooling.
|
|
|
|
# default: "false"
|
|
|
|
cache-all-crates: ""
|
|
|
|
|
2022-11-07 02:15:00 +08:00
|
|
|
# Determiners whether the cache should be saved.
|
|
|
|
# If `false`, the cache is only restored.
|
|
|
|
# Useful for jobs where the matrix is additive e.g. additional Cargo features.
|
|
|
|
# default: "true"
|
|
|
|
save-if: ""
|
2020-09-25 22:42:39 +08:00
|
|
|
```
|
|
|
|
|
2022-09-19 14:55:11 +08:00
|
|
|
Further examples are available in the [.github/workflows](./.github/workflows/) directory.
|
2021-06-29 05:18:07 +08:00
|
|
|
|
2021-01-29 01:40:43 +08:00
|
|
|
## Outputs
|
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
**`cache-hit`**
|
2021-01-29 01:40:43 +08:00
|
|
|
|
|
|
|
This is a boolean flag that will be set to `true` when there was an exact cache hit.
|
|
|
|
|
2021-01-10 16:42:14 +08:00
|
|
|
## Cache Effectiveness
|
|
|
|
|
|
|
|
This action only caches the _dependencies_ of a crate, so is more effective if
|
|
|
|
the dependency / own code ratio is higher.
|
|
|
|
|
|
|
|
It is also most effective for repositories with a `Cargo.lock` file. Library
|
|
|
|
repositories with only a `Cargo.toml` file have limited benefits, as cargo will
|
|
|
|
_always_ use the most up-to-date dependency versions, which may not be cached.
|
|
|
|
|
|
|
|
Usage with Stable Rust is most effective, as a cache is tied to the Rust version.
|
|
|
|
Using it with Nightly Rust is less effective as it will throw away the cache every day.
|
|
|
|
|
2020-12-08 06:56:50 +08:00
|
|
|
## Cache Details
|
2020-10-04 00:39:38 +08:00
|
|
|
|
2022-03-05 17:04:16 +08:00
|
|
|
This action currently caches the following files/directories:
|
2020-09-25 22:42:39 +08:00
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
- `~/.cargo` (installed binaries, the cargo registry, cache, and git dependencies)
|
|
|
|
- `./target` (build artifacts of dependencies)
|
2020-09-25 22:42:39 +08:00
|
|
|
|
2020-10-04 00:10:54 +08:00
|
|
|
This cache is automatically keyed by:
|
2020-09-25 22:42:39 +08:00
|
|
|
|
2022-03-05 17:04:16 +08:00
|
|
|
- the github [`job_id`](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_id),
|
2022-07-16 18:38:38 +08:00
|
|
|
- the rustc release / host / hash,
|
|
|
|
- the value of some compiler-specific environment variables (eg. RUSTFLAGS, etc), and
|
2022-03-05 17:04:16 +08:00
|
|
|
- a hash of all `Cargo.lock` / `Cargo.toml` files found anywhere in the repository (if present).
|
|
|
|
- a hash of all `rust-toolchain` / `rust-toolchain.toml` files in the root of the repository (if present).
|
2020-09-25 22:42:39 +08:00
|
|
|
|
2020-10-04 00:39:38 +08:00
|
|
|
An additional input `key` can be provided if the builtin keys are not sufficient.
|
|
|
|
|
2022-03-05 17:04:16 +08:00
|
|
|
Before being persisted, the cache is cleaned of:
|
2022-07-16 18:38:38 +08:00
|
|
|
|
2022-03-05 17:04:16 +08:00
|
|
|
- Any files in `~/.cargo/bin` that were present before the action ran (for example `rustc`).
|
|
|
|
- Dependencies that are no longer used.
|
|
|
|
- Anything that is not a dependency.
|
|
|
|
- Incremental build artifacts.
|
|
|
|
- Any build artifacts with an `mtime` older than one week.
|
|
|
|
|
|
|
|
In particular, the workspace crates themselves are not cached since doing so is
|
|
|
|
[generally not effective](https://github.com/Swatinem/rust-cache/issues/37#issuecomment-944697938).
|
|
|
|
For this reason, this action automatically sets `CARGO_INCREMENTAL=0` to disable
|
|
|
|
incremental compilation, so that the Rust compiler doesn't waste time creating
|
|
|
|
the additional artifacts required for incremental builds.
|
|
|
|
|
|
|
|
The `~/.cargo/registry/src` directory is not cached since it is quicker for Cargo
|
|
|
|
to recreate it from the compressed crate archives in `~/.cargo/registry/cache`.
|
2020-10-04 00:39:38 +08:00
|
|
|
|
2020-11-21 19:21:26 +08:00
|
|
|
The action will try to restore from a previous `Cargo.lock` version as well, so
|
|
|
|
lockfile updates should only re-build changed dependencies.
|
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
The action invokes `cargo metadata` to determine the current set of dependencies.
|
|
|
|
|
2020-11-21 19:21:26 +08:00
|
|
|
Additionally, the action automatically works around
|
|
|
|
[cargo#8603](https://github.com/rust-lang/cargo/issues/8603) /
|
|
|
|
[actions/cache#403](https://github.com/actions/cache/issues/403) which would
|
|
|
|
otherwise corrupt the cache on macOS builds.
|
2022-03-05 17:04:16 +08:00
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
## Cache Limits and Control
|
|
|
|
|
|
|
|
This specialized cache action is built on top of the upstream cache action
|
|
|
|
maintained by GitHub. The same restrictions and limits apply, which are
|
|
|
|
documented here:
|
2023-05-19 04:48:40 +08:00
|
|
|
[Caching dependencies to speed up workflows](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows)
|
2022-07-16 18:38:38 +08:00
|
|
|
|
|
|
|
In particular, caches are currently limited to 10 GB in total and exceeding that
|
|
|
|
limit will cause eviction of older caches.
|
|
|
|
|
|
|
|
Caches from base branches are available to PRs, but not across unrelated
|
|
|
|
branches.
|
|
|
|
|
|
|
|
The caches can be controlled using the [Cache API](https://docs.github.com/en/rest/actions/cache)
|
|
|
|
which allows listing existing caches and manually removing entries.
|
|
|
|
|
|
|
|
## Debugging
|
|
|
|
|
|
|
|
The action prints detailed information about which information it considers
|
|
|
|
for its cache key, and it outputs more debug-only information about which
|
|
|
|
cleanup steps it performs before persisting the cache.
|
|
|
|
|
|
|
|
You can read up on how to [enable debug logging](https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging)
|
|
|
|
to see those details as well as further details related to caching operations.
|
|
|
|
|
2022-03-05 17:04:16 +08:00
|
|
|
## Known issues
|
|
|
|
|
2022-07-16 18:38:38 +08:00
|
|
|
- The cache cleaning process currently removes all the files from `~/.cargo/bin`
|
|
|
|
that were present before the action ran (for example `rustc`).
|