-
Notifications
You must be signed in to change notification settings - Fork 0
ci(build): track build performance with Next.js cache + metrics #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e1e8b23
ci(build): track build performance with Next.js cache + metrics
factory-sam 38c5e9b
Merge remote-tracking branch 'origin/main' into feat/build-performanc…
factory-sam daa51bb
fix(build-metrics): handle spawn errors and correct docs field name
factory-sam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #!/usr/bin/env node | ||
| // Runs the production build, measures wall-clock duration, and exports a | ||
| // machine-readable build-metrics report. Used in CI to track build performance | ||
| // over time and surface regressions; appends a summary to the GitHub job page | ||
| // when GITHUB_STEP_SUMMARY is available. Forwards the build's exit code. | ||
| import { spawn } from "node:child_process"; | ||
| import { mkdirSync, readFileSync, appendFileSync, writeFileSync } from "node:fs"; | ||
| import { resolve, dirname } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const reportPath = resolve(repoRoot, "reports", "build-metrics.json"); | ||
|
|
||
| function readNextVersion() { | ||
| try { | ||
| const pkg = JSON.parse(readFileSync(resolve(repoRoot, "package.json"), "utf8")); | ||
| return pkg.dependencies?.next ?? pkg.devDependencies?.next ?? "unknown"; | ||
| } catch { | ||
| return "unknown"; | ||
| } | ||
| } | ||
|
|
||
| function runBuild() { | ||
| return new Promise((resolveRun) => { | ||
| const start = process.hrtime.bigint(); | ||
| const child = spawn("pnpm", ["build"], { stdio: "inherit", cwd: repoRoot, env: process.env }); | ||
|
|
||
| let settled = false; | ||
| const settle = (code) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| const durationMs = Number((process.hrtime.bigint() - start) / 1_000_000n); | ||
| resolveRun({ code: code ?? 1, durationMs }); | ||
| }; | ||
|
|
||
| child.on("error", (err) => { | ||
| console.error("Failed to run pnpm build", err); | ||
| settle(1); | ||
| }); | ||
| child.on("close", (code) => settle(code)); | ||
| }); | ||
| } | ||
|
|
||
| const { code, durationMs } = await runBuild(); | ||
| const durationSec = Math.round(durationMs / 100) / 10; | ||
|
|
||
| const metrics = { | ||
| status: code === 0 ? "success" : "failure", | ||
| durationMs, | ||
| durationSec, | ||
| timestamp: new Date().toISOString(), | ||
| nodeVersion: process.version, | ||
| nextVersion: readNextVersion(), | ||
| cacheRestored: process.env.NEXT_BUILD_CACHE_HIT === "true", | ||
| commit: process.env.GITHUB_SHA ?? null | ||
| }; | ||
|
|
||
| mkdirSync(dirname(reportPath), { recursive: true }); | ||
| writeFileSync(reportPath, `${JSON.stringify(metrics, null, 2)}\n`); | ||
|
|
||
| const summary = | ||
| `### Build metrics\n\n` + | ||
| `- Status: ${metrics.status}\n` + | ||
| `- Duration: ${durationSec}s\n` + | ||
| `- Cache restored: ${metrics.cacheRestored}\n` + | ||
| `- Next.js: ${metrics.nextVersion}\n` + | ||
| `- Node: ${metrics.nodeVersion}\n`; | ||
|
|
||
| console.log(`\nBuild ${metrics.status} in ${durationSec}s (metrics -> ${resolve("reports/build-metrics.json")})`); | ||
|
|
||
| if (process.env.GITHUB_STEP_SUMMARY) { | ||
| try { | ||
| appendFileSync(process.env.GITHUB_STEP_SUMMARY, `${summary}\n`); | ||
| } catch { | ||
| // Non-fatal: summary is best-effort. | ||
| } | ||
| } | ||
|
|
||
| process.exit(code); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.