Add build-info script so the running build is visible

This commit is contained in:
Grok Bot
2026-11-26 09:10:37 +00:00
parent 2d2c6c55b5
commit 3c470ba85b
3 changed files with 65 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
{
"version": "1.0.0",
"commit": null,
"branch": null,
"sourceHash": "f32721f929",
"builtAt": "2026-11-26T09:09:23.099Z"
}
+13
View File
@@ -0,0 +1,13 @@
// lib/build-info.json is generated by scripts/write-build-info.mjs (prebuild).
// Fallback keeps `next dev` and `tsc` working before the first build.
import generated from './build-info.json';
export type BuildInfo = {
version: string;
commit: string | null;
branch: string | null;
sourceHash: string;
builtAt: string;
};
export const buildInfo: BuildInfo = generated as BuildInfo;
+45
View File
@@ -0,0 +1,45 @@
// Runs before `next build`. Records what was built so a redeploy is visible on the page.
// Works with or without a .git directory (ZIP deploys have none).
import { execSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { readFileSync, readdirSync, statSync, writeFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
function git(cmd) {
try {
return execSync(`git ${cmd}`, { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim();
} catch {
return null;
}
}
// Content hash of the source tree: changes whenever the code changes, even without git.
function hashTree(dirs) {
const h = createHash('sha256');
const walk = (d) => {
for (const name of readdirSync(d).sort()) {
const p = join(d, name);
const s = statSync(p);
if (s.isDirectory()) walk(p);
else { h.update(p); h.update(readFileSync(p)); }
}
};
for (const d of dirs) { try { walk(d); } catch { /* optional dir */ } }
return h.digest('hex').slice(0, 10);
}
const info = {
version: process.env.APP_VERSION || pkg.version,
commit:
process.env.GIT_COMMIT || process.env.SOURCE_COMMIT || process.env.COMMIT_SHA ||
git('rev-parse --short HEAD') || null,
branch: git('rev-parse --abbrev-ref HEAD'),
sourceHash: hashTree(['app', 'lib', 'components', 'public']),
builtAt: new Date().toISOString(),
};
mkdirSync('lib', { recursive: true });
writeFileSync('lib/build-info.json', JSON.stringify(info, null, 2) + '\n');
console.log('[build-info]', JSON.stringify(info));