mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 12:49:00 +00:00
* fix(ci): read portable build plan from a file instead of a pipe Write the Turbo dry plan to .cache/ci-build-plan.json, then have key and snapshot read that complete file by path. This removes the live stdin pipe from the portable-build-cache plan step. * fix(ci): clear stale portable build output when the plan file is missing If readFile fails, still remove the previous key plan or snapshot so a missing or unreadable input cannot leave stale output for later steps.
161 lines
4.8 KiB
JavaScript
161 lines
4.8 KiB
JavaScript
import { createHash } from "node:crypto";
|
|
import {
|
|
copyFile,
|
|
lstat,
|
|
mkdir,
|
|
readFile,
|
|
rm,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { z } from "zod";
|
|
|
|
const output = ".cache/ci-build-cache";
|
|
const plan = ".cache/ci-build-plan.json";
|
|
|
|
function readTasks(source) {
|
|
const { tasks } = z
|
|
.object({
|
|
tasks: z
|
|
.array(
|
|
z.object({
|
|
taskId: z.string().min(1),
|
|
// The pinned Turbo version uses eager 64-bit hexadecimal hashes.
|
|
hash: z.string().regex(/^[0-9a-f]{16}$/u),
|
|
}),
|
|
)
|
|
.min(1),
|
|
})
|
|
.parse(JSON.parse(source));
|
|
const identities = new Set();
|
|
for (const { taskId } of tasks) {
|
|
if (identities.has(taskId))
|
|
throw new Error(`Duplicate portable build task: ${taskId}`);
|
|
identities.add(taskId);
|
|
}
|
|
return tasks;
|
|
}
|
|
|
|
async function copyEntry(hash) {
|
|
const entries = [];
|
|
for (const suffix of [".tar.zst", "-meta.json", "-manifest.json"]) {
|
|
const name = `${hash}${suffix}`;
|
|
const source = join(".turbo/cache", name);
|
|
const entry = await lstat(source).catch((error) => {
|
|
// Eviction can remove a cache hit even during the successful build.
|
|
// Manifests are also optional in supported local cache entries.
|
|
if (error.code === "ENOENT") return undefined;
|
|
throw error;
|
|
});
|
|
if (!entry) continue;
|
|
if (!entry.isFile())
|
|
throw new Error(
|
|
`Portable build cache entry must be a regular file: ${source}`,
|
|
);
|
|
entries.push({ suffix, name, source, size: entry.size });
|
|
}
|
|
// Publish whole reusable entries only. Consumers rebuild missing tasks.
|
|
if (
|
|
![".tar.zst", "-meta.json"].every((suffix) =>
|
|
entries.some((entry) => entry.suffix === suffix),
|
|
)
|
|
)
|
|
return undefined;
|
|
const copied = [];
|
|
let missing = false;
|
|
for (const entry of entries) {
|
|
try {
|
|
await copyFile(entry.source, join(output, entry.name));
|
|
copied.push(entry);
|
|
} catch (error) {
|
|
if (error.code !== "ENOENT") throw error;
|
|
if (entry.suffix === "-manifest.json") continue;
|
|
missing = true;
|
|
break;
|
|
}
|
|
}
|
|
if (missing) {
|
|
for (const entry of copied) await rm(join(output, entry.name));
|
|
return undefined;
|
|
}
|
|
return {
|
|
files: copied.length,
|
|
bytes: copied.reduce((sum, entry) => sum + entry.size, 0),
|
|
};
|
|
}
|
|
|
|
async function snapshot(tasks) {
|
|
await mkdir(output, { recursive: true });
|
|
let files = 0;
|
|
let bytes = 0;
|
|
const available = new Set();
|
|
try {
|
|
for (const hash of new Set(tasks.map((task) => task.hash))) {
|
|
const entry = await copyEntry(hash);
|
|
if (!entry) continue;
|
|
available.add(hash);
|
|
files += entry.files;
|
|
bytes += entry.bytes;
|
|
}
|
|
// A fully evicted cache still has an explicit artifact and coverage record;
|
|
// required build outputs and downstream verification remain separate gates.
|
|
const manifest = `${JSON.stringify({
|
|
version: 1,
|
|
tasks: tasks.map((task) => ({
|
|
...task,
|
|
available: available.has(task.hash),
|
|
})),
|
|
})}\n`;
|
|
await writeFile(join(output, "portable-build-cache.json"), manifest);
|
|
files++;
|
|
bytes += Buffer.byteLength(manifest);
|
|
} catch (error) {
|
|
await rm(output, { recursive: true, force: true });
|
|
throw error;
|
|
}
|
|
console.log(
|
|
JSON.stringify({
|
|
tasks: tasks.length,
|
|
files,
|
|
bytes,
|
|
path: output,
|
|
missing_tasks: tasks
|
|
.filter((task) => !available.has(task.hash))
|
|
.map((task) => task.taskId),
|
|
}),
|
|
);
|
|
}
|
|
|
|
const [mode, input, ...extra] = process.argv.slice(2);
|
|
if (extra.length || !input || !["key", "snapshot"].includes(mode))
|
|
throw new Error("Usage: portable-build-cache.mjs key|snapshot <plan.json>");
|
|
// Read the complete plan file first so invalidation can delete the retained
|
|
// plan even when that path is also the input. If the read fails, still clear
|
|
// this mode's previous output so a missing or unreadable file cannot leave a
|
|
// stale plan or snapshot. Failed validation must also not leave stale output
|
|
// for downstream reuse.
|
|
let source;
|
|
try {
|
|
source = await readFile(input, "utf8");
|
|
} finally {
|
|
if (mode === "snapshot") await rm(output, { recursive: true, force: true });
|
|
else await rm(plan, { force: true });
|
|
}
|
|
const tasks = readTasks(source);
|
|
if (mode === "key") {
|
|
const prefix = z
|
|
.string()
|
|
.min(1)
|
|
.regex(/^[^\r\n]+$/u)
|
|
.parse(process.env.BUILD_CACHE_PREFIX);
|
|
const hashes = tasks.map(({ taskId, hash }) => `${taskId}:${hash}`).sort();
|
|
const digest = createHash("sha256").update(hashes.join("\n")).digest("hex");
|
|
// Retain only validated identities for the later snapshot. Running Turbo
|
|
// again after the build can evict cache entries before they are copied.
|
|
await mkdir(".cache", { recursive: true });
|
|
await writeFile(plan, `${JSON.stringify({ tasks })}\n`);
|
|
console.log(`key=${prefix}-${digest}\nprefix=${prefix}-`);
|
|
} else {
|
|
await snapshot(tasks);
|
|
}
|