Step 10 of 10IntermediateDeployDevOps

Deploy with separate dev and prod environments

Ship Inkwell to production with its own database, OAuth app, bucket and secrets, set only in the host's settings. Make every schema change go through migrations automatically, then break production on purpose and practise the recovery.

Project
Inkwell
Time
3-5 hours

You write the code. This guide explains the ideas, the order of work and the traps. It never hands you the finished code, because building it yourself is the point.

What you're building

A real production Inkwell at a public URL, which friends can sign into and write on, fully separate from the copy you break every day on your laptop. Each environment has its own database, GitHub OAuth app, upload bucket and secrets. The production secrets live only in Vercel's settings: not in git, not in a chat message, not on your laptop.

Schema changes reach production the same way every time: you commit a migration file, and the deploy applies it. Then you break production deliberately and fix it, so the first time it happens for real, you already know the steps.

Why it matters

When production breaks, the cause is very often something that just changed: a deploy, a config edit, a schema change. Teams protect themselves with exactly what you'll build here, separate environments, secrets kept out of code, repeatable migrations and a practised rollback. Interviewers ask "how do you deploy?" and "what happens when a deploy goes wrong?" to find out whether you've run something real. This is also the last project where breaking prod costs nothing, so use it.

Before you start

  • Environments. Vercel has three: Development (your laptop), Preview (a deploy for each branch or pull request) and Production (your main branch). Each environment variable can be set for any of them, with different values.
  • Hosted Postgres with branches. Neon runs Postgres for you and can make branches, cheap copies of a database. Use the main branch for production and a separate dev branch for Preview deploys. Your laptop keeps using Docker.
  • Migration workflow. Every schema change is a numbered SQL file generated by drizzle-kit and committed to git. drizzle-kit migrate applies, in order, the files a database hasn't seen yet, and records which ones it applied. Every environment replays the same files, so they can't drift apart.
  • Backward-compatible migrations. During a deploy, and after a rollback, old code runs against the new schema. So a migration should never break the code currently running: add columns as nullable or with defaults, and rename in stages (add new, copy, switch code, drop old later).
  • Rollback. Going back to the previous working deploy. On Vercel, Instant Rollback points production at an earlier deployment in seconds. It rolls back code, not your database.

Build it step by step

Step 1: Create the production resources

Create a Neon project. Its main branch is production, and you add a dev branch for previews. Create a second GitHub OAuth app with your production URL and the callback https://<your-domain>/api/auth/callback/github. Create a second bucket with its own narrow access key and a CORS rule for your production origin. Generate a fresh AUTH_SECRET. Nothing is shared with dev.

Neon gives you a pooled connection string (with -pooler in the host) and a direct one. Use the pooled one for the app, because serverless functions open many short connections. Check Neon's connection docs for which one to use for migrations.

Check it: every production value is in a password manager or Vercel, and none is in your repo.

Step 2: Set variables per environment in Vercel

In the Vercel project settings, add every key from .env.example. Production values go in Production. The Neon dev branch URL and dev bucket go in Preview. Then validate them at startup: write a small module, src/env.ts, that parses process.env with a Zod schema and throws a clear error naming any missing or malformed variable. Import it from your db and auth modules, so a bad config fails at once, not halfway through a request.

One snag: preview URLs change on every deploy, and a GitHub OAuth app accepts only one callback URL, so sign-in on previews needs extra setup. Auth.js documents a redirect proxy for this. For this project, it's fine to test sign-in locally and in production only.

Check it: remove one variable from Preview and redeploy. The logs show your "missing DATABASE_URL" message. Put it back.

Step 3: Make deploys run migrations

In the Vercel project settings, override the Build Command so migrations run before the build:

npm run db:migrate && npm run build

The DATABASE_URL set for each environment decides which database gets migrated: Preview builds migrate the dev branch, Production builds migrate production. If a migration fails, the build fails, and Vercel keeps serving the previous deployment. From now on, never run drizzle-kit push or hand-written SQL against production. Every change is a generated migration file in a commit.

Check it: add a nullable column (for example subtitle), generate the migration, push a branch, and watch the Preview build log apply it to the dev branch. Merge, and watch Production apply it.

Step 4: Audit your secrets

Search the whole history, not just the current files: git log -p | grep -iE "secret|password|AKIA|postgres://". Anything real that turns up has to be rotated: generate a new value at the provider and delete the old one. Removing it from history isn't enough, because a public repo can be copied within minutes. Turn on GitHub's secret scanning and push protection for the repo if they're available to you.

Check it: the search returns only .env.example placeholders and documentation.

Step 5: Break production on purpose

Do all three, one at a time, and time how long each recovery takes:

1. Bad code:     make the home page throw an error, merge, watch it fail.
                 Recover: Instant Rollback in Vercel, then fix forward.
2. Bad config:   delete AUTH_SECRET from Production and redeploy.
                 Recover: read the startup error in the logs, restore, redeploy.
3. Bad migration: add a NOT NULL column with no default to posts (which has rows),
                 and push it straight to main.
                 Watch: the build fails at migrate, and the old version keeps serving.
                 Recover: remove that migration (drizzle-kit's journal file tracks it
                 too), make the column safe, generate again, push.

Read the Vercel runtime logs each time: knowing where errors show up is half the lesson.

Check it: after each one, production works again, and you can say which tool told you what was wrong.

Step 6: Write the runbook

Add a "Deploying" section to the README: how environments are split, where secrets live (and that they're never committed), how migrations run, and the steps to roll back. Add the rule for backward-compatible migrations. Future you, at midnight, is the reader.

Check it: a friend can answer "the site is down, what do I do?" from the README alone.

Common mistakes

  • Sharing one database between dev and prod. Symptom: test posts appear on the live site, or a reset script wipes real data. Fix: separate databases, and production's URL exists only in Vercel.
  • Wrong OAuth callback in production. Symptom: GitHub says the redirect_uri isn't associated with the app. Fix: a production OAuth app whose callback matches your production domain exactly.
  • Running drizzle-kit push or manual SQL on prod. Symptom: prod has a column that no migration file mentions, and the next migration fails. Fix: generated migrations only. Bring prod back in line with a new migration.
  • A secret with the NEXT_PUBLIC_ prefix. Symptom: the value is readable in your JavaScript bundle in DevTools. Fix: remove the prefix, rotate the secret, and read it only on the server.
  • Expecting a rollback to undo the database. Symptom: you roll back the code, but the new column is still there, and the old code crashes on it. Fix: keep migrations backward compatible, so old code works with the new schema.

Say it in the interview

"Inkwell has separate dev, preview and production environments, each with its own database, OAuth app, bucket and secrets. Production secrets live only in Vercel, and the app checks its config with Zod at startup. Schema changes are migration files that run in the build, so if a migration fails, the deploy fails and the old version keeps serving. Before launch I broke prod three ways on purpose and practised recovering. The tradeoff with migrating in the build step is that it's simple but runs on every deploy, so every migration has to be safe to run while the old code is still live."

Done when

0/5 checked

Test each one yourself. If one fails, you're not done yet, and that's fine.

Stretch goals

  • Move migrations into a GitHub Actions job that runs before the production deploy, and require pull requests to pass lint, type checks and your draft test.

Go deeper

The official docs for everything in this step.