fix: speed up native branch follower refresh

This commit is contained in:
Codex
2026-07-03 14:21:39 +00:00
parent 7751cc167b
commit f3fccb8f35
8 changed files with 99 additions and 40 deletions
@@ -2,7 +2,7 @@
// SPEC: PJ2026-01060703 CI/CD branch follower native HWLAB control-plane refresh.
// Responsibility: render and apply the HWLAB node Tekton Pipeline from a k8s git-mirror snapshot.
import { spawnSync } from "node:child_process";
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { tmpdir } from "node:os";
import path from "node:path";
@@ -17,6 +17,7 @@ const overlay = JSON.parse(Buffer.from(requiredEnv("HWLAB_RENDER_OVERLAY_B64"),
const workDir = mkdtempSync(path.join(tmpdir(), `hwlab-control-plane-${sourceCommit.slice(0, 12)}-`));
const repoDir = path.join(workDir, "repo");
const renderDir = path.join(workDir, "render");
const depsDir = path.join(workDir, "deps");
try {
checkoutSnapshot();
@@ -39,6 +40,8 @@ function checkoutSnapshot() {
}
function prepareYamlDependency() {
mkdirSync(depsDir, { recursive: true });
writeFileSync(path.join(depsDir, "package.json"), JSON.stringify({ private: true, dependencies: {} }), "utf8");
if (canResolveYaml()) return;
const registry = requiredOverlayString("npmRegistry");
const timeoutMs = String(requiredPositiveNumber("npmFetchTimeoutMs"));
@@ -51,10 +54,10 @@ function prepareYamlDependency() {
npm_config_fetch_retries: retries,
});
if (commandExists("bun")) {
const result = runOptional("bun", ["add", "--no-save", "--ignore-scripts", "--registry", registry, yamlSpec], { cwd: repoDir, env });
const result = runOptional("bun", ["add", "--no-save", "--ignore-scripts", "--registry", registry, yamlSpec], { cwd: depsDir, env });
if (result.status === 0 && canResolveYaml()) return;
}
run("npm", ["install", "--package-lock=false", "--no-save", "--ignore-scripts", "--no-audit", "--no-fund", "--omit=dev", "--registry", registry, yamlSpec], { cwd: repoDir, env });
run("npm", ["install", "--package-lock=false", "--no-save", "--ignore-scripts", "--no-audit", "--no-fund", "--omit=dev", "--registry", registry, yamlSpec], { cwd: depsDir, env });
if (!canResolveYaml()) throw new Error("yaml dependency remains unresolved after install");
}
@@ -66,13 +69,13 @@ function yamlDependencySpec() {
}
function canResolveYaml() {
const result = runOptional("node", ["-e", "require.resolve('yaml')"], { cwd: repoDir, stdio: "ignore" });
const result = runOptional("node", ["-e", "require.resolve('yaml')"], { cwd: depsDir, stdio: "ignore" });
return result.status === 0;
}
function applyDeployOverlay() {
const requireFromRepo = createRequire(path.join(repoDir, "package.json"));
const YAML = requireFromRepo("yaml");
const requireFromDeps = createRequire(path.join(depsDir, "package.json"));
const YAML = requireFromDeps("yaml");
const deployPath = path.join(repoDir, "deploy/deploy.yaml");
const doc = YAML.parse(readFileSync(deployPath, "utf8"));
doc.nodes = doc.nodes || {};
@@ -155,13 +158,13 @@ function renderEnv(extra = {}) {
function run(command, args, options = {}) {
const result = runOptional(command, args, options);
if (result.status !== 0) throw new Error(`${command} ${args.join(" ")} failed with exit ${result.status}`);
if (result.status !== 0) throw new Error(failedCommandMessage(command, args, result));
return result;
}
function capture(command, args) {
const result = runOptional(command, args, { cwd: repoDir, encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] });
if (result.status !== 0) throw new Error(`${command} ${args.join(" ")} failed with exit ${result.status}`);
if (result.status !== 0) throw new Error(failedCommandMessage(command, args, result));
return result.stdout || "";
}
@@ -178,6 +181,17 @@ function commandExists(command) {
return runOptional("sh", ["-lc", `command -v ${command} >/dev/null 2>&1`], { cwd: repoDir, stdio: "ignore" }).status === 0;
}
function failedCommandMessage(command, args, result) {
const signal = result.signal ? ` signal=${result.signal}` : "";
const error = result.error ? ` error=${result.error.message}` : "";
const stderr = typeof result.stderr === "string" && result.stderr.length > 0 ? ` stderr=${tail(result.stderr, 1000)}` : "";
return `${command} ${args.join(" ")} failed with exit ${result.status}${signal}${error}${stderr}`;
}
function tail(value, maxChars) {
return value.length <= maxChars ? value : value.slice(value.length - maxChars);
}
function requiredEnv(name) {
const value = process.env[name];
if (!value) throw new Error(`${name} is required`);