ci: save release and dependency-check Rust caches only on main (#10758)

* ci: save release and dependency-check Rust caches only on main

The macOS and Windows release builds and the dependency check saved Rust
caches on every ref, including tags and PRs. Those caches are unreachable
from other refs and evict main's caches. Save them only on main, and
extend the cache policy test to every workflow and composite action.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* ci: correct the macOS rust-cache key comment

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
dennis zhuang
2026-11-30 15:36:51 +00:00
committed by GitHub
parent 187facb58f
commit cafa744e51
5 changed files with 77 additions and 13 deletions
@@ -22,18 +22,6 @@ inputs:
runs:
using: composite
steps:
- name: Cache cargo assets
id: cache
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ inputs.arch }}-build-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install protoc
shell: bash
run: |
@@ -43,6 +31,15 @@ runs:
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: ${{ inputs.arch }}
cache: false
- name: Rust Cache
uses: Swatinem/rust-cache@v2
with:
# The two macOS matrix jobs share github.job and the runner type, and the
# default key does not include the target triple.
key: ${{ inputs.arch }}
save-if: ${{ github.ref == 'refs/heads/main' }}
- name: Start etcd # For integration tests.
if: ${{ inputs.disable-run-tests == 'false' }}
@@ -31,9 +31,12 @@ runs:
with:
target: ${{ inputs.arch }}
components: llvm-tools-preview
cache: false
- name: Rust Cache
uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
- name: Install WSL distribution
uses: Vampire/setup-wsl@v2
+6
View File
@@ -16,6 +16,12 @@ A manual Rust CI run on main can warm the compilation caches sooner. These jobs
keep their separate keys and existing runner types; no extra warmup job is added.
Coverage caches are not warmed by these schedules.
PR, merge-group, and non-main manual runs restore caches without saving them.
The same applies to Rust build caches in every other workflow and composite
action. Release builds save the macOS and Windows caches only from main (the
scheduled nightly release or a manual run on main); tag builds restore them.
GitHub lets a run restore only caches saved by its own ref or the default
branch, so a Rust cache saved by a tag or PR cannot be reused elsewhere and only
evicts main's caches.
Fuzz binaries still flow to downstream jobs through workflow artifacts on every
run; only the persistent binary cache is main-written. Re-running a PR cannot
reuse a newly saved PR-specific binary cache under this policy.
+57 -1
View File
@@ -1,5 +1,5 @@
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const { existsSync, readdirSync, readFileSync } = require('node:fs');
const { resolve } = require('node:path');
const { test } = require('node:test');
@@ -153,3 +153,59 @@ test('warmup keeps scope exclusions, dependency failures and cancellation gates'
}
}
});
// The tests above parse four workflows with fixed indentation. This one covers
// every workflow and composite action, so caches added elsewhere (release
// builds, single-purpose PR checks) follow the same main-only write policy.
function allWorkflowAndActionFiles() {
const root = resolve(__dirname, '../..');
const files = readdirSync(resolve(root, 'workflows'))
.filter(file => /\.ya?ml$/.test(file))
.map(file => resolve(root, 'workflows', file));
for (const action of readdirSync(resolve(root, 'actions'))) {
for (const file of ['action.yml', 'action.yaml']) {
const path = resolve(root, 'actions', action, file);
if (existsSync(path)) files.push(path);
}
}
return files.map(path => ({ path, text: readFileSync(path, 'utf8') }));
}
function anySteps(text) {
return text.split(/^\s*- (?=(?:name|uses|id|if|run|shell|with|env):)/m).slice(1);
}
test('every Rust cache in workflows and composite actions saves only on main', () => {
// apidoc runs only on pushes to main, so its implicit cache is main-scoped.
const implicitCacheAllowed = ['apidoc.yml'];
let toolchains = 0;
let caches = 0;
for (const { path, text } of allWorkflowAndActionFiles()) {
for (const step of anySteps(text)) {
if (/uses: actions\/cache@/.test(step)) {
assert.fail(path + ': actions/cache saves on every ref; use rust-cache with save-if');
}
if (/uses: actions-rust-lang\/setup-rust-toolchain@/.test(step)) {
if (!implicitCacheAllowed.some(name => path.endsWith(name))) {
assert.match(step, /^\s+cache: false$/m, path);
}
toolchains++;
}
if (/uses: Swatinem\/rust-cache@/.test(step)) {
const condition = step.match(/^\s+save-if: (.+)$/m)?.[1];
assert.ok(condition, path + ': rust-cache without save-if');
const expression = condition.replace(/^\$\{\{\s*|\s*\}\}$/g, '');
const saves = github => new Function('github', 'return (' + expression + ');')(github);
for (const [event_name, ref] of [
['pull_request', 'refs/pull/123/merge'],
['merge_group', 'refs/heads/gh-readonly-queue/main/pr-123'],
['workflow_dispatch', 'refs/heads/feature'],
['push', 'refs/tags/v1.3.2'],
]) {
assert.equal(saves({ event_name, ref }), false, path + ' saves on ' + ref);
}
caches++;
}
}
}
assert.ok(toolchains >= 20 && caches >= 15, 'expected all toolchain and cache steps to be scanned');
});
+2
View File
@@ -19,6 +19,8 @@ jobs:
- name: Set up Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
cache: false
- name: Run cargo tree
run: cargo tree --prefix none > dependencies.txt