ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
စာအုပ်ထုတ်ဝေတိုက်တစ်ခုကို စဉ်းစားကြည့်ပါ။ စာရေးဆရာ ပြင်ဆင်မှု တစ်ခါလုပ်တိုင်း အယ်ဒီတာက ကြည့်ဖို့ proof မိတ္တူ တစ်စောင် ထုတ်ပေးပါတယ်၊ ဒါက preview deployment ပါ။ အယ်ဒီတာ သဘောတူမှ ဆိုင်တွေကို ပို့တဲ့ စာအုပ်အစစ် ထွက်ပါတယ်၊ ဒါက production deployment ပါ။
စာအုပ် ထုတ်ပြီးတာနဲ့ အလုပ်မပြီးသေးပါဘူး။ ဘယ်မူ ထုတ်ခဲ့လဲ၊ အမှား တွေ့ရင် ယခင်မူကို ဘယ်လို ပြန်ထုတ်မလဲ သိထားရပါတယ်။ Deploy လုပ်တာလည်း command တစ်ခု အောင်ရုံ မဟုတ်ဘဲ ဒီမေးခွန်းတွေကို ဖြေနိုင်ဖို့ပါ။
Problem → Why → Solution
ပြဿနာ: preview က production data ကို ဖျက်လိုက်တယ်
Team တစ်ခုက DATABASE_URL ကို environment အားလုံးအတွက် တန်ဖိုး တစ်ခုတည်း ထည့်ထားပါတယ်။ Developer တစ်ယောက်က "note အားလုံး ဖျက်ရန်" ဆိုတဲ့ admin feature ကို branch တစ်ခုမှာ စမ်းရေးပြီး push လုပ်ပါတယ်။ Preview URL ရလာတော့ စမ်းကြည့်ဖို့ button ကို နှိပ်လိုက်ပါတယ်။
Preview deployment က production database ကို ချိတ်ထားလို့ user အစစ်တွေရဲ့ note တွေ ပျောက်သွားပါတယ်။ ပြီးတော့ production ကို ဘယ် commit က run နေလဲ၊ backup ဘယ်မှာလဲ ဆိုတာကို ချက်ချင်း မဖြေနိုင်ကြပါဘူး။
Key Value Environments
DATABASE_URL postgres://app@prod-db.internal/notes Production, Preview, Development
AUTH_SECRET (one shared secret) Production, Preview, Developmentဘာကြောင့် ဒီ setup က အန္တရာယ်များတာလဲ
Preview deployment တွေက မပြီးသေးတဲ့၊ review မလုပ်ရသေးတဲ့ code ကို run ဖို့ ရည်ရွယ်ထားတာပါ။ အဲဒီ code ကို production database နဲ့ secret တွေ ပေးလိုက်ရင် စမ်းသပ်မှု တိုင်းက user အစစ်တွေကို ထိခိုက်နိုင်ပါတယ်။ Environment တစ်ခုစီကို database၊ secret သီးသန့် ပေးမှ preview ကို လွတ်လွတ်လပ်လပ် စမ်းလို့ ရပါတယ်။
Core concept: Git ကို အခြေခံတဲ့ deployment flow
Vercel က Next.js ကို ဖန်တီးထားတဲ့ company ရဲ့ hosting platform ဖြစ်ပြီး Next.js app တွေကို server setup မလိုဘဲ deploy လုပ်နိုင်ပါတယ်။ Next.js ကို ထောက်ပံ့တဲ့ platform တခြားတွေလည်း ရှိပြီး နောက် lesson မှာ Docker နဲ့ ကိုယ်တိုင် host လုပ်ပုံကို လေ့လာပါမယ်။ ဒီမှာ သင်ယူတဲ့ preview၊ environment ခွဲခြားမှု၊ smoke test၊ rollback ဆိုတဲ့ အယူအဆတွေက platform ဘယ်ဟာ သုံးသုံး အသုံးဝင်ပါတယ်။
GitHub၊ GitLab ဒါမှမဟုတ် Bitbucket repository ကို import လုပ်လိုက်ရင် Vercel က framework ကို အလိုလို သိပြီး build command ကို သတ်မှတ်ပေးပါတယ်။ Monorepo ဖြစ်ရင်တော့ Next.js app ရှိတဲ့ root directory ကို ကိုယ်တိုင် ရွေးပေးရပါတယ်။ အဲဒီနောက်ပိုင်း deploy လုပ်တာက git push ပါပဲ။
From push to production
-----------------------
git push feature/tags ──> build ──> Preview (unique URL per deployment)
uses Preview variables + test DB
│
│ pull request reviewed, merged into main
▼
git push main ─────────> build ──> Production (your domain)
uses Production variables + prod DB
│
▼
smoke test passes? ── no ──> roll back to the previous deploymentEnvironment variable တစ်ခုစီကို Production၊ Preview၊ Development ဆိုတဲ့ environment သုံးခုထဲက ဘယ်မှာ သုံးမလဲ ရွေးနိုင်ပါတယ်။ DATABASE_URL ကို Production မှာ production database၊ Preview မှာ test database လို့ တန်ဖိုး ကွဲကွဲ ထည့်ပါ။ Variable ကို ပြောင်းရင် deployment အသစ်မှသာ အသက်ဝင်လို့ ပြောင်းပြီးတိုင်း redeploy လုပ်ရပါမယ်။ ယခင် lesson မှာ ပြောခဲ့သလို NEXT_PUBLIC_ variable တွေကတော့ build ထဲ ပါသွားတာမို့ ဒါက ပိုတောင် အရေးကြီးပါတယ်။
Deploy တိုင်းမှာ commit SHA နဲ့ branch ကို ချိတ်ထားလို့ dashboard ကနေ production က ဘယ် commit ကို run နေလဲ အမြဲ သိနိုင်ပါတယ်။ Deploy ပြီးနောက် domain၊ HTTPS၊ log၊ database connection ကို စစ်ပြီး smoke test အောင်မှ "ပြီးပြီ" လို့ ယူဆပါ။
ဒီလို platform ပေါ်က deployment တစ်ခုဟာ immutable ဖြစ်ပါတယ်: build ပြီးတာနဲ့ ဘယ်တော့မှ မပြောင်းတော့ဘဲ push တိုင်းက deployment အဟောင်းတွေဘေးမှာ အသစ်တစ်ခုကို ထပ်ထုတ်ပါတယ်။ ဒါကြောင့် rollback လုပ်ရတာ လွယ်ကူပါတယ်။ ပြန်ဆုတ်တယ်ဆိုတာ code အဟောင်းကို ပြန် build လုပ်တာ မဟုတ်ဘဲ ရှိပြီးသား၊ အလုပ်လုပ်တယ်လို့ သိပြီးသား deployment ဆီ domain ကို ပြန်ညွှန်တာပါ။ Release တွေ ဒီဇိုင်းဆွဲတဲ့အခါ ဒါကို သတိထားပါ၊ database ပြောင်းလဲမှုကတော့ ဒီလို ပြန်ညွှန်လို့ မရတဲ့ တစ်ခုတည်းသော အရာ ဖြစ်လို့ပါ။
- Branch push က preview URL ထုတ်
- Preview က test DB ကိုပဲ ချိတ်
- main merge က production ထုတ်
- Smoke test မအောင်ရင် rollback
Production ကို ဘယ် commit က run နေလဲ၊ ပြဿနာ ဖြစ်ရင် ဘယ် deployment ကို ပြန်တင်မလဲ ဆိုတာကို အချိန်မရွေး ဖြေနိုင်ရပါမယ်။
အခြေခံ ပုံစံ: branch ကနေ production ထိ
git switch -c feature/<name>
git push -u origin feature/<name> # -> preview deployment with its own URL
# open a pull request, review the preview URL, then merge into main
git switch main
git pull
# merged commits on main -> production deploymentFeature တစ်ခုစီကို branch တစ်ခုမှာ ရေးပြီး push လုပ်ရင် preview ရပါတယ်။ Review ပြီး main ကို merge လုပ်မှ production deployment ဖြစ်ပါတယ်။ Production branch ကို project setting မှာ ပြောင်းလို့ ရပါတယ်။
ဥပမာ: Myanmar Notes ရဲ့ ပထမဆုံး deploy
Code ကို remote repository ကို push လုပ်ပြီး Vercel dashboard မှာ project အသစ်အဖြစ် import လုပ်ပါတယ်။ Dashboard ရဲ့ menu နာမည်တွေက အချိန်နဲ့အမျှ ပြောင်းနိုင်လို့ အဆင့်တွေကို ယေဘုယျ ပုံစံနဲ့ ရေးထားပါတယ်။ ပထမဆုံး deploy မလုပ်ခင် environment variable တွေကို ထည့်ထားဖို့ မမေ့ပါနဲ့၊ မဟုတ်ရင် build က fail ဖြစ်တတ်ပါတယ်။
git add .
git commit -m "Build Myanmar Notes"
git push origin main
# Vercel dashboard
# 1. New Project → Import repository
# 2. Environment Variables ထည့်ပါ
# 3. Deploy နှိပ်ပါ
# 4. Production URL တွင် smoke test လုပ်ပါCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Record a known version
git commit -m "Build Myanmar Notes"Deployment တိုင်းက commit တစ်ခုနဲ့ ချိတ်နေလို့ commit message ကို ရှင်းရှင်းရေးပါ။ ပြဿနာ ဖြစ်ရင် ဒီ SHA နဲ့ ဘာပြောင်းခဲ့လဲ ပြန်ရှာပါတယ်။ git add . မလုပ်ခင် .env.local လို file တွေ .gitignore ထဲ ရှိလား စစ်ပါ။
- The push that triggers a deploy
git push origin mainProject ကို ချိတ်ပြီးနောက် main ကို push လုပ်တိုင်း production deployment အသစ် စတင်ပါတယ်။ တခြား branch တွေကို push လုပ်ရင်တော့ preview deployment ဖြစ်ပါတယ်။
- One-time project setup
# 1. New Project → Import repositoryRepository ကို import လုပ်တဲ့အခါ framework preset က Next.js ဖြစ်လား၊ root directory မှန်လား စစ်ပါ။ ဒါကို project တစ်ခုအတွက် တစ်ကြိမ်ပဲ လုပ်ရပါတယ်။
- Variables before the first build
အဆင့် 2 မှာ DATABASE_URL နဲ့ AUTH_SECRET လို variable တွေကို environment အလိုက် ထည့်ပါ။ Build က static page တွေကို render လုပ်ရင်း ဒီ variable တွေကို လိုအပ်နိုင်လို့ နောက်မှ ထည့်ရင် redeploy လုပ်ရပါမယ်။
- Done means verified
နောက်ဆုံး အဆင့်က production URL မှာ smoke test လုပ်တာပါ။ Build အောင်တာက code compile ဖြစ်တယ်လို့ပဲ ပြောနိုင်ပြီး login၊ database၊ ပုံတွေ အလုပ်လုပ်လား ဆိုတာကိုတော့ မပြောနိုင်ပါဘူး။
HTTPS ပါသော production URL နှင့် commit တိုင်းအတွက် preview deployment workflow ရရှိမည်။Preview က production database ကို မထိအောင် ကာကွယ်ခြင်း
Dashboard မှာ variable ကို မှန်မှန် ခွဲထားတာက ပထမ ကာကွယ်မှုပါ။ ဒုတိယ ကာကွယ်မှုအဖြစ် app စတဲ့အခါ code ကိုယ်တိုင် စစ်တာ ထည့်နိုင်ပါတယ်။ Vercel က VERCEL_ENV ဆိုတဲ့ system variable ကို production၊ preview၊ development ထဲက တစ်ခုနဲ့ ပေးလို့ production မဟုတ်တဲ့ deployment က production database host ကို ချိတ်ဖို့ ကြိုးစားရင် ချက်ချင်း ရပ်ခိုင်းနိုင်ပါတယ်။
type Env = Record<string, string | undefined>;
const PROD_DB_HOST = "prod-db.internal";
export function checkDatabaseTarget(env: Env): string {
const target = env.VERCEL_ENV ?? "development";
const url = new URL(env.DATABASE_URL ?? "postgres://localhost/notes");
if (target !== "production" && url.hostname === PROD_DB_HOST) {
throw new Error(`${target} deployment must not use the production database`);
}
return `${target} -> ${url.hostname}`;
}
for (const env of [
{ VERCEL_ENV: "production", DATABASE_URL: "postgres://app@prod-db.internal/notes" },
{ VERCEL_ENV: "preview", DATABASE_URL: "postgres://app@test-db.internal/notes" },
{ VERCEL_ENV: "preview", DATABASE_URL: "postgres://app@prod-db.internal/notes" },
]) {
try {
console.log("ok ", checkDatabaseTarget(env));
} catch (err) {
console.log("STOP ", (err as Error).message);
}
}Domain၊ CLI နဲ့ rollback
- Custom domain: project setting မှာ domain ထည့်ပြီး dashboard က ပြတဲ့ DNS record ကို domain registrar မှာ သတ်မှတ်ပါ။ HTTPS certificate ကို platform က ထုတ်ပေးပါတယ်။
- Function region: server code run မယ့် region ကို database နဲ့ နီးတဲ့ နေရာ ရွေးပါ။ Database က Singapore မှာ ရှိပြီး function က အမေရိကမှာ run ရင် query တိုင်း ကမ္ဘာကို ဖြတ်ကူးရပါတယ်။
- CLI: vercel command က terminal ကနေ preview deploy လုပ်ပြီး vercel --prod က production ကို deploy လုပ်ပါတယ်။ vercel env pull က development variable တွေကို local file ထဲ ဆွဲချပေးပါတယ်။
- Rollback: dashboard မှာ ယခင် production deployment ကို ပြန်တင်လို့ ရပါတယ် (ပြန် build မလုပ်ရ)။ Feature အမည်နဲ့ plan အလိုက် ကန့်သတ်ချက်တွေကို လက်ရှိ docs မှာ စစ်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Team တွေမှာ pull request တိုင်းမှာ preview URL ကို bot က comment အဖြစ် အလိုလို တင်ပေးပါတယ်။ Designer၊ product manager နဲ့ QA တွေက code ကို မဖတ်ဘဲ feature အစစ်ကို browser မှာ ဖွင့်ကြည့်ပြီး feedback ပေးနိုင်ပါတယ်။ E2E test တွေကိုလည်း ဒီ preview URL ပေါ်မှာ run ပြီး မအောင်ရင် merge မလုပ်ခိုင်းပါဘူး။
Preview တွေအတွက် database ကို နည်းလမ်း အမျိုးမျိုးနဲ့ စီစဉ်ကြပါတယ်: preview အားလုံး မျှသုံးတဲ့ staging database တစ်ခု၊ ဒါမှမဟုတ် branch တစ်ခုချင်းအတွက် database branch သီးသန့် ပေးနိုင်တဲ့ provider တွေကို သုံးတာပါ။ Production ဘက်မှာတော့ deploy တိုင်းပြီးရင် smoke test script ကို CI ကနေ run ပြီး error rate ကို ၁၀ မိနစ်လောက် စောင့်ကြည့်ပါတယ်။ ပြဿနာ မြင်ရင် အရင်ဆုံး rollback လုပ်ပြီးမှ အေးအေးဆေးဆေး အကြောင်းရင်း ရှာပါတယ်။
အမှားများ
အမှားများတဲ့ အချက်တွေ
- Preview deployment ကို production database နဲ့ production secret တွေ ချိတ်ထားတာ။ Review မလုပ်ရသေးတဲ့ code က user အစစ်တွေရဲ့ data ကို ပြင်ဖို့၊ ဖျက်ဖို့ ခွင့်ရသွားပါတယ်။
- Build အောင်ပြီး "Ready" ပေါ်တာနဲ့ ပြီးပြီလို့ ယူဆပြီး user flow ကို မစမ်းတာ။ Database connection string မှား၊ OAuth callback URL မှာ domain အသစ် မထည့်ရသေးတာ လို ပြဿနာတွေက build ကို မထိခိုက်ပေမယ့် login ကို ပျက်စေပါတယ်။
- Dashboard မှာ variable ပြောင်းပြီး redeploy မလုပ်တာ။ Deployment အဟောင်းက တန်ဖိုး အဟောင်းနဲ့ ဆက် run နေလို့ "ပြင်ပြီးပြီ ဒါပေမယ့် အလုပ်မလုပ်ဘူး" ဆိုပြီး အချိန်ကုန်ပါတယ်။
- Monorepo မှာ root directory ကို မသတ်မှတ်ဘဲ repository root ကနေ build လုပ်ခိုင်းတာ။ Build က Next.js app ကို ရှာမတွေ့ဘဲ fail ဖြစ်တတ်ပြီး ပိုဆိုးရင် package မှားကို build လုပ်သွားပါတယ်။
Mini challenge
ကိုယ်တိုင် လုပ်ကြည့်ရအောင်: deploy ပြီးနောက် smoke test
Deployment URL တစ်ခုကို လက်ခံပြီး အရေးကြီး route တွေရဲ့ status code နဲ့ စာသားကို စစ်တဲ့ smoke test script ကို ရေးပါ။ Check တစ်ခုခု fail ရင် rollback လုပ်ဖို့ သတိပေးပါ။ ဘယ်နေရာမှာမဆို run လို့ ရအောင် deploy ထားတဲ့ app အစား node:http နဲ့ server အတုလေး တစ်ခု သုံးပါ။
- Check တစ်ခုစီမှာ path၊ မျှော်လင့်တဲ့ status နဲ့ ရွေးချယ်စရာ contains စာသား ပါပါတယ်
- Login မဝင်ရင် /dashboard က 307 redirect ဖြစ်ရပါမယ်၊ ဒါကြောင့် fetch မှာ redirect: "manual" သုံးပါ
- Check တစ်ခုစီအတွက် PASS ဒါမှမဟုတ် FAIL စာကြောင်း တစ်ကြောင်း ထုတ်ပါ
- အဆုံးမှာ fail အရေအတွက်နဲ့ rollback လုပ်ရမလား ပြပါ
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
type Check = { path: string; status: number; contains?: string };
const CHECKS: Check[] = [
// TODO: "/", "/notes", "/dashboard" (signed out), "/api/health", a missing note
];
export async function smoke(baseUrl: string): Promise<boolean> {
// TODO: fetch each path without following redirects
// TODO: compare status and body, print PASS/FAIL per check
// TODO: print a final verdict and return true only if everything passed
return false;
}
// TODO: a stand-in server with createServer(...) where /api/health is broken,
// listen on port 0, run smoke() against it, then close the serverရလဒ်: /၊ /notes၊ /dashboard နဲ့ မရှိတဲ့ note route အတွက် PASS ပေါ်ပြီး server အတုမှာ ပျက်နေတဲ့ /api/health က "FAIL /api/health -> 500" ပေါ်ပါတယ်။ နောက်ဆုံးမှာ "1 check(s) failed: roll back" ပေါ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
new URL(check.path, baseUrl) နဲ့ URL အပြည့်ကို တည်ဆောက်ပါ။ Server ကို listen(0, ...) နဲ့ စရင် OS က လွတ်နေတဲ့ port ကို ပေးပြီး server.address() ကနေ port ကို ယူနိုင်ပါတယ်။ စစ်ပြီးရင် server.close() ကို ခေါ်မှ process က ထွက်ပါမယ်။
အဖြေ ကြည့်ရန်
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
// Run after every deploy against the deployment URL
type Check = { path: string; status: number; contains?: string };
const CHECKS: Check[] = [
{ path: "/", status: 200, contains: "Myanmar Notes" },
{ path: "/notes", status: 200 },
{ path: "/dashboard", status: 307 },
{ path: "/api/health", status: 200, contains: '"status":"ok"' },
{ path: "/notes/does-not-exist", status: 404 },
];
export async function smoke(baseUrl: string): Promise<boolean> {
let failed = 0;
for (const check of CHECKS) {
const res = await fetch(new URL(check.path, baseUrl), { redirect: "manual" });
const body = await res.text();
const okStatus = res.status === check.status;
const okBody = !check.contains || body.includes(check.contains);
if (!okStatus || !okBody) failed++;
const mark = okStatus && okBody ? "PASS" : "FAIL";
const why = okStatus && !okBody ? ` (missing "${check.contains}")` : "";
console.log(`${mark} ${check.path} -> ${res.status}${why}`);
}
const verdict = failed === 0 ? "smoke test passed" : `${failed} check(s) failed`;
console.log(failed === 0 ? verdict : `${verdict}: roll back`);
return failed === 0;
}
// Demo: a stand-in for the deployed app, so this file runs anywhere.
const fakeApp = createServer((req, res) => {
const routes: Record<string, [number, string]> = {
"/": [200, "<h1>Myanmar Notes</h1>"],
"/notes": [200, "<ul></ul>"],
"/api/health": [500, '{"status":"db down"}'],
};
if (req.url === "/dashboard") {
res.writeHead(307, { location: "/login" }).end();
return;
}
const [status, body] = routes[req.url ?? ""] ?? [404, "Not found"];
res.writeHead(status).end(body);
});
fakeApp.listen(0, async () => {
const { port } = fakeApp.address() as AddressInfo;
await smoke(`http://127.0.0.1:${port}`);
fakeApp.close();
});Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Branch push တိုင်း preview deployment၊ production branch ကို merge တာက production deployment ဖြစ်ပါတယ်
- DATABASE_URL နဲ့ secret တွေကို Production၊ Preview၊ Development အလိုက် တန်ဖိုး ကွဲကွဲ ထားပါ
- Variable ပြောင်းရင် redeploy လုပ်ပြီး deploy တိုင်းကို commit SHA နဲ့ ခြေရာခံပါ
- Build အောင်တာနဲ့ မပြီးသေးပါ၊ smoke test မအောင်ရင် ယခင် deployment ကို ပြန်တင်ပါ
Next.js — Deploying — Next.js
Next.js — Deploying — Next.js