Overview
Deploy to Vercel with automatic previews, environment management, and database migrations. Every push to main deploys to production.
What it is
Automated deployment pipeline with Vercel for hosting, preview environments for PRs, and Prisma migrations for database schema changes.
Why we use it
Zero-config deployment, automatic SSL, edge network distribution, and instant rollbacks when needed.
When to use
Every code change. Push to main for production, create PRs for preview environments.
Key Features
- Automatic deployment on git push
- Preview URLs for every pull request
- Environment-specific configuration
- Instant rollback to previous deployments
Quick Start
Deploy to Production
Push to main branch to trigger production deployment.
# Deploy to production via GitHub # 1. Push to main branch git push origin main # Vercel automatically: # - Builds the Next.js app # - Runs Prisma migrations (if configured) # - Deploys to production edge network # - Creates preview for PRs
Patterns
Preview Deployments
Every PR gets a unique preview URL for testing.
# Preview deployments for pull requests # Every PR automatically gets a preview URL # Create a branch git checkout -b feature/new-component # Make changes and push git add . git commit -m "Add new component" git push -u origin feature/new-component # Vercel creates: https://project-git-feature-new-component.vercel.app
Environment Variables
Configure secrets per environment in Vercel dashboard.
# Environment variables in Vercel Dashboard # Settings → Environment Variables # Required for production: DATABASE_URL=postgresql://... BETTER_AUTH_SECRET=... RESEND_API_KEY=... UPSTASH_REDIS_REST_URL=... UPSTASH_REDIS_REST_TOKEN=... # Per-environment overrides: # Production / Preview / Development
Database Migrations
Run Prisma migrations on deployment.
# Database migrations workflow # 1. Create migration locally npx prisma migrate dev --name add_user_roles # 2. Review generated SQL cat prisma/migrations/*/migration.sql # 3. Commit migration files git add prisma/ git commit -m "Add user roles migration" # 4. Production migration runs on deploy # Configure in vercel.json or package.json build script
Rollback Strategy
Revert to previous deployment if issues arise.
# Rollback to previous deployment # In Vercel Dashboard: Deployments → Select → Promote # Or via CLI: vercel rollback [deployment-url] # For database rollback: # Always create reversible migrations # Or restore from Neon backup
Watch Out
Hardcoding secrets in source code
Don't
// Secrets in code const API_KEY = 'sk-live-abc123...'; // NEVER! const dbUrl = 'postgresql://user:pass@host/db'; // NO!
Do
// Secrets via environment variables
const apiKey = process.env.API_KEY;
const dbUrl = process.env.DATABASE_URL;
// Validated at startup
if (!process.env.REQUIRED_VAR) {
throw new Error('REQUIRED_VAR not set');
}Running destructive migrations without backup
Don't
-- Breaking migration (data loss!) ALTER TABLE users DROP COLUMN email; -- NO! DROP TABLE user_sessions; -- NEVER!
Do
-- Safe migration (reversible) ALTER TABLE users ADD COLUMN email_verified BOOLEAN DEFAULT false; -- Or with backfill: ALTER TABLE users ADD COLUMN role VARCHAR(20); UPDATE users SET role = 'member' WHERE role IS NULL; ALTER TABLE users ALTER COLUMN role SET NOT NULL;
- Missing environment variables in production
- Deploying untested code to production
Related
Workflow:
Push to main → Vercel builds → Runs migrations → Deploys to production