iT邦幫忙

2026 iThome 鐵人賽

DAY 25
0
Vibe Coding

Vibe Coding的30天,自然語言與AI共舞,從Prompt到高品質原型落地系列 第 25

Day 25|Vercel自動化CI/CD部署與Neon PostgreSQL上雲

  • 分享至 

  • xImage
  •  

在Day24中,我們完成了Next.js效能優化、動態OG社交卡片與PWA離線支援,讓VibePulse在體驗與效能上達到生產等級。

今天(Day25),我們將跨出從本地開發(Local)走向全世界使用(Production)最關鍵的一步——Vercel自動化CI/CD部署與NeonServerlessPostgreSQL雲端資料庫託管

今天我們將實作:

NeonPostgreSQL雲端資料庫建置:建立Serverless雲端資料庫並設定連線池(ConnectionPooling)。

Prisma雲端Migration策略:配置prismamigratedeploy與PrismaClient生成流程。

Vercel自動化CI/CD部署:串接GitHubRepo、設定雲端環境變數並完成部署!

實戰步驟1:建立NeonPostgreSQL雲端資料庫
Neon是專為Serverless架構設計的託管PostgreSQL,支援自動擴充與ConnectionPooling,非常適合搭配Vercel/Next.js使用。

前往Neon官網註冊並建立專案vibepulse-prod。

建立完成後,在Dashboard的ConnectionDetails中,選取Prisma格式。

Neon會提供兩組ConnectionStrings:

PooledConnectionString(用於API/EdgeFunction執行短連線queries):
postgres://[user]:[password]@[ep-name]-pooler.[region].aws.neon.tech/neondb?sslmode=require&pgbouncer=true

DirectConnectionString(用於執行PrismaMigrationDDL變更):
postgres://[user]:[password]@[ep-name].[region].aws.neon.tech/neondb?sslmode=require

實戰步驟2:更新PrismaSchema以支援連線池
為避免ServerlessFunctions在高峰期頻繁建立資料庫連線導致Exhaustion,我們需在prisma/schema.prisma配置directUrl:

// prisma/schema.prisma

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")        // 帶有 pgbouncer=true 的 Pooled 連線
  directUrl = env("DIRECT_URL")          // 用於 Migration 的 Direct 連線
}

generator client {
  provider = "prisma-client-js"
}

// ...其餘 Skill, PracticeLog, Note 模型保持不變

在地端.env進行測試更新:

# .env (Local/Production 測試)
DATABASE_URL="postgres://user:pass@ep-pooler.neon.tech/neondb?sslmode=require&pgbouncer=true"
DIRECT_URL="postgres://user:pass@ep.neon.tech/neondb?sslmode=require"

實戰步驟3:調整package.json部署腳本
在Vercel每次進行Build時,我們需要自動執行Prisma的結構同步與Client產生。更新package.json:

{
  "name": "vibepulse",
  "version": "1.0.0",
  "scripts": {
    "dev": "next dev",
    "build": "prisma generate && prisma migrate deploy && next build",
    "start": "next start",
    "lint": "next lint"
  }
}

說明:

prismagenerate:確保生成最新版PrismaClient程式碼。

prismamigratedeploy:自動套用未執行的Migration變更至Neon雲端資料庫(使用DIRECT_URL)。

實戰步驟4:Vercel專案建立與環境變數設置
將最新程式碼Push至GitHub儲存庫:

git add .
git commit -m "feat: prepare production deployment with Neon and Vercel"
git push origin main

登入VercelDashboard,點擊"AddNewProject"並匯入GitHub的vibepulseRepo。

在EnvironmentVariables區塊新增雲端環境變數:
DATABASE_URL Neon提供帶有pgbouncer=true的Pooled連線字串
DIRECT_URL Neon提供直連(Direct)的連線字串
NEXT_PUBLIC_APP_URL 部署後的正式網域(https://vibepulse.vercel.app)
點擊"Deploy"!Vercel即刻開始執行預備、編譯、資料庫Migration與正式上線。

今天我們成功完成VibePulse雲端正式上線:Serverless雲端資料庫:整合NeonPostgreSQL,並透過ConnectionPooling解決Serverless連線瓶頸。Prisma雙軌連線:配置DATABASE_URL(Pooled)與DIRECT_URL(Direct),確保API高速查詢與Migration穩定執行。CI/CD自動化流程:完成Vercel與GitHub的雙向綁定,實現Push即自動部署上線的流暢開發體驗!明日預告


上一篇
Day 24|Next.js效能優化與PWA支援:Lighthouse滿分挑戰
下一篇
Day 26|多語系國際化(i18n)next-intl整合與語系切換
系列文
Vibe Coding的30天,自然語言與AI共舞,從Prompt到高品質原型落地28
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言