• Stars
    star
    1,275
  • Rank 36,921 (Top 0.8 %)
  • Language
    TypeScript
  • License
    GNU Lesser Genera...
  • Created about 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A GitHub Action that implements smart caching for rust/cargo projects

Rust Cache Action

A GitHub Action that implements smart caching for rust/cargo projects with sensible defaults.

Example usage

- uses: actions/checkout@v3

# selecting a toolchain either by action or manual `rustup` calls should happen
# before the plugin, as the cache uses the current rustc version as its cache key
- run: rustup toolchain install stable --profile minimal

- uses: Swatinem/rust-cache@v2
  with:
    # The prefix cache key, this can be changed to start a new cache manually.
    # default: "v0-rust"
    prefix-key: ""

    # A cache key that is used instead of the automatic `job`-based key,
    # and is stable over multiple jobs.
    # default: empty
    shared-key: ""

    # An additional cache key that is added alongside the automatic `job`-based
    # cache key and can be used to further differentiate jobs.
    # default: empty
    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.
    # default: "CARGO CC CFLAGS CXX CMAKE RUST"
    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.
    # default: ". -> target"
    workspaces: ""

    # Additional non workspace directories to be cached, separated by newlines.
    cache-directories: ""

    # Determines whether workspace `target` directories are cached.
    # If `false`, only the cargo registry will be cached.
    # default: "true"
    cache-targets: ""

    # Determines if the cache should be saved even when the workflow has failed.
    # default: "false"
    cache-on-failure: ""

    # 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: ""

    # 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,
    # or when only runs from `master` should be saved to the cache.
    # default: "true"
    save-if: ""
    # To only cache runs from `master`:
    save-if: ${{ github.ref == 'refs/head/master' }}

    # Specifies what to use as the backend providing cache
    # Can be set to either "github" or "buildjet"
    # default: "github"
    cache-provider: ""

Further examples are available in the .github/workflows directory.

Outputs

cache-hit

This is a boolean flag that will be set to true when there was an exact cache hit.

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, unless a specific nightly build is being pinned.

Cache Details

This action currently caches the following files/directories:

  • ~/.cargo (installed binaries, the cargo registry, cache, and git dependencies)
  • ./target (build artifacts of dependencies)

This cache is automatically keyed by:

  • the github job_id,
  • the rustc release / host / hash,
  • the value of some compiler-specific environment variables (eg. RUSTFLAGS, etc), and
  • 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).
  • a hash of all .cargo/config.toml files in the root of the repository (if present).

An additional input key can be provided if the builtin keys are not sufficient.

Before being persisted, the cache is cleaned of:

  • 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. 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.

The action will try to restore from a previous Cargo.lock version as well, so lockfile updates should only re-build changed dependencies.

The action invokes cargo metadata to determine the current set of dependencies.

Additionally, the action automatically works around cargo#8603 / actions/cache#403 which would otherwise corrupt the cache on macOS builds.

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: Caching dependencies to speed up workflows

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 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 to see those details as well as further details related to caching operations.

Known issues

  • The cache cleaning process currently removes all the files from ~/.cargo/bin that were present before the action ran (for example rustc).

More Repositories

1

rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
TypeScript
804
star
2

esgraph

creates a control flow graph from an esprima abstract syntax tree
JavaScript
168
star
3

diff

implementation of myers diff algorithm
JavaScript
82
star
4

virtualdom

tools to work with a virtual dom
JavaScript
31
star
5

jade-virtualdom

compile jade templates to virtualdom
JavaScript
21
star
6

proxy

Small shim providing `new Proxy(target, handler)`
JavaScript
14
star
7

walkes

very simple walker for estree AST
JavaScript
12
star
8

scroll-beyond

Very simple gedit plugin to scroll past the end of documents
Python
12
star
9

cow

copy on write deep, cyclic js objects
JavaScript
9
star
10

analyses

basic data flow analyses framework based on esprima
JavaScript
9
star
11

fucov

A GitHub Action that does single-action code coverage generation.
TypeScript
8
star
12

scrollbars

An efficient overflow area with custom scrollbars
JavaScript
8
star
13

wamp1

Very simple WAMP v1 implementation
JavaScript
7
star
14

perf-cpuprofile

Converts linux `perf` tool output to .cpuprofile files readable by chromiums devtools
JavaScript
7
star
15

tern_for_gedit

This is a simple gedit plugin that provides code completion results based on tern
Python
6
star
16

next-monorepo

reproduction for some problems with monorepos
TypeScript
4
star
17

analyses-old

Static Analysis based on Esprima
JavaScript
3
star
18

inferences

type inference for js
JavaScript
3
star
19

rust-maintain

A GitHub Action to do crate maintenance
Rust
3
star
20

stream-utils

some utilities for whatwg streams
JavaScript
2
star
21

hexite

TypeScript
2
star
22

psy

Rust
2
star
23

yavd

yet another virtual dom library
JavaScript
2
star
24

codingcontest-2018-11-16

https://register.codingcontest.org/listing/6-2018-11-16
TypeScript
2
star
25

model

transparent ES5 models
JavaScript
2
star
26

fondoeh

Rust
2
star
27

channel-utils

1
star
28

motorina

too early to reveal :-)
TypeScript
1
star
29

fast-babel-register

Temporary fork until @babel/register upstream is fast enough
JavaScript
1
star
30

model-cast

Automatically cast properties for Swatinem/model
JavaScript
1
star
31

dotfiles

Vim Script
1
star
32

style-canvas

Component to apply CSS styles to a canvas
JavaScript
1
star
33

pinch

experiments with csp
JavaScript
1
star
34

normalize-serve-jade

Opinionated jade server for frontend development workflow using Normalize.IO
JavaScript
1
star
35

depstalk

Scrape the dependents of a GitHub repository
Rust
1
star
36

component-istanbul

instruments component files with istanbul coverage
JavaScript
1
star
37

observ-delegate

A observable value representation that delegates to a different observable value
JavaScript
1
star
38

crid

Crid - The cryptographic ID serializer
Rust
1
star
39

websocket-buffering

small wrapper around websocket that does message buffering and reconnects
JavaScript
1
star
40

component-linknpm

Link downloaded components into node_modules
JavaScript
1
star
41

co-promisify

Seems like I have duplicated this. So just use
JavaScript
1
star