From cafa744e5144a52f1b98f2ced25dc1792cbc5263 Mon Sep 17 00:00:00 2001 From: dennis zhuang Date: Mon, 30 Nov 2026 15:36:51 +0000 Subject: [PATCH] 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 * ci: correct the macOS rust-cache key comment Signed-off-by: Dennis Zhuang --------- Signed-off-by: Dennis Zhuang --- .../actions/build-macos-artifacts/action.yml | 21 +++---- .../build-windows-artifacts/action.yml | 3 + .github/ci/cache-policy.md | 6 ++ .../scripts/tests/ci-cache-policy.test.cjs | 58 ++++++++++++++++++- .github/workflows/dependency-check.yml | 2 + 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/.github/actions/build-macos-artifacts/action.yml b/.github/actions/build-macos-artifacts/action.yml index 2affb5b94f7..3f1fad19f10 100644 --- a/.github/actions/build-macos-artifacts/action.yml +++ b/.github/actions/build-macos-artifacts/action.yml @@ -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' }} diff --git a/.github/actions/build-windows-artifacts/action.yml b/.github/actions/build-windows-artifacts/action.yml index d25dd27e117..b347f3258bc 100644 --- a/.github/actions/build-windows-artifacts/action.yml +++ b/.github/actions/build-windows-artifacts/action.yml @@ -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 diff --git a/.github/ci/cache-policy.md b/.github/ci/cache-policy.md index d1eb053ec1c..43d02fd862a 100644 --- a/.github/ci/cache-policy.md +++ b/.github/ci/cache-policy.md @@ -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. diff --git a/.github/scripts/tests/ci-cache-policy.test.cjs b/.github/scripts/tests/ci-cache-policy.test.cjs index ba6817424c6..12a3057a843 100644 --- a/.github/scripts/tests/ci-cache-policy.test.cjs +++ b/.github/scripts/tests/ci-cache-policy.test.cjs @@ -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'); +}); diff --git a/.github/workflows/dependency-check.yml b/.github/workflows/dependency-check.yml index 014a41e45c6..e5d6e036a85 100644 --- a/.github/workflows/dependency-check.yml +++ b/.github/workflows/dependency-check.yml @@ -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