Skip to content

Commit 4df931d

Browse files
authored
[CI] Pre-fetch and cache Cargo dependencies
In this commit, we optimize the population of the global Cargo registry and cache in the CI workflow. By populating the cache before running the jobs in the matrix, we avoid redundant downloads of crates from the internet, improving overall workflow efficiency.
1 parent 81e13f1 commit 4df931d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

.github/workflows/ci.yml

+43
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ env:
3131
jobs:
3232
build_test:
3333
runs-on: ubuntu-latest
34+
# Generate and populate the global Cargo registry and cache first. Each
35+
# job in the matrix runs in parallel, so without populating the cache
36+
# first, most jobs would duplicate the work of downloading crates from
37+
# the internet. Populating the cache first ensures that this work only
38+
# happens once.
39+
needs: generate_cache
3440

3541
strategy:
3642
# By default, this is set to `true`, which means that a single CI job
@@ -78,6 +84,13 @@ jobs:
7884
steps:
7985
- uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
8086

87+
- uses: actions/cache@v3
88+
with:
89+
path: |
90+
~/.cargo/registry/index/
91+
~/.cargo/registry/cache/
92+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
93+
8194
# We use toolchain descriptors ("msrv", "stable", and "nightly") in the
8295
# matrix. This step converts the current descriptor to a particular
8396
# toolchain version by looking up the corresponding key in `Cargo.toml`. It
@@ -358,3 +371,33 @@ jobs:
358371
| tee -a $GITHUB_STEP_SUMMARY >&2
359372
exit 1
360373
fi
374+
375+
generate_cache:
376+
runs-on: ubuntu-latest
377+
name: Generate cache
378+
steps:
379+
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
380+
381+
- uses: actions/cache@v3
382+
with:
383+
path: |
384+
~/.cargo/registry/index/
385+
~/.cargo/registry/cache/
386+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
387+
388+
- name: Populate cache
389+
run: |
390+
# Ensure all dependencies are downloaded - both for our crates and for tools
391+
# we use in CI. We don't care about these tools succeeding for two reasons:
392+
# First, this entire job is best-effort since it's just a performance optimization.
393+
# Second, there may be failures due to issues other than failing to download
394+
# dependencies (e.g., `cargo metadata` called with a malformed `Cargo.toml`,
395+
# build failure in our own crate or in dependencies, etc). For those reasons,
396+
# we discard stderr and ignore status codes.
397+
#
398+
# For downloading our crates' dependencies in particular, note that there is
399+
# no support for doing this directly [1], so we just check all crates using --tests.
400+
#
401+
# [1] https://stackoverflow.com/a/42139535/836390
402+
cargo check --workspace --tests &> /dev/null || true
403+
cargo metadata &> /dev/null || true

0 commit comments

Comments
 (0)