Contents Lesson 5 of 16

11 min read · professional

CI keeps checking when you stop remembering

By the end of this lesson you will have a CI step that fails when any file but src/lib/eodhd.ts mentions the key, and a red run you watched for each kind of check before closing the pull requests that caused them.

You have been reviewing carefully for four courses. CI exists because that will not last, and it is not a criticism — attention is finite and yours will move on.

The starter has a seatbelt, not a lock

The starter repository ships scripts/precommit-key-guard.sh, a hook that refuses a commit staging something shaped like an API key. Its own comments are honest about the limits:

"This is a seatbelt, not a lock. It cannot see a key you never staged, and it cannot see one already in your history."

Two more limits. The starter keeps its hooks in .githooks/ and npm install switches them on, so every clone that ran it is covered — but a clone that never ran npm install is not, a commit made by a cloud agent never meets them, and git commit --no-verify skips them entirely.

So the same checks have to exist somewhere nobody can skip and nobody has to install. That is the whole argument for CI.

The four checks worth having

In order of what they save you from:

The key scan. The same pattern as the pre-commit hook, run on every pull request — but over the whole tree, not the diff. The hook already covers the diff at the moment the line is written; CI runs after the hook can be skipped with one flag, after a push from a machine that never installed it, and after a key somebody committed months ago. Scanning only the diff here would reproduce exactly the blind spot CI exists to cover. Check out the full history too, or a shallow clone scans the tip and misses the commit worth catching.

This is the one that prevents the unrecoverable mistake, so it goes first. The starter ships it in .github/workflows/key-scan.yml, beside a second job that runs the other three checks below; read both rather than trusting this paragraph. Your job in this lesson is to make sure each one fails when it should, and to add the one only your project needs.

Type check and lint. tsc --noEmit and your linter. Cheap, and they catch the class of error that a generated diff introduces most often.

The build. If it does not build, nothing else matters. It also catches the import that only worked because of your local cache.

The tests. The suite you started in Build Your First Tool and grew in Build a Screener, plus Backtest Strategies's honesty checks and Build a Dashboard's cross-user store test. These are the ones that encode judgement, and they are the reason the suite is worth having at all.

Fail the build, do not warn

A warning is a check that has already stopped working. Nobody reads a green build's warnings, and after the third one you stop reading the output at all.

If a rule is not worth failing on, remove it. A small suite that must pass beats a large one that mostly does.

The check you write yourself

Ask CI something specific to your project that no off-the-shelf tool knows. For this repository the natural one is: does any file outside the proxy reference the API key?

- name: Key stays in the proxy
  run: |
    ! grep -rn "EODHD_API_KEY" src --include="*.ts*" \
      | grep -v "^src/lib/eodhd.ts:"

Four lines, and it enforces the central architectural rule of the whole track — permanently, on every push, without anyone remembering it. That is what a good project-specific check looks like: an invariant you decided once, made mechanical.

The pre-commit hook still earns its place

CI catches it after you push; the hook catches it before it exists. Keep both, because the difference between "I fixed it before committing" and "I have to rewrite history and rotate a key" is the hook.

Layers, not alternatives — the same argument as cooldown plus hysteresis in Build a Dashboard.

Try it now

Add the key scan and the project-specific check first, then push a branch that deliberately violates each one and confirm the build goes red. A check you have never seen fail is not evidence — the same rule as Backtest Strategies's tests.

The prompt

The starter's workflow already runs the key scan, lint, type check, tests and the leak check, which builds the app. What it lacks is the check only your project needs. Mock mode on, plan mode on, and paste:

Add one step at the end of the checks job in
.github/workflows/key-scan.yml, named "Key stays in eodhd.ts". It fails
when any .ts or .tsx file under src other than src/lib/eodhd.ts
contains EODHD_API_KEY, printing each offending line. Keep it the only
command in its step. Run the same command here and show me what it
prints. For each line, tell me whether it reads the key or only names
it in a sentence; a read moves into src/lib/eodhd.ts, a sentence stops
naming the variable, until the command prints nothing. Then run
npm test, npm run typecheck, npm run lint and
npm run check:leak. Do not push and do not open a pull request: I do
that. Then show me the diff.

A good answer adds a few lines to the workflow (the exact count depends on how the agent writes the step), and in the end the command run locally prints nothing. On the starter it prints lines at first, and none of them reads the key: grep cannot tell a read from a mention, and four files name EODHD_API_KEY in sentences meant for the user. Ours made the proxy's no-key answer take configProblem's sentence from src/lib/eodhd.ts and carry a kind the browser reads, instead of the browser searching the body for the name, and the other sentences now say "the key in .env.local". A line that does read the key moves into src/lib/eodhd.ts.

Check it yourself

The first three checks need your repository on GitHub and pull requests. Without GitHub, only the last point can be checked, and the first three wait until you have it.

If your repository is a fork, every pull request here goes to your own repository, never to the starter: GitHub offers the starter as the base by default. Run gh repo set-default once and pick yours, and on github.com change the base repository before you create the pull request.

Commit this lesson's work before the checks, so a throwaway branch made from it carries the new step:

npm test && npm run typecheck
git status          # the files you expect, and no .env file
git add -A
git commit -m "CI: the key is read in one file, and a red run proves the check works"
  1. On a throwaway branch made from that commit, read the key in another server file — console.log(Boolean(process.env.EODHD_API_KEY)) in src/lib/quotes.ts will do — then commit, push and open a pull request that is not a draft. The new step goes red and names the file. A draft runs nothing.
  2. On github.com, on a second throwaway branch, put a made-up key-shaped string in the README with the web editor — twelve lowercase hex characters, a dot, six digits — and open a pull request. The scan job goes red. That commit never met your hooks, which is the case CI is for.
  3. In the web editor again, change one expected value in a test and open a pull request: the checks job goes red at npm test.
  4. Close all three pull requests, switch back to your own branch and delete the throwaway ones. Then, locally, stage a line holding the same made-up string and try to commit: the hook refuses before the commit exists, which is the layer CI cannot replace. Remove the line.
Compare with ours
# .github/workflows/key-scan.yml, last step of the checks job
- name: Key stays in eodhd.ts
  run: |
    ! grep -rn "EODHD_API_KEY" src --include="*.ts*" \
      | grep -v "^src/lib/eodhd.ts:"

The starter reads the key in src/lib/eodhd.ts, which the proxy calls, so that is the one file allowed. Keep the command alone in its step: under set -e, a command negated with ! that fails does not stop the script, so a second line after it would turn the step green whatever grep found. The full state at the end of this unit is on the starter's branch ref/ship-your-tool/u2.

Checkpoint

git status          # nothing to commit: the work went in before the checks
git log --oneline -1   # "CI: the key is read in one file, ..."

Then answer this lesson's questions.