Chapter 1 · Session 11
Production comes only from main
There are tables, test data, access rules, sign-in and the ten quality checks, but no screens yet. That makes this the cheapest moment to make deploys safe: nothing the plant uses can break while you set it up, and every screen from Session 12 on is tested where it will run.
25 min2 stepsRules first, then previews
By the end of this chapter you can
- Say what changes in this session, and why it comes before the first screen.
- Check that every production deployment was built by Vercel from main, and write the rule down for Claude Code.
- Point every preview at the development project, so a half-finished change can only touch made-up data.
Why deploys come before screens
Until now you have applied each migration to the live database yourself, after the merge, as Session 5 showed. That works while you remember. We did not: our platform's production database fell nine migrations behind main, the oldest seven weeks old, and pages that worked in preview failed in production with database errors, days after the merge. The cure is to make the deploy apply migrations and stop if one fails, and to check every day that production has every migration main has [1].
Doing it now, in week 3, costs one session while nothing depends on the platform. Doing it after the operator screens are live means learning it during an outage.
| Until this session | From this session | |
|---|---|---|
| Live database changes | You run npx supabase db push after each merge | The deploy workflow applies them on merge; you never run db push for production again |
| Previews | Talk to the production project (the integration copied its keys everywhere) | Talk to the development project, with made-up data only |
| Production | Vercel builds main and makes it live at once | Vercel makes it live only when the deploy workflow is green |
| Knowing production is current | You remember to check | A daily check compares production with main and opens an issue |
| Backups | None of your own | Nightly, encrypted, kept off the platform, and restored once |
| When it breaks | Improvise | docs/runbook.md, with drills written in its log |
Production comes only from main: check it, and write it down
10 minutesWhy
Production must be built by Vercel from main, after a reviewed merge, and nothing else. We hit the alternative: a deploy run from a laptop built on the laptop, picked up its local settings and sent production to a database at localhost; three live pages failed while the same commit built from GitHub worked. Writing the rule into CLAUDE.md and the deny list makes it hold for every later session and every agent, without you repeating it.
Do this
https://vercel.com/acme-ops/acme-platform/deployments
Filter by Production. Every row should show the branch main and a commit message. A production row with no commit is a deployment from someone's computer.
Read CLAUDE.md, .claude/settings.json and docs/KEYS.md. On a new branch deploy/rules:
1. In CLAUDE.md, add a section "Deploys and the live database":
- Production is built only by Vercel from main, after a merged pull request. Never run vercel deploy, vercel --prod, vercel promote or vercel rollback, and never deploy from this computer. If you think a deploy is needed, tell me.
- Migrations reach the development and production databases only through the deploy workflow on merge (added in this session). Never run supabase db push, supabase db reset or supabase link against either project, and never ask for a database password.
- A red check counts only if it ran on the branch's latest commit. Before diagnosing one, say which commit it ran on and how far behind main that commit is. If it is behind, merge origin/main into the branch (no rebase, no force push), push, and wait for the new checks. Never re-run old runs.
2. In .claude/settings.json, keep every existing deny rule and add denies for vercel deploy, vercel promote, vercel rollback and supabase link, each in both the npx and the plain form.
3. Change nothing else.
Open a pull request titled "Deploy rules", wait for every check to finish, and tell me the result in plain words.You will see recreated screen
| Deployment | Status | Source | Created |
|---|---|---|---|
| Current | Ready | main · Clamps: required checks and CODEOWNERS (#31) | 2d ago |
| Ready | main · Sign-in that denies by default (#27) | 5d ago | |
| Ready | main · Access rules from the matrix (#24) | 8d ago |
- Each row: main, and the title of the pull request it came from. A row that says it was deployed with the command line, or shows no commit, needs explaining before you go on.
You will see recreated screen
- The rule in words, for every future session.
- The same rule, enforced: Claude Code cannot run these commands even if asked.
Check it worked
Every production row in Vercel came from main. Pull request Deploy rules is green on its latest commit, changes only CLAUDE.md and .claude/settings.json, and owner two has approved and merged it.
Fastest path
When Claude Code asks to run gh pr checks --watch, choose the "don't ask again" option: it only reads the checks, and every prompt in this session ends with it.
If it goes wrong
A production deployment has no commit, or says it was deployed from the command line
Someone deployed from a computer. Open the newest production deployment that came from main and use its menu to make it the live one again (Promote, or Instant Rollback if it is older). Then find out who deployed and why: a computer sends its local settings and unsaved files with it. From now on the deny rule stops Claude Code doing it.
The Vercel build fails with "Reached heap limit", or "Deployment has failed" on several pull requests at once
We hit this: the build ran out of memory, and every pull request showed it as a different failure. Say: Open the Vercel build log for this deployment and tell me the exact error. If it ran out of memory, find where the build's memory limit is set, tell me the peak the build reached, raise the limit to at least 512 MB above that peak, and add a test that fails if the limit is ever set below peak plus 512 MB. If this branch is behind main, merge main first and check whether main already raised it.
A check is red for a reason that has nothing to do with this change, or its logs have expired
The verdict is probably about an old commit. Agents have chased such checks for hours, and re-running old runs once sent us 113 failure emails in a day. Say: Before diagnosing this red check: tell me which commit it ran on, how far that commit is behind main, and whether main has fixed anything since. If it is behind, merge origin/main into the branch (no rebase, no force push), push, and wait for the new checks. Only diagnose what is still red after that.
Previews use the development project
15 minutesWhy
Since Session 1, the Supabase integration has copied the production project's keys into every Vercel environment, so every preview talks to the production database. With no screens yet that has done no harm. From Session 12, previews save records, and a half-finished change tried on a preview would write into the plant's real data. Pointing previews at the development project from Session 6 means anything tried on a preview lands on made-up data, and real data never leaves production.
Do this
https://supabase.com/dashboard/project/abcdefghijklmnopqrst/settings/integrations
The production project's integrations. Open the Vercel connection to acme-platform and limit the environments it syncs to Production only. Save.
https://supabase.com/dashboard/project/tsrqponmlkjihgfedcba/settings/integrations
The development project's integrations. Under Vercel, add a project connection to acme-platform in the acme-ops team, syncing Preview and Development only.
https://vercel.com/acme-ops/acme-platform/settings/environment-variables
Check by name: each Supabase variable now has one row for Production and one for Preview and Development. Values stay hidden; nobody typed them.
Then make it visible on the site, so you never have to guess which database a deployment uses:
Read node_modules/next/dist/docs/ for how this version of Next.js reads environment variables, then the /status page from Session 3.
On a new branch status/database, change the /status page so it also says which Supabase project this deployment talks to: "production" when NEXT_PUBLIC_SUPABASE_URL contains abcdefghijklmnopqrst, "development" when it contains tsrqponmlkjihgfedcba, and "unknown" otherwise. Never show the address or any key. Add a test for each of the three answers.
Open a pull request titled "Status shows the database", wait for every check and for the Vercel preview, and give me the preview link.You will see recreated screen
| Key | Value | Environments |
|---|---|---|
| NEXT_PUBLIC_SUPABASE_URL | •••••••• | Production |
| NEXT_PUBLIC_SUPABASE_URL | •••••••• | Preview, Development |
| NEXT_PUBLIC_SUPABASE_ANON_KEY | •••••••• | Production |
| NEXT_PUBLIC_SUPABASE_ANON_KEY | •••••••• | Preview, Development |
- Two rows per name: one per project.
- Both kept in step by the integration, so rotating a key in either project updates Vercel.
You will see recreated screen
- On a preview: development. On https://acme-platform.vercel.app/status after the merge: production. Anything else means stop and look.
Check it worked
The preview's /status says development. After owner two approves and you merge, https://acme-platform.vercel.app/status says production.
Fastest path
Do the two integration pages and the Vercel check before pasting the prompt: Claude Code's preview then proves the switch worked in the same pull request.
If it goes wrong
The integration page offers no choice of environments, or Vercel will not let you edit the variables
Integration screens change. Do not delete the production connection. Ask in office hours, or type the development project's address and publishable key into the Preview rows on Vercel's page yourself, copied from the development project's API Keys page: the value only, no quotes, no NAME=.
The preview build stops with "TypeError: Invalid URL", with the value hidden
We hit this: an address pasted with quotes, a NAME= prefix or a space. Open the variable in Vercel and paste the value again, on its own. The start-up check from Session 10 should name which variable is wrong without printing it; if it did not, say: The start-up check did not catch a malformed Supabase address. Add a test that feeds it an address with quotes, a NAME= prefix and a space, and expects each to be named.
Something saves on your computer but is gone on the preview or the live site
On Vercel the app's own folders are read-only and each server copy is separate, so writing a file works only on your computer. We hit this. Say: Find every place the app writes a file to disk (fs.writeFile, fs.appendFile, anything under public/ or the project folder). Move each to a database table or to Supabase Storage, and add a test that fails if new code writes into the project folders.
The preview asks me to sign in to Vercel
That is Vercel's Deployment Protection: previews are for your team only. Leave it on, sign in, and carry on.
Knowledge check
Why do previews use the development project from this session on?
Knowledge check
A production deployment in Vercel's list shows no commit and was made from the command line. What does it tell you?
References
- Supabase Docs: Managing environments (migrations from GitHub Actions). https://supabase.com/docs/guides/deployment/managing-environments
- Vercel Docs: Environments (local, preview, production). https://vercel.com/docs/deployments/environments
- Vercel Docs: Deploying from Git. https://vercel.com/docs/deployments/git
- Supabase Docs: Vercel integration. https://supabase.com/docs/guides/integrations/vercel-marketplace
- Claude Code docs: Settings and permission rules. https://code.claude.com/docs/en/settings
Chapter 2 · Session 11
Migrations on merge, and a daily check
You type five secrets into GitHub. One prompt writes the workflow that applies migrations on every merge, development first, and the check that proves every morning that production matches main. Then production learns to wait for the workflow, and you fire the alarm once to prove it reaches you.
40 min4 stepsNo more db push by hand
By the end of this chapter you can
- Type the deploy secrets into GitHub yourself and list them by name in docs/KEYS.md.
- Have Claude Code write deploy.yml and prod-freshness.yml in one pull request.
- Make Vercel wait for the deploy workflow, so a failed migration stops the deploy.
- Fire the freshness alarm on purpose and see it reach both owners.
What the deploy does on every merge
This follows Supabase's own pattern for deploying migrations from GitHub Actions: the workflow links to the project with an access token and the database password, both kept as GitHub secrets, and runs supabase db push [1][2]. A failure stops the job, and everything after it.
One rule keeps this safe. For a minute during every deploy, and for as long as you like after a rollback, the old code runs against the new schema. So every migration must work with the code that is already running: add a column first, start using it in a later change, and remove the old one only when nothing reads it. This is called expand and contract [3]. Adding is safe in one step; renaming, dropping or making a column required is not.
Type the secrets into GitHub yourself
15 minutesWhy
The deploy logs in to Supabase from GitHub's computers, so GitHub needs the keys. Typed by you into GitHub's secrets page, they are encrypted, never shown again, and never pass through a chat or a file. A secret that is missing or wrong is the commonest failure here: on our platform a secret that was never created resolved to an empty string, so nothing complained and nothing worked. Step 4's workflow checks each one's shape before it uses it.
Do this
First the two values that do not exist yet. Each goes straight into the password manager, never anywhere else:
https://supabase.com/dashboard/account/tokens
Generate new token. Name: github-actions-acme-platform. Copy it straight into the password manager as Supabase: access token for GitHub Actions. It is shown once.
In the password manager, generate a 32-character password and save it as Backup passphrase: acme-platform. Every backup from chapter 3 is locked with it: without it, no backup can be opened.
https://supabase.com/dashboard/project/tsrqponmlkjihgfedcba
The development project. Press Connect at the top and copy the Session pooler connection string. It contains [YOUR-PASSWORD]: you replace that with the development project's password inside GitHub's form below, not in a file or a chat.
Then add five repository secrets, one at a time:
https://github.com/acme-ops/acme-platform/settings/secrets/actions
New repository secret for each row below. Paste the value only: no quotes, no NAME=, no spaces at either end.
| Name (type exactly) | Value, from |
|---|---|
| SUPABASE_ACCESS_TOKEN | Password manager: Supabase access token for GitHub Actions |
| PRODUCTION_PROJECT_ID | abcdefghijklmnopqrst (not secret, but kept here so no workflow has it written in) |
| PRODUCTION_DB_PASSWORD | Password manager: Supabase: acme-platform-prod database password |
| DEV_DB_URL | The development project's Session pooler string, with its password in place of [YOUR-PASSWORD] |
| BACKUP_PASSPHRASE | Password manager: Backup passphrase: acme-platform |
| Key or secret | What it unlocks | Where it lives | Who holds it | Never |
|---|---|---|---|---|
| SUPABASE_ACCESS_TOKEN | Acts as you in Supabase, in every organisation you belong to | GitHub Actions secret; password manager | pat@acme.com created it; sam@acme.com can replace it | Never in a file, a chat or a log; never without an expiry note in docs/KEYS.md |
| PRODUCTION_DB_PASSWORD | Full control of the production database | GitHub Actions secret; password manager | pat@acme.com, sam@acme.com | Never on a laptop's command line again after this session |
| DEV_DB_URL | Full control of the development database (made-up data only) | GitHub Actions secret | pat@acme.com, sam@acme.com | Never pointing at the production project |
| BACKUP_PASSPHRASE | Opens every backup | GitHub Actions secret; password manager | pat@acme.com, sam@acme.com | Never only in GitHub: lose it there and every backup is unreadable |
You will see recreated screen
| Repository secrets | Last updated |
|---|---|
| BACKUP_PASSPHRASE | now |
| DEV_DB_URL | now |
| PRODUCTION_DB_PASSWORD | now |
| PRODUCTION_PROJECT_ID | now |
| SUPABASE_ACCESS_TOKEN | now |
- Exactly these five names. A typo in a name is a missing secret to the workflow.
- One at a time: name, value, Add secret.
Check it worked
The page lists exactly the five names, spelled as in the table. The token and the passphrase are in the password manager. Nothing was pasted anywhere else.
Fastest path
Put the password manager and GitHub's page side by side: copy one value, paste, save, next. Five minutes, and no value ever passes through Claude Code.
If it goes wrong
DEV_DB_URL still contains [YOUR-PASSWORD], or the password has characters such as @, # or /
We hit this: a database address refused as copied, because of a placeholder, quotes or a character that must be encoded. The development project holds only made-up data, so the quick fix is safe: reset its password (Project Settings, Database) to one of letters and digits only, save it in the password manager, and type the string into the secret again with Update.
I pasted NAME=value, or the value with quotes
Open the secret, choose Update and paste the value on its own. We hit exactly this in the Academy: a key's value was the variable's own name. Step 4's workflow checks each secret's shape and names any that is wrong, without printing it.
The access token is mine. What happens when I leave?
A Supabase access token acts as the person who made it. Write who holds it in docs/KEYS.md (Step 4 does), and make replacing it part of any owner leaving: owner two generates a new one and updates the secret. The runbook (Step 10) lists the steps.
Claude Code asks me for one of these values
Answer: No. Secrets are typed by a person into GitHub's settings page. Use the secret's name.
One prompt: the deploy workflow and the daily freshness check
15 minutesWhy
The workflow that applies migrations and the check that proves it worked belong in one pull request, so the safety net arrives with the change. One complete prompt also replaces the round trips it takes to add each part as you think of it: the order of jobs, what happens on failure, the secret checks, the alarm and the key register are all in it. Telling Claude Code to read Supabase's guide first stops it inventing a workflow from memory.
Do this
Read CLAUDE.md, docs/KEYS.md, supabase/ and .github/. Read Supabase's guide "Managing environments" (https://supabase.com/docs/guides/deployment/managing-environments) before writing anything.
On a new branch deploy/migrations-on-merge:
1. .github/workflows/deploy.yml, named "deploy", running on every push to main and by hand (workflow_dispatch), one run at a time (a concurrency group that never cancels a running deploy):
- job "secrets": check that SUPABASE_ACCESS_TOKEN, PRODUCTION_PROJECT_ID, PRODUCTION_DB_PASSWORD and DEV_DB_URL are set and have the right shape (the token starts with sbp_; the project id is 20 lower-case letters; DEV_DB_URL starts with postgresql://, does not contain [YOUR-PASSWORD] and does not contain the production project id). If one is wrong, name it and say what is wrong; never print it.
- job "migrate-dev", after "secrets": apply pending migrations to the development project with supabase db push --db-url.
- job "migrate-production", after "migrate-dev": supabase link to PRODUCTION_PROJECT_ID, print supabase migration list, then supabase db push. If anything fails, the job fails and nothing after it runs.
- Use the Supabase CLI through supabase/setup-cli, pinned to one version. Never echo a secret.
2. .github/workflows/prod-freshness.yml, named "prod-freshness", every day at 06:10 UTC and by hand: for production and for development, compare the migrations applied with the files in supabase/migrations on main (supabase migration list and supabase db push --dry-run; it must never apply anything). If either project is missing a migration, or has one main does not have, fail and open a GitHub issue titled "Production is behind main" that lists the versions, assigned to both owners. Give it a manual input fire_alarm that takes the failure path on purpose without touching either database. The issue step must work without a copy of the repository: use gh with --repo and give the job permission to write issues.
3. .github/CODEOWNERS: require review from pat@acme.com and sam@acme.com (CODEOWNERS accepts the email on each GitHub account) for .github/workflows/.
4. docs/KEYS.md: add the five Actions secrets by name (SUPABASE_ACCESS_TOKEN, PRODUCTION_PROJECT_ID, PRODUCTION_DB_PASSWORD, DEV_DB_URL, BACKUP_PASSPHRASE): what each unlocks, where it lives, who holds it, never a value. Note that the access token belongs to the person who made it.
5. CLAUDE.md, under "Deploys and the live database": deploy.yml now applies migrations on merge; nobody runs supabase db push by hand for production.
Do not run either workflow, do not link to or change any Supabase project, and do not read any .env file. Push, open a pull request titled "Migrations on merge", wait for every check to finish, and tell me in plain words what each new workflow will do, which secrets it reads by name, and what happens when a migration fails.You will see recreated screen
- The plan, in the order the prompt gave.
- Secrets by name only. The values arrive on GitHub's computer when the job runs, hidden in logs.
- Read this summary against the prompt: four jobs in order, what failure does, the alarm, owner review.
Check it worked
Pull request Migrations on merge is green on its latest commit; the summary names the jobs secrets, migrate-dev and migrate-production in that order; docs/KEYS.md lists five secret names and no values; GitHub has asked owner two for a review because of CODEOWNERS.
Fastest path
The last paragraph of the prompt (do not run it, wait for the checks, report in plain words) is what turns this into one round trip. The first run happens in Step 5, when you are watching.
If it goes wrong
Claude Code says it needs the database password, or wants to run the workflow to test it
Say: No. The workflow runs on GitHub with the secrets I typed. Its first run is the merge, and I will watch it. The deny rules from Step 1 stop it linking to a project or pushing to a database anyway.
It says done, but the pull request shows a red check
Say: The checks on the pull request are red. Run gh pr checks, read the failed log, fix the cause and wait for green before you report. We trust the check on GitHub, never the summary.
Owner two cannot approve: review is required from a code owner who is not listed
CODEOWNERS names people by their GitHub account. Say: CODEOWNERS does not match owner two's GitHub account. Use their GitHub user name, which I will tell you: <user name>.
Make production wait for the deploy, merge, and watch it run
10 minutesWhy
Vercel builds main the moment it changes, at the same time as the deploy workflow runs. Without a link between them, new code can go live before its migration has run, or stay live after the migration failed. Vercel's deployment checks make a production build wait for named GitHub checks before it goes live: if the deploy is red, the plant keeps the previous version. After this step you no longer run db push by hand for production, and your computer is pointed at the development project so an old habit cannot reach real data.
Do this
Owner two approves pull request Migrations on merge; merge it. Then watch the first run:
https://github.com/acme-ops/acme-platform/actions/workflows/deploy.yml
The run for your merge. Open it and wait for three green jobs.
Then tell Vercel to wait for it:
https://vercel.com/acme-ops/acme-platform/settings
In the project's settings, open Deployment Checks, add a check, and choose deploy / migrate-production from GitHub. It is listed once the workflow has run once. Save.
Finally, point the Supabase tool on your computer at the development project. It asks for the development project's password: copy it from the password manager.
cd ~/code/acme-platform && git switch main && git pull && npx supabase link --project-ref tsrqponmlkjihgfedcbaYou will see recreated screen
| Job | Result | Time |
|---|---|---|
| secrets | ✓ 4 secrets present and well formed | 6s |
| migrate-dev | ✓ Remote database is up to date. | 38s |
| migrate-production | ✓ Remote database is up to date. | 41s |
supabase migration list Local | Remote | Time (UTC) 20261016091500 | 20261016091500 | 2026-10-16 09:15:00 20261017140200 | 20261017140200 | 2026-10-17 14:02:00
- Green, from a push to main, never from a computer.
- Development first, then production. A red line here would stop everything below it.
You will see recreated screen
| Check | Source |
|---|---|
| deploy / migrate-production | GitHub Actions |
- The one check that matters: production waits until migrations have been applied.
Check it worked
The deploy run is green with three jobs; the migration list in its log shows the same versions under Local and Remote; Vercel's deployment checks list deploy / migrate-production; npx supabase migration list on your computer now shows the development project. You do not run npx supabase db push for production again.
If it goes wrong
migrate-production failed
The deploy stopped: production keeps its previous build and schema, which is what you want. Say: The deploy workflow failed in migrate-production. Read its log and tell me in plain words which migration failed, why, and whether anything was applied. Do not touch either project. Propose the fix as a new migration on a new branch. Never edit an applied migration and never apply anything by hand. When the fix merges, the deploy runs again by itself.
Remote migration versions not found in local migrations directory
The live database has a migration main does not: someone changed it outside the files, or an applied file was renamed or deleted. Do not force anything. Say: The deploy says remote migration versions are not in the local directory. Explain in plain words which versions differ and what each did, without changing any project. Then decide with owner two.
The job cannot connect: network is unreachable, or an address full of colons
GitHub's machines do not use the newer kind of internet address (IPv6), and a direct database address may need it. Say: The deploy cannot reach the database from GitHub Actions. Use the connection Supabase recommends for IPv4-only clients (the session pooler), following Supabase's guide, without printing any address.
There is no Deployment Checks page in the project's settings
Vercel renames pages from time to time: search the settings for checks. If it is not there, ask in office hours. Until it is set, open the deploy run after each merge and check it is green before you tell anyone a change is live.
A production deployment shows Canceled
A newer merge replaced it within a minute, so Vercel canceled the older build. We spent hours on this before learning it: a canceled deploy that a newer ready one replaced is not a failure.
Fire the alarm once, on purpose
5 minutesWhy
A check that fails without anyone hearing is worse than no check, because everyone believes it is watching. We hit this: a nightly test caught a real production fault ten nights running, but the step that opens an issue had no copy of the repository and died, so nobody heard. Firing the alarm on purpose once proves the issue really reaches both owners' inboxes.
Do this
https://github.com/acme-ops/acme-platform/actions/workflows/prod-freshness.yml
Run workflow on main, set fire_alarm to true, and run it. When the issue arrives, run it again with fire_alarm false.
You will see recreated screen
Production: no differences (drill) Development: no differences (drill) This issue was opened on purpose to test the alarm.
- Opened by the workflow, not by a person: the alarm path works.
- Both owners. An alarm only one person hears fails when that person is away.
Check it worked
Both owners received the issue email. The second run, with fire_alarm false, is green and says production and development match main. Close the drill issue with a comment: Alarm drill, worked.
If it goes wrong
The run failed but no issue appeared
That is the exact fault we had. Say: prod-freshness failed but opened no issue. Read the failed step's log, fix the issue step so it works without a copy of the repository (gh with --repo, permission to write issues), and tell me what to click to fire it again. Do not run it yourself. Then fire it again yourself.
The check is red every morning and nobody looks
Any check that has been red for two mornings without an open issue is itself a defect. Put the freshness check on the runbook's daily list (Step 10).
A real (not drill) issue says production is behind main
Open the deploy run for the last merge: usually migrate-production failed, and Step 5's first fix applies. Once the cause is fixed and merged, the deploy applies what is missing. If nothing new needs merging, run the deploy workflow by hand on main (Run workflow). Never catch up with db push from a computer.
Knowledge check
After this session, who applies a merged migration to the production database?
Knowledge check
The deploy fails in migrate-production. What does the plant see?
References
- Supabase Docs: Managing environments (GitHub Actions). https://supabase.com/docs/guides/deployment/managing-environments
- Supabase CLI reference: supabase db push. https://supabase.com/docs/reference/cli/supabase-db-push
- Martin Fowler: Parallel Change (expand and contract). https://martinfowler.com/bliki/ParallelChange.html
- GitHub Docs: Using secrets in GitHub Actions. https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions
- GitHub Docs: About code owners. https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners
- GitHub Docs: Events that trigger workflows (schedule, workflow_dispatch). https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
Chapter 3 · Session 11
Roll back, back up, restore, and write the runbook
Two things go wrong after a merge: bad code, which Vercel undoes in seconds, and a bad migration, which nothing undoes, so you fix it forward. Then the rarer, worse case: a lost database, restored from a backup you have already restored once. You rehearse all three in daylight and write them into a runbook someone else can follow.
40 min4 stepsEnds with a gate
By the end of this chapter you can
- Roll the code back in Vercel, undo the rollback, and fix a database change forward with a new migration.
- Add a nightly, encrypted backup that opens an issue if it fails.
- Restore that backup into the development project, compare the row counts and time it.
- Draft docs/runbook.md from the repository and make it true with the drills.
Code goes back; the database goes forward
Vercel keeps every deployment it has built, and the production address points at one of them. Instant Rollback moves that pointer back to an earlier build in seconds, with no rebuild [1]. Two things it does not do: it does not touch the database, and after a rollback new merges stop going live by themselves until you undo it, so a second bad merge cannot undo your rescue [1].
A migration has no rollback button. Once applied it is history, as Session 5 taught: a mistake in it is corrected by a new migration, through a pull request and the same deploy. That is called fixing forward.
Rehearse a rollback: the code goes back, the database goes forward
15 minutesWhy
The first rollback should not happen during an outage with the floor waiting. This drill makes one harmless, visible change to the code and one to the database, then undoes each the right way: the code with Vercel's Instant Rollback, the database with a forward fix. It also gives the deploy workflow its first real migration to apply, so you watch the whole path work end to end.
Do this
On a new branch drill/rollback:
1. Change the heading of the /status page to "ROLLBACK DRILL".
2. Add a new migration (npx supabase migration new rollback_drill) that only sets a comment on the table lots: 'Rollback drill: wrong on purpose'. Add a pgTAP test that the table lots has a comment.
Change nothing else. Open a pull request titled "Rollback drill", wait for every check, and tell me the result.Owner two approves; merge. Wait for the deploy run to go green, then open https://acme-platform.vercel.app/status: it says ROLLBACK DRILL. Now roll it back, with a stopwatch running:
https://vercel.com/acme-ops/acme-platform/deployments
Find the production deployment before the drill, open its menu (⋯) and choose Instant Rollback. Confirm. Refresh https://acme-platform.vercel.app/status until the old heading is back; write down the seconds.
https://supabase.com/dashboard/project/abcdefghijklmnopqrst/editor
Open the table lots: its description still says Rollback drill: wrong on purpose. The rollback did not touch the database.
Now fix it forward:
On a new branch drill/forward-fix:
1. Put the /status heading back as it was before the drill.
2. Add a NEW migration (npx supabase migration new rollback_drill_fix) that sets the comment on lots to 'One row is one lot.' Do not edit the rollback_drill migration: it has been applied to production. Update the test if it needs to.
Open a pull request titled "Forward fix after the drill", wait for every check, and tell me the result.Merge it and wait for the deploy run. Because production is rolled back, the new build does not go live by itself: in Vercel, choose Undo Rollback on the banner, or Promote on the newest production deployment.
You will see recreated screen
| Deployment | Status | Source |
|---|---|---|
| Ready | main · Rollback drill (#37) | |
| Current | Ready | main · Status shows the database (#34) |
- The safety catch: until you undo it, a second bad merge cannot replace your rescue.
- The drill build is still kept, for study. Production points at the one below it.
You will see recreated screen
- A new file, a later time.
- Read for this sentence. If it says it edited the applied file, stop (see below).
Check it worked
https://acme-platform.vercel.app/status shows the normal heading; Vercel's current production deployment is the forward-fix merge, with no rollback banner; the deploy log's migration list shows both drill migrations under Local and Remote; you have the rollback time in seconds.
If it goes wrong
After the rollback, merges no longer go live
That is by design. Choose Undo Rollback, or Promote on the newest production deployment, once the fix is merged.
Claude Code edited the applied rollback_drill migration instead of adding one
Say: Revert the change to the applied migration. Every change to a live table is a new migration. An edited applied file changes nothing in production and makes a fresh database differ from it.
After a real rollback, the old code fails against the new schema
The migration was not one the previous build could live with. Do not roll back further and do not edit the database by hand: fix forward with a new migration or a code change, through a pull request. From now on, add first and remove later (chapter 2).
Backups: what you are promising
Before choosing a backup, agree two numbers with the supervisors. NIST's contingency planning guide names them [2]: the recovery point objective, how much recent data the plant could lose (and re-key from paper), and the recovery time objective, how long the floor can work without the platform. Back up at least as often as the first; practise restoring faster than the second.
| Option | Recovery point | Notes |
|---|---|---|
| Nightly export by GitHub Actions, encrypted, kept as a workflow artifact (this session) | Up to 24 hours | Yours, tested by you, off the database provider. Inside GitHub's included Actions minutes for a small database |
| Weekly download of that export to a company file store | Up to 7 days | The offline copy: the 1 in 3-2-1 |
| Supabase Pro's daily backups | Up to 24 hours | Restored from Supabase's dashboard; the Free plan has no backups you can download [3]. Pro was $25 a month in October 2026; prices change |
| Supabase point-in-time recovery | Minutes | A paid add-on to Pro, for when losing a shift's records is not acceptable [3] |
Add the nightly backup
10 minutesWhy
Supabase's own backups live in the same account as the database, so one mistyped command or one stolen login can reach both. A nightly export run by GitHub, encrypted with a passphrase only the owners hold, is a copy you control and can restore anywhere: the files come from PostgreSQL's standard dump tool, not a Supabase format [5]. If it fails, it opens an issue, the same way the freshness alarm does.
Do this
Read docs/KEYS.md, .github/workflows/deploy.yml and Supabase's guide "Backup and restore using the CLI" (https://supabase.com/docs/guides/platform/migrating-within-supabase/backup-restore).
On a new branch ops/nightly-backup, add .github/workflows/backup.yml, named "backup", every night at 02:40 UTC and by hand:
- check the secrets it needs the same way deploy.yml does, plus BACKUP_PASSPHRASE (at least 24 characters);
- link to the production project and export roles, schema and data with supabase db dump, as the guide shows;
- write counts.txt: the number of rows in each table of the public schema, counted from the data export;
- put the files in one archive and encrypt it with gpg (AES256) using BACKUP_PASSPHRASE; nothing unencrypted is uploaded or printed;
- upload the encrypted file as an artifact named backup-<date>, kept 30 days;
- if any step fails, open an issue "Backup failed" assigned to both owners, the same way prod-freshness does.
Add a line on backups to docs/KEYS.md under BACKUP_PASSPHRASE. Do not run the workflow. Open a pull request titled "Nightly backup", wait for every check, and tell me in plain words what it does each night and where the file goes.Merge it, then make the first backup now rather than waiting for tonight:
https://github.com/acme-ops/acme-platform/actions/workflows/backup.yml
Run workflow on main. When it is green, the artifact is at the bottom of the run's page.
Every Monday, download the newest artifact into the company file store folder IT gives you: that is the offline copy. The runbook (Step 10) carries the reminder.
You will see recreated screen
counts.txt sites 1 equipment 6 parts 14 characteristics 41 lots 0 memberships 2 inspections 0 holds 0
| Artifacts | Size | Expires |
|---|---|---|
| backup-2026-10-21.tgz.gpg | 38 KB | in 30 days |
- Green. A red run opens a "Backup failed" issue for both owners.
- Encrypted. Anyone with read access to the repository can download it; without the passphrase it is noise.
Check it worked
The run is green, one encrypted artifact is listed, and the log shows a count for each table but no rows. Production still has only reference rows and memberships: no plant records yet.
If it goes wrong
The artifact is downloadable by anyone with read access to the repository
Correct, and the reason it is encrypted. Keep repository access to the people the role matrix allows, and the passphrase in GitHub and the password manager only.
Last night's backup did not run at 02:40
GitHub may delay scheduled runs when it is busy, most often at the start of an hour; 02:40 avoids the worst. If a night has no run at all, run it by hand and check the schedule line in backup.yml.
The passphrase is lost
Every backup locked with it is unreadable. That is why it lives in the password manager too, and why Step 9 restores with it: a restore proves the passphrase you hold is the one that locks the files.
Photos or files attached to records are not in the backup
Database exports do not include Supabase Storage. There are none yet; when a later session adds files, it adds them to the backup.
Restore it into the development project, and time it
15 minutesWhy
A backup nobody has restored is only a hope: the day you need it, it is corrupt, slow, or restores to a database the code no longer matches. Restoring is its own skill with its own failures, so you do it once now. Production holds only the reference rows from your migrations and the memberships of the people invited in Session 9, no plant records, so this first restore can go into the development project. Session 23 repeats it as a witnessed drill into a throwaway project, once real records exist.
Do this
Read .github/workflows/backup.yml and Supabase's guide "Backup and restore using the CLI".
On a new branch ops/restore-drill, add .github/workflows/restore-drill.yml, named "restore-drill", run only by hand, with an input backup_run (empty means the latest successful backup run):
- refuse to start unless DEV_DB_URL is set and does not contain PRODUCTION_PROJECT_ID: this workflow must never be able to write to production;
- download that run's artifact and decrypt it with BACKUP_PASSPHRASE;
- empty the public tables of the development project, then restore the public schema's data from the backup as the guide shows;
- count the rows of each public table in the development project and compare them with counts.txt; fail if any differs;
- print a table (table, rows in backup, rows restored) and the minutes from start to finish.
Never print data or secrets. Do not run the workflow. Open a pull request titled "Restore drill", wait for every check, and tell me in plain words what it does and what it can never do.Merge it. Start a stopwatch, then run it:
https://github.com/acme-ops/acme-platform/actions/workflows/restore-drill.yml
Run workflow on main, backup_run left empty.
When it is green, open the preview of the Restore drill pull request (previews use the development project): /sign-in must load, and a protected page opened signed out must send you to sign-in. Stop the stopwatch. Then put the development project back to made-up data, as in Session 6: empty the tables and run the seed.
https://supabase.com/dashboard/project/tsrqponmlkjihgfedcba/sql/new
The development project's SQL editor: paste the truncate line and then supabase/seed.sql from GitHub, and run.
You will see recreated screen
| Table | Rows in backup | Rows restored |
|---|---|---|
| sites | 1 | 1 |
| equipment | 6 | 6 |
| parts | 14 | 14 |
| characteristics | 41 | 41 |
| memberships | 2 | 2 |
- The guard ran first: this workflow cannot point at production.
- Counts compared table by table, not "it looked fine".
- Add the time for the preview check: that total is the number for the drill log.
Check it worked
Every count matches; the preview's sign-in page loads and a protected page refuses you signed out; you have the total minutes; and the development project's Table Editor shows the DEMO rows again after the seed.
If it goes wrong
The workflow refuses: DEV_DB_URL points at production
It is doing its job. Fix the secret (Step 3) so it holds the development project's string. Never weaken the guard to make the drill run.
The restore fails with "relation does not exist" or "column does not exist"
The development project does not match the schema the backup came from: it is behind main. Check the latest prod-freshness run, let the deploy bring development up to date (run deploy by hand on main), then run the drill again. This is one of the ways restores fail on the day, which is why you rehearse.
A count differs
Do not sign the drill off. Say: The restore drill's counts differ for <table>. Explain why from the run's log without changing any project, and propose a fix to the backup or the restore.
A protected page shows data to a signed-out visitor on the preview
Stop. Sign-in or the access rules (Sessions 8 and 9) are not doing their job. Open an issue, tell owner two, and merge nothing else until the failing test exists and passes.
The drill took longer than your recovery time objective
Write the real number down anyway: it is the most useful line in the runbook. Then decide with the supervisors whether to restore faster (Supabase Pro's own backups) or plan a longer paper fallback.
Write the runbook from the repository, then make it true
10 minutesWhy
When the platform misbehaves at 03:00 and you are on holiday, owner two follows a page, not your memory. The repository already knows most of that page: the workflows, the key names, the drills you just did. Claude Code drafts it from them in minutes; you fill in what only people know (who to call, the recovery numbers, the drill times), and owner two approves it only if they could follow it without you.
Do this
Read CLAUDE.md, docs/KEYS.md, .github/workflows/, supabase/migrations/ and the /status page.
On a new branch docs/runbook, write docs/runbook.md in plain English for the second owner, sam@acme.com, who must be able to follow it without me:
1. What it is: the live address https://acme-platform.vercel.app, the repository acme-ops/acme-platform, the Supabase projects acme-platform-prod (production) and acme-platform-dev (development), the Vercel project, and the owners.
2. Who to call: a table for me to fill in (role, name, phone, hours).
3. Deploy: merging to main is the only way. What deploy.yml does, step by step, and how to check it worked (the deploy run, Vercel, /status). Say plainly that nobody runs supabase db push for production, and nobody deploys from a computer.
4. Roll back the code: Vercel Instant Rollback, then Undo Rollback once fixed. What it does not undo.
5. Fix a migration: always forward, with a new migration; never edit an applied file.
6. Restore a backup: where backups are, the restore-drill workflow, why a drill never restores into production, the weekly offline copy, and our recovery point and recovery time objectives (leave them for me to fill in).
7. Keys: every secret and variable by NAME from docs/KEYS.md, where it is set, who holds it and how to replace it; never a value.
8. Daily: read the prod-freshness and backup results; a red one without an open issue is a defect.
9. Known problems: a table (symptom, check, fix) starting with: deploy failed in migrate-production; prod-freshness red; a canceled production deployment; a build out of memory; a red check on an old commit.
10. Drill log: a table (date, who, procedure, minutes, what changed) with today's rollback drill and restore drill; leave the minutes for me to fill in.
Every step names the exact page or command. Open a pull request titled "Runbook", and confirm that no key, password or token value appears anywhere in it.Fill in Who to call, the two recovery numbers you agreed with the supervisors, and the drill minutes, in the pull request's Files changed tab (edit the file there) or by telling Claude Code. Owner two reads it as if you were away, and approves only if they could follow every step.
You will see recreated screen
- The sentence that matters most after this session, in the runbook where owner two will read it.
- The drills you did today, ready for their times.
- It says what is missing. Those gaps are yours to fill, not Claude Code's to guess.
Check it worked
docs/runbook.md is on main with ten sections, Who to call and the recovery numbers filled in, both drills in the log with their times, no key value anywhere, and owner two's approval.
If it goes wrong
A key, password or token value appears in the draft
Stop. Do not merge. Say: Remove every value; name the key and where it is set instead. Then treat that key as exposed: replace it the same day (the runbook's Keys section says how) and note the date in docs/KEYS.md. A runbook is printed; a key on paper is a key on the notice board.
The runbook describes something nobody has done
Mark that section not yet drilled. Only what has been done and timed is proven; Session 23 adds the witnessed restore drill.
Owner two cannot follow a step
That is the review working. Fix the step in the same pull request until they can. A runbook only its author can follow is a diary.
Knowledge check
What does a Vercel Instant Rollback not undo?
Knowledge check
A migration already applied in production has a mistake. What do you do?
Knowledge check
Which backup counts as proven?
References
- Vercel Docs: Instant Rollback. https://vercel.com/docs/instant-rollback
- NIST SP 800-34 Rev. 1: Contingency Planning Guide for Federal Information Systems. https://csrc.nist.gov/pubs/sp/800/34/r1/upd1/final
- Supabase Docs: Database backups. https://supabase.com/docs/guides/platform/backups
- CISA: #StopRansomware Guide. https://www.cisa.gov/stopransomware/ransomware-guide
- PostgreSQL Documentation: pg_dump. https://www.postgresql.org/docs/current/app-pgdump.html
- Supabase Docs: Backup and restore using the CLI. https://supabase.com/docs/guides/platform/migrating-within-supabase/backup-restore
- GitHub Docs: Storing and sharing data from a workflow (artifacts). https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/storing-and-sharing-data-from-a-workflow
Chapter 4 · 12 questions · 80% passes
Final assessment
Twelve questions across the session. Score 80% (10 of 12) to pass. Your LMS records your score and each answer; you can review the chapters and try again.
15 min12 questions≈ 15 minutesRetake allowed
Your result
CivOps AI Academy
Deploy and Run: Migrations on Merge, Previews, Rollback, Backups and the Runbook
Element F18 complete · Learner
Your LMS records this completion. For the CivOps Foundation certificate, finish the Foundation Course at https://civops.io/learn.