From b6aeaf4fe59ee0576a850c2518c4e6063e78bf75 Mon Sep 17 00:00:00 2001 From: Trevor Elliott Date: Thu, 17 Nov 2022 17:07:38 -0800 Subject: [PATCH] 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. --- .github/actions/install-rust/action.yml | 85 ++++++++++++++++++++++++- .github/actions/install-rust/main.js | 36 ----------- 2 files changed, 82 insertions(+), 39 deletions(-) delete mode 100644 .github/actions/install-rust/main.js diff --git a/.github/actions/install-rust/action.yml b/.github/actions/install-rust/action.yml index 6afc01d6f2..e63fcc4eb2 100644 --- a/.github/actions/install-rust/action.yml +++ b/.github/actions/install-rust/action.yml @@ -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" <> "$GITHUB_ENV" + fi + + if [[ "${{ runner.os }}" = "macOS" ]]; then + cat >> "$GITHUB_ENV" <> $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` diff --git a/.github/actions/install-rust/main.js b/.github/actions/install-rust/main.js deleted file mode 100644 index b144d70aea..0000000000 --- a/.github/actions/install-rust/main.js +++ /dev/null @@ -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"); -}