AnalysisAI Infrastructure

The deploy that took the site down, and the guard that now prevents it

One type error, one unconditional restart, and every route answered 502. We rebuilt the deploy around a build gate and an atomic artifact swap, then broke a build on purpose to prove the guard holds.

The failure is not exotic. A build fails, the deploy script restarts the service anyway, and the service comes up against a half-written .next directory. Every route answers 502. Nobody notices for eleven minutes because the deploy script's own output said the build had "compiled successfully" — the word was in the log, three hundred lines above the type error.

This is a post-mortem of that shape of failure and the deploy we replaced it with, with the measurements from deliberately breaking a build to check the guard.

The three decisions that produced the outage

1. Build in place, then restart unconditionally.

npm run build
systemctl restart app     # runs even when the build exited 1

The restart is the moment damage becomes visible: the old process is killed and the new one starts against a .next that is missing chunks. A build that fails halfway leaves a directory that looks populated.

2. Read the exit code from a pipe.

npm run build 2>&1 | grep -E 'Compiled|Failed'

$? here is the status of grep, not of npm. The pipeline reported success while the build was failing, so the guard that did exist never fired. This is the single most expensive line in the story, because it made every other safeguard cosmetic.

3. No artifact to go back to.

The build wrote into the live directory, so there was nothing to restore. Recovery meant rebuilding from the previous commit under pressure, which is exactly when a second mistake happens.

What we replaced it with

The new deploy has three properties: the build happens somewhere else, the swap is atomic, and verification decides whether the deploy stands.

stage a copy  ->  build there  ->  exit 0?  ->  move live .next aside
                                              ->  move new .next in
                                              ->  restart
                                              ->  probe 14 routes
                                              ->  fail? swap back, restart

Three details make it work on this box specifically:

  • The stage uses a hardlink for `node_modules` (cp -al), not a symlink. Turbopack resolves the workspace root from the lockfile and refuses a symlinked node_modules with "Could not find the Next.js package", which reads like a missing install. A hardlink costs seconds and no disk.
  • The build runs as the service user. The environment file is 0600 owned by that user; a root build dies at Failed to load env and leaves a half-written .next behind, producing the same 502 without an obvious cause.
  • The exit code comes from `npm`, not from a pipe. The script captures rc=$? immediately after the build and aborts before touching the live tree.

Proof: breaking a build on purpose

A guard nobody has exercised is a guess. We added a file with a deliberate type error to the source tree and ran the deploy:

[21:10:12] staging a build copy
[21:10:37] building (this is the gate)
[21:12:31] build exit=1 — production untouched
src/app/broken-probe.ts(1,14): error TS2322: Type 'string' is not assignable to type 'number'.
[21:12:31] FAILED: build gate refused the deploy
DEPLOY_EXIT=1

While that ran, the live route answered 200 — the running process was never restarted, and the live .next was never touched. The broken file was then removed.

The same script on a good build:

[21:14:43] build exit=0
[21:14:43] backing up the live artifact
[21:14:43] swapping the new artifact into place
[21:14:43] restarting
[21:14:47] route probe   (14 routes, all 200)
[21:14:52] deployed — all routes 200

Wall time 134 seconds, of which 101 was the build. The restart itself cost about 4 seconds of unavailability, measured by polling / every 200 ms from the moment systemctl restart returned — not read from the journal, which reports process spawn (about 0.1 s) and not the time until the port answers.

What this buys, and what it does not

It buys the difference between "a mistake costs a failed deploy" and "a mistake costs an outage and a rebuild under pressure". That difference is worth more than any amount of care taken when writing the change.

It does not buy zero downtime. Four seconds is four seconds: a crawler can hit it, and a retry is the only mitigation at this architecture. Real zero-downtime needs two instances behind a proxy with a health check, which is a larger change than this site's traffic justifies today.

It also does not replace the other gates: type-check is a floor, not a test. A build that compiles can still render an empty page, which is why the route probe exists and why it fetches a real article URL from the sitemap rather than only the homepage.

The general lesson

A deploy script is production code, and it fails in production code ways: a pipe swallows the status, a restart runs regardless, a rollback path was never exercised. Write it so the dangerous step cannot happen on a bad signal, then break it on purpose once and watch it refuse.

Sources

Related

Keep reading

All news →
PreviousWhat an automated article costs, measured over 30 daysNextSeventy-six articles, three clicks: what a year of auto-publishing taught us

Comments

0 comments

Sign in to join the discussion, or create an account.

No comments yet

Be the first to say something useful.