Skip to content

Commit 65243f1

Browse files
noreplyclaude
andcommitted
Fork CI: one fork-ci-result check, secret scan, actionlint, faster tests
- fork-ci-result needs every job and fails on a skip without a reason; its summary lists each job, why it skipped, and job wall times. - Secret scan (gitleaks, checksum-pinned) of the commits a pull request or push adds; workflow lint (actionlint) next to the drift check. - Ubuntu 24.04 runners as upstream: ubuntu-latest (26.04) fails an apps/server test with its newer git. - Test is split across two runners by test file count; vp's task cache is kept between runs for the typecheck and the server shards. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 918e22b commit 65243f1

4 files changed

Lines changed: 319 additions & 52 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Fork only. Splits the Test job's packages across the shards of its matrix in
2+
// .github/workflows/fork-tests.yml. Upstream runs the job on one large runner;
3+
// a GitHub-hosted runner has 4 CPUs, so the packages go to several runners.
4+
//
5+
// node .github/scripts/fork-test-shards.cjs 1/2 prints shard 1's packages
6+
//
7+
// TEST_PACKAGES (from the `changes` job) limits the set; empty means every
8+
// package the Test job covers. Packages are weighted by their test file count
9+
// and placed, heaviest first, on the least loaded shard, so the split follows
10+
// new packages and tests without a hand-kept list.
11+
const childProcess = require("node:child_process");
12+
const fs = require("node:fs");
13+
14+
const { readWorkspace } = require("./ci-changes.cjs");
15+
16+
// Tested by their own jobs (Test Server, Test Web) or not a package to test.
17+
const OWN_JOB = new Set(["t3", "@t3tools/web", "@t3tools/monorepo"]);
18+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/;
19+
20+
function assign(packages, count) {
21+
const shards = Array.from({ length: count }, () => ({ weight: 0, packages: [] }));
22+
const ordered = packages.toSorted((a, b) => b.weight - a.weight || a.name.localeCompare(b.name));
23+
for (const pkg of ordered) {
24+
const lightest = shards.reduce((best, shard) => (shard.weight < best.weight ? shard : best));
25+
lightest.weight += pkg.weight;
26+
lightest.packages.push(pkg);
27+
}
28+
return shards;
29+
}
30+
31+
function testFiles(dir) {
32+
return childProcess
33+
.execFileSync("git", ["ls-files", "-z", "--", dir], { encoding: "utf8", maxBuffer: 1 << 26 })
34+
.split("\0")
35+
.filter((file) => TEST_FILE.test(file)).length;
36+
}
37+
38+
function main() {
39+
const match = (process.argv[2] ?? "").match(/^(\d+)\/(\d+)$/);
40+
if (!match || Number(match[1]) < 1 || Number(match[1]) > Number(match[2]))
41+
throw new Error("usage: fork-test-shards.cjs <index>/<count>");
42+
const [index, count] = [Number(match[1]), Number(match[2])];
43+
const only = (process.env.TEST_PACKAGES ?? "").split(/\s+/).filter(Boolean);
44+
const packages = readWorkspace()
45+
.filter((pkg) => pkg.hasTest && !OWN_JOB.has(pkg.name))
46+
.filter((pkg) => only.length === 0 || only.includes(pkg.name))
47+
.map((pkg) => ({ name: pkg.name, weight: Math.max(1, testFiles(pkg.dir)) }));
48+
const unknown = only.filter((name) => !packages.some((pkg) => pkg.name === name));
49+
if (unknown.length)
50+
throw new Error(`TEST_PACKAGES names no tested package: ${unknown.join(" ")}`);
51+
52+
const shard = assign(packages, count)[index - 1];
53+
for (const pkg of shard.packages) console.log(pkg.name);
54+
const summary = process.env.GITHUB_STEP_SUMMARY;
55+
if (summary) {
56+
const list = shard.packages.map((pkg) => `\`${pkg.name}\` (${pkg.weight})`).join(", ");
57+
fs.appendFileSync(
58+
summary,
59+
`### Test shard ${index}/${count}\n\n` +
60+
(shard.packages.length
61+
? `${shard.packages.length} of ${packages.length} packages, ${shard.weight} test files: ${list}\n`
62+
: `No packages: the change affects ${packages.length} tested package(s), all on other shards.\n`),
63+
);
64+
}
65+
}
66+
67+
module.exports = { assign };
68+
69+
if (require.main === module) main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const assert = require("node:assert/strict");
2+
const test = require("node:test");
3+
4+
const { assign } = require("./fork-test-shards.cjs");
5+
6+
const names = (shards) => shards.map((shard) => shard.packages.map((pkg) => pkg.name));
7+
8+
test("places the heaviest package first on the lightest shard", () => {
9+
const packages = [
10+
{ name: "small", weight: 1 },
11+
{ name: "big", weight: 10 },
12+
{ name: "mid", weight: 6 },
13+
{ name: "mid2", weight: 5 },
14+
];
15+
assert.deepEqual(names(assign(packages, 2)), [
16+
["big", "small"],
17+
["mid", "mid2"],
18+
]);
19+
});
20+
21+
test("ties break by name, so every shard computes the same split", () => {
22+
const packages = [
23+
{ name: "b", weight: 2 },
24+
{ name: "a", weight: 2 },
25+
{ name: "c", weight: 2 },
26+
];
27+
assert.deepEqual(names(assign(packages, 2)), [["a", "c"], ["b"]]);
28+
assert.deepEqual(names(assign(packages.toReversed(), 2)), [["a", "c"], ["b"]]);
29+
});
30+
31+
test("every package lands on exactly one shard, and spare shards stay empty", () => {
32+
const packages = [{ name: "only", weight: 3 }];
33+
assert.deepEqual(names(assign(packages, 3)), [["only"], [], []]);
34+
});

‎.github/scripts/fork-tests-drift.cjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ function compare(recorded, now) {
124124
continue;
125125
}
126126
if (a.header !== b.header)
127-
changes.push(`${file}: job \`${job}\`: settings changed (runs-on, if, needs, matrix, env...)`);
127+
changes.push(
128+
`${file}: job \`${job}\`: settings changed (runs-on, if, needs, matrix, env...)`,
129+
);
128130
const oldSteps = new Map(a.steps.map((s) => [s.name, s.digest]));
129131
const newSteps = new Map(b.steps.map((s) => [s.name, s.digest]));
130132
for (const [name, d] of newSteps) {

0 commit comments

Comments
 (0)