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
+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');
});