Bỏ qua

Environments — Quản lý Môi trường

3 Môi trường

Project chạy trên 3 môi trường riêng biệt, mỗi môi trường có database, domain, và config riêng.

graph LR
    A["🖥️ Local Dev<br/>localhost"] -->|Push to develop| B["🧪 Test/Staging<br/>test.chienle.dev"]
    B -->|Verify OK| C["🚀 Production<br/>chienle.dev"]

So sánh chi tiết

URLs & Domains

🖥️ Local Dev 🧪 Test/Staging 🚀 Production
Frontend localhost:3000 test.chienle.dev chienle.dev
Backend localhost:8000 api-test.chienle.dev api.chienle.dev
Swagger localhost:8000/docs api-test.chienle.dev/docs api.chienle.dev/docs

Infrastructure

🖥️ Local Dev 🧪 Test/Staging 🚀 Production
Frontend Host npm run dev Cloudflare Pages (Preview) Cloudflare Pages
Backend Host uvicorn --reload Northflank (staging) Northflank
Database Local PostgreSQL Supabase (staging project) Supabase (prod project)
Storage R2 (shared bucket) R2 (shared hoặc staging bucket) R2 (prod bucket)
Branch feature/*, develop staging main
Deploy Manual Auto on push Auto on push

Environment Variables

Backend (backend/.env)

DATABASE_URL=postgresql://postgres:123456@localhost:5432/course_dev
CORS_ORIGINS=http://localhost:3000
R2_ENDPOINT=https://your-account.r2.cloudflarestorage.com
R2_KEY=your-key
R2_SECRET=your-secret
R2_BUCKET=your-bucket
CDN_BASE=https://your-cdn
DATABASE_URL=postgresql://...staging-supabase...?sslmode=require
CORS_ORIGINS=https://test.chienle.dev
R2_ENDPOINT=https://your-account.r2.cloudflarestorage.com
R2_KEY=your-key
R2_SECRET=your-secret
R2_BUCKET=your-staging-bucket
CDN_BASE=https://your-staging-cdn
DATABASE_URL=postgresql://...prod-supabase...?sslmode=require
CORS_ORIGINS=https://chienle.dev,https://www.chienle.dev
R2_ENDPOINT=https://your-account.r2.cloudflarestorage.com
R2_KEY=your-key
R2_SECRET=your-secret
R2_BUCKET=your-prod-bucket
CDN_BASE=https://cdn.chienle.dev

Frontend (frontend/.env.local)

NEXT_PUBLIC_API=http://localhost:8000
NEXT_PUBLIC_API=https://api-test.chienle.dev
NEXT_PUBLIC_API=https://api.chienle.dev

Không bao giờ commit .env files

  • .env.env.local đã có trong .gitignore
  • Staging/Prod env vars được set trên Platform (Northflank, Cloudflare Pages)
  • Chỉ commit .env.example với giá trị mẫu

Database per Environment

Tách biệt database

Mỗi môi trường phải có database riêng. Không bao giờ dùng chung database giữa staging và production.

Setup Database cho từng môi trường

Môi trường Database Cách tạo
Local course_dev CREATE DATABASE course_dev; trên local PostgreSQL
Staging Supabase project riêng Tạo project mới trên Supabase
Production Supabase project riêng Tạo project mới trên Supabase

Chạy Migration cho từng môi trường

# Local
alembic upgrade head

# Staging (đổi DATABASE_URL tạm thời)
DATABASE_URL="postgresql://...staging..." alembic upgrade head

# Production
DATABASE_URL="postgresql://...prod..." alembic upgrade head

Checklist thêm môi trường mới

  • Tạo branch staging từ develop
  • Tạo Supabase project mới cho staging
  • Cấu hình Northflank service mới (staging), chọn branch staging
  • Cấu hình Cloudflare Pages Preview cho branch staging
  • Set tất cả env vars trên cả 2 platforms
  • Chạy alembic upgrade head trên staging database
  • Setup DNS cho test.chienle.devapi-test.chienle.dev
  • Test toàn bộ flow trên staging