Cache the crates index and package cache for jobs that build rust (#5293)

Rework the install-rust action to include caching of the crates index and cached downloaded crates.
This commit is contained in:
Trevor Elliott
2022-11-17 17:07:38 -08:00
committed by GitHub
parent 9a44ef7443
commit b6aeaf4fe5
2 changed files with 82 additions and 39 deletions

View File

@@ -1,12 +1,91 @@
name: 'Install Rust toolchain'
description: 'Install both `rustup` and a Rust toolchain'
description: 'Install a rust toolchain and cache the crates index'
inputs:
toolchain:
description: 'Default toolchan to install'
required: false
default: 'stable'
lockfiles:
description: 'Path glob for Cargo.lock files to use as cache keys'
required: false
default: '**/Cargo.lock'
runs:
using: node16
main: 'main.js'
using: composite
steps:
- name: Install Rust
shell: bash
run: |
if [[ "${{ runner.os }}" = "Windows" ]]; then
rustup self update
fi
rustup set profile minimal
rustup update "${{ inputs.toolchain }}" --no-self-update
rustup default "${{ inputs.toolchain }}"
# Save disk space by avoiding incremental compilation. Also turn down
# debuginfo from 2 to 1 to help save disk space.
cat >> "$GITHUB_ENV" <<EOF
CARGO_INCREMENTAL=0
CARGO_PROFILE_DEV_DEBUG=1
CARGO_PROFILE_TEST_DEBUG=1
EOF
# Deny warnings on CI to keep our code warning-free as it lands in-tree.
# Don't do this on nightly though, since there's a fair amount of
# warning churn there.
if [[ "${{ inputs.toolchain }}" != nightly* ]]; then
echo RUSTFLAGS="-D warnings" >> "$GITHUB_ENV"
fi
if [[ "${{ runner.os }}" = "macOS" ]]; then
cat >> "$GITHUB_ENV" <<EOF
CARGO_PROFILE_DEV_SPLIT_DEBUGINFO=unpacked
CARGO_PROFILE_TEST_SPLIT_DEBUGINFO=unpacked
EOF
fi
- name: Choose registry cache key
shell: bash
# Update the registry index cache at most once per day. actions/cache
# won't write changes back to the cache if the cache key already exists,
# so this means every job may have to re-download the index entries which
# are new since the last time the cache key changed. Changing the cache
# key relatively frequently keeps the amount of duplicated work down. But
# changing it too frequently means we might hit the 10GB quota too
# quickly, which would cause GitHub to evict other caches we still want.
run: echo CARGO_REGISTRY_CACHE_KEY=$(date +%Y%m%d) >> $GITHUB_ENV
- name: Cache Cargo registry index
uses: actions/cache@v3
with:
path: ~/.cargo/registry/index/
key: cargo-registry-${{ env.CARGO_REGISTRY_CACHE_KEY }}
# Any older registry-index cache is still valid. It's a git clone, so
# cargo only has to pull down the changes since the index was cached.
restore-keys: cargo-registry-
- name: Cache crate sources for dependencies
uses: actions/cache@v3
with:
path: |
~/.cargo/registry/cache/
~/.cargo/git/db/
key: cargo-crates-${{ inputs.lockfiles }}-${{ hashFiles(inputs.lockfiles) }}
# If Cargo.lock has changed, we probably will need to get the source
# code for some crates we don't already have. But any crates we have
# source cached for are still valid. The only problem is nothing
# removes old crate sources from the cache, so using `restore-keys`
# this way may use more of our GitHub cache quota than we'd like.
#
# Also, scope this cache by which Cargo.lock we're building from.
# Otherwise, whichever job writes the cache first will get its
# dependencies cached, and that cache will be used as the basis for the
# next job, even though odds are pretty good the cache is useless.
restore-keys: cargo-crates-${{ inputs.lockfiles }}-
# TODO: on cache miss, after cargo has updated the registry index, run `git gc`

View File

@@ -1,36 +0,0 @@
const child_process = require('child_process');
const toolchain = process.env.INPUT_TOOLCHAIN;
const fs = require('fs');
function set_env(name, val) {
fs.appendFileSync(process.env['GITHUB_ENV'], `${name}=${val}\n`)
}
// Needed for now to get 1.24.2 which fixes a bug in 1.24.1 that causes issues
// on Windows.
if (process.platform === 'win32') {
child_process.execFileSync('rustup', ['self', 'update']);
}
child_process.execFileSync('rustup', ['set', 'profile', 'minimal']);
child_process.execFileSync('rustup', ['update', toolchain, '--no-self-update']);
child_process.execFileSync('rustup', ['default', toolchain]);
// Deny warnings on CI to keep our code warning-free as it lands in-tree. Don't
// do this on nightly though since there's a fair amount of warning churn there.
if (!toolchain.startsWith('nightly')) {
set_env("RUSTFLAGS", "-D warnings");
}
// Save disk space by avoiding incremental compilation, and also we don't use
// any caching so incremental wouldn't help anyway.
set_env("CARGO_INCREMENTAL", "0");
// Turn down debuginfo from 2 to 1 to help save disk space
set_env("CARGO_PROFILE_DEV_DEBUG", "1");
set_env("CARGO_PROFILE_TEST_DEBUG", "1");
if (process.platform === 'darwin') {
set_env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "unpacked");
set_env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "unpacked");
}