DEV Community

Eric Gitangu
Eric Gitangu

Posted on Originally published at developer.ericgitangu.com

Which build is this? Stamping every static bundle, and a default that pointed at production

I'm Eric Gitangu. I work on backend and platform systems for a mobility/fintech platform. This one is about a small JSON file, and a default value that pointed the wrong way. The work is built and tested on branches and is not live in production yet.

The question the bundle couldn't answer

We ship several single-page apps as static exports. A build is a folder of files. Once it sits on a server, nothing in it says which commit it came from, which environment it was built for, or whether someone built it from a checkout with uncommitted changes. To find out you open the deploy log and hope it still exists and still tells the truth.

I wanted the server to be able to ask the bundle itself.

The stamp

On my branch, every build writes one small file at the very end of the export, after the other post-build steps. It holds:

  • the app name
  • the full commit sha
  • the branch
  • whether tracked files had uncommitted changes
  • the build time, in UTC
  • an environment label
  • the site origin
  • the origin the brand assets were fetched from

The label comes from one variable the build already needs, the site URL. One known hostname maps to "prod", another to "staging", and anything else is "other". Unknown stays unknown instead of being guessed.

Two rules keep the file honest.

  1. It reads nothing else from the environment. The stamp is assembled from named fields, so no secret can ride along. A test puts a fake live-looking key in the environment and asserts it never shows up in the output.
  2. The check is strict about shape. It fails when the file is missing, when it belongs to another app, when the sha isn't a full commit sha, when the time isn't UTC, and when the sha isn't the checkout the check runs in. An extra key fails it too, so nothing gets added to the stamp without a review.

A detached CI checkout has no branch name, so the sha and branch can be passed in. That is the one override.

The default that pointed at production

The same day, I fixed a related problem in the scripts that fetch the brand assets. In the earlier version, they read an origin from an environment variable and, when it was unset, fell back to the production origin. A build that forgot the variable would fetch the production assets and bake them into whatever it was building. Nothing fails. Nothing is logged. The bundle just quietly belongs to the wrong environment.

On the branch, I deleted the default. The origin is required and has to be an absolute http or https origin. Without it the script exits and says there is no default. One small helper, copied byte for byte into each repo that has such a script.

The warning that let old bundles through

The next day I put that origin into the stamp, because the server-side deploy step I wrote for it compares the origin with the target it is activating and refuses a release when they differ. As first written, that step only logged a warning when the key was absent. It is on a branch too.

A check that warns on a missing key would pass every bundle built before the key existed, and every bundle built by a path that skips the stamp. So I moved the requirement into the producer: the stamp can't be written without a valid origin, and the check refuses a stamp that lacks one. Absent means a failed build, not a line in a log.

What I wouldn't trust it for

  • A stamp is what the builder says, not proof of what's inside. It tells you where to look. It doesn't replace comparing the files.
  • The dirty flag only watches tracked files. A stray untracked file won't flip it.
  • The environment label comes from the same variable the build used. If that variable is wrong, so is the label. The stamp can't catch a mistake it shares.

The tests

The tests run the real scripts. The stamp has exactly the named fields. Unknown hosts get "other", and a planted key never reaches the file. The check names its reason for each failure. A missing or malformed origin stops both the asset scripts and the stamp. And one test scans the build scripts to make sure none of them carries the production origin as a default.

I put a stripped-down version in a gist you can run with node --test: https://gist.github.com/ericgitangu/a236bd7ffaca7c9c71e9ad5d76d08457

Where it stands

Built and tested on branches. Not live in production yet.

What I took from it

A default is a decision made by the person who wrote the line, for a caller who isn't there. I'd rather a missing input be an error, the fact be written into the artifact, and the check run where the artifact is used. And if a check can only warn, look at who produces the thing it checks.

Top comments (0)