From ef1fd7a195e6756f288283286c8e6c27d41c4a72 Mon Sep 17 00:00:00 2001 From: luo jiyin Date: Tue, 29 Sep 2026 12:35:50 +0800 Subject: [PATCH] ci: reduce change planner startup time --- .github/scripts/ci-plan.mjs | 10 +++++++--- .github/scripts/ci-plan.test.mjs | 14 +++++++++++++- .github/scripts/ci-workflow.test.mjs | 5 +++++ .github/workflows/ci.yml | 18 ++++++++++++++---- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.github/scripts/ci-plan.mjs b/.github/scripts/ci-plan.mjs index b35f56b45d..e8982ad3d9 100644 --- a/.github/scripts/ci-plan.mjs +++ b/.github/scripts/ci-plan.mjs @@ -26,6 +26,12 @@ export function knownJavaDrivers(root, nativeDrivers) { } } +export function cargoMetadata(root, execute = execFileSync) { + return JSON.parse(execute("cargo", ["+stable", "metadata", "--locked", "--offline", "--no-deps", "--format-version", "1"], { + cwd: root, encoding: "utf8", maxBuffer: 16 * 1024 * 1024, + })); +} + export function planCi({ files, metadata, root, eventName = "pull_request", rustChanged = false, agentsChanged = false, javaDrivers }) { const unknownDiff = files === null; files ??= []; @@ -97,9 +103,7 @@ export function planCi({ files, metadata, root, eventName = "pull_request", rust if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) { const root = process.cwd(); - const metadata = JSON.parse(execFileSync("cargo", ["metadata", "--locked", "--offline", "--no-deps", "--format-version", "1"], { - cwd: root, encoding: "utf8", maxBuffer: 16 * 1024 * 1024, - })); + const metadata = cargoMetadata(root); const plan = planCi({ files: changedPaths(process.env.BASE_SHA, root), metadata, root, eventName: process.env.GITHUB_EVENT_NAME, rustChanged: process.env.RUST_CHANGED === "true", agentsChanged: process.env.AGENTS_CHANGED === "true", diff --git a/.github/scripts/ci-plan.test.mjs b/.github/scripts/ci-plan.test.mjs index be478f6b6b..57e3b98b72 100644 --- a/.github/scripts/ci-plan.test.mjs +++ b/.github/scripts/ci-plan.test.mjs @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; import path from "node:path"; import { execFileSync } from "node:child_process"; import { goAgents, integrationCases, rustGroups } from "./ci-config.mjs"; -import { changedPaths, planCi } from "./ci-plan.mjs"; +import { cargoMetadata, changedPaths, planCi } from "./ci-plan.mjs"; import { rustCommand } from "./ci-rust.mjs"; import { gateFailures } from "./ci-gate.mjs"; import { assertCoverage, parseCoverage } from "./ci-rust-coverage.mjs"; @@ -40,6 +40,18 @@ const metadata = { }; const plan = (files, options = {}) => planCi({ files, metadata, root, ...options }); const groups = (result) => result.rust_matrix.include.map((entry) => entry.group); + +test("the planner uses the runner stable Cargo only for metadata", () => { + let invocation; + const result = cargoMetadata("/workspace", (command, args, options) => { + invocation = { command, args, options }; + return '{"packages":[],"workspace_members":[]}'; + }); + assert.deepEqual(result, { packages: [], workspace_members: [] }); + assert.equal(invocation.command, "cargo"); + assert.deepEqual(invocation.args, ["+stable", "metadata", "--locked", "--offline", "--no-deps", "--format-version", "1"]); + assert.equal(invocation.options.cwd, "/workspace"); +}); // Keep the gate fixtures in sync with ci-gate.mjs routedJobs. Both Windows jobs // share the windows_win7_bundle routing output. const routedJobs = { frontend: "frontend", packages: "packages", "github-scripts": "github_scripts", diff --git a/.github/scripts/ci-workflow.test.mjs b/.github/scripts/ci-workflow.test.mjs index 522a986d0c..e19a95215f 100644 --- a/.github/scripts/ci-workflow.test.mjs +++ b/.github/scripts/ci-workflow.test.mjs @@ -172,7 +172,12 @@ test("Windows compatibility jobs cache Rust compilation without wrapping C or C+ test("the planner uses the exact event base and preserves a single workflow cancellation scope", () => { const changes = job("changes"); assert.ok(changes.includes("github.event.pull_request.base.sha || github.event.before")); + assert.ok(changes.includes('git fetch --no-tags --depth=1 origin "$BASE_SHA"')); + assert.ok(changes.includes("base: ${{ steps.change-base.outputs.sha }}")); + assert.ok(changes.includes("BASE_SHA: ${{ steps.change-base.outputs.sha }}")); assert.ok(changes.includes("node .github/scripts/ci-plan.mjs")); + assert.doesNotMatch(changes, /fetch-depth:\s*0/); + assert.doesNotMatch(changes, /dtolnay\/rust-toolchain/); for (const flag of ["rust", "rust_full", "rust_matrix", "agents", "agent_go", "agent_rust", "agent_integration", "plan"]) { assert.ok(changes.includes(`steps.plan.outputs.${flag}`)); } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e826be2a8c..69a27de27e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1051,12 +1051,24 @@ jobs: steps: - uses: actions/checkout@v7 with: - fetch-depth: 0 + fetch-depth: 1 + + - name: Fetch change base + id: change-base + env: + BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} + run: | + if [[ -z "$BASE_SHA" || "$BASE_SHA" =~ ^0+$ ]]; then + exit 0 + fi + git fetch --no-tags --depth=1 origin "$BASE_SHA" + echo "sha=$BASE_SHA" >> "$GITHUB_OUTPUT" - name: Detect changed areas uses: dorny/paths-filter@v4 id: filter with: + base: ${{ steps.change-base.outputs.sha }} filters: | frontend: - 'apps/desktop/**' @@ -1174,12 +1186,10 @@ jobs: - '.github/workflows/ci.yml' - 'apps/desktop/src/types/database.ts' - 'crates/dbx-core/assets/database-drivers.manifest.json' - - name: Setup Rust - uses: dtolnay/rust-toolchain@1.97.1 - name: Plan dependency-aware CI jobs id: plan env: - BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} + BASE_SHA: ${{ steps.change-base.outputs.sha }} RUST_CHANGED: ${{ steps.filter.outputs.rust }} AGENTS_CHANGED: ${{ steps.filter.outputs.agents }} run: node .github/scripts/ci-plan.mjs