簡介
原始碼的組織方式決定了開發人員的經驗、CI/CD 複雜性和團隊協作。本文對 Mono-Repo 與 Multi-Repo 進行了比較,並提供瞭如何選擇正確的指南。

1. 多倉庫
1.1 模型
github.com/company/
├── product-service/ (repo riêng)
├── order-service/ (repo riêng)
├── cart-service/ (repo riêng)
├── product-mfe/ (repo riêng)
├── cart-mfe/ (repo riêng)
├── shell-app/ (repo riêng)
├── shared-ui/ (repo riêng, npm package)
└── shared-types/ (repo riêng, npm package)
1.2 優點和缺點
| 優勢 | 缺點 |
|---|---|
| 所有權清晰 | 共享程式碼難以管理(npmpublish) |
| 獨立CI/CD | 複雜的跨儲存庫變更 |
| 較小的儲存庫 = 快速克隆 | 依賴版本漂移 |
| 細粒度的存取控制 | 一致性很難執行 |
2. Mono-Repo
2.1 模型
github.com/company/platform/
├── apps/
│ ├── shell-app/
│ ├── product-mfe/
│ ├── cart-mfe/
│ └── order-mfe/
├── services/
│ ├── product-service/
│ ├── order-service/
│ └── cart-service/
├── packages/
│ ├── shared-ui/
│ ├── shared-types/
│ └── eslint-config/
├── turbo.json / nx.json
└── package.json (workspace root)
2.2 優點和缺點
| 優勢 | 缺點 |
|---|---|
| 輕鬆共享代碼(內部包) | 大型倉庫 → 緩慢克隆 |
| 原子跨包更改 | CI 複雜度(影響檢測) |
| 一致的工具與配置 | 粗粒度存取控制 |
| 重構很容易 | 建置時間長(需要快取) |
3.決策矩陣
| 因素 | 多倉庫 | Mono-Repo |
|---|---|---|
| 團隊規模 | 50 多名開發人員,許多團隊 | < 50 名開發人員,幾個團隊 |
| 共享代碼 | 較少共享 | 許多共享包 |
| 部署獨立性 | ✅ 天然 | ✅ 受影響檢測 |
| 跨領域變化 | ❌ 多個 PR | ✅ 單一公關 |
| CI/CD | 每個儲存庫都很簡單 | 複雜但強大 |
| 入職 | 簡單(1 項服務/儲存庫) | 中型(大型回購) |
4. Mono-Repo 工具
4.1 渦輪雷波
// turbo.json
{
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", ".next/**"]
},
"test": {
"dependsOn": ["build"]
},
"lint": {}
}
}
# Build chỉ affected packages
turbo build --filter=...[origin/main]
# Build product-mfe và dependencies
turbo build --filter=product-mfe...
4.2 尼克斯
# Affected commands (chỉ build/test code thay đổi)
nx affected --target=build --base=main
nx affected --target=test --base=main
# Dependency graph
nx graph
4.3 主要特點
| 特點 | 渦輪雷波 | NX |
|---|---|---|
| 受影響的偵測 | ✅ 基於 Git | ✅ 依賴關係圖 |
| 遠端快取 | ✅ 維塞爾 | ✅ Nx 雲端 |
| 任務編排 | ✅ 管道 | ✅ 任務圖 |
| 代碼產生 | ❌ | ✅ 發電機 |
| 插件生態系 | 有限公司 | 豐富(React、Node 等) |
5. 程式碼所有權(CODEOWNERS)
# .github/CODEOWNERS
apps/product-mfe/ @team-product
apps/cart-mfe/ @team-cart
services/product-*/ @team-product
services/order-*/ @team-order
packages/shared-ui/ @team-platform
packages/shared-*/ @team-platform
turbo.json @team-platform
→ PR 根據更改的文件自動分配審閱者。
6. 建議
E-Commerce Platform (series này):
Mono-Repo (Turborepo):
├── apps/ (Shell + MFEs)
├── services/ (Microservices)
├── packages/ (shared-ui, shared-types, eslint-config)
└── infra/ (Terraform, K8s manifests)
Lý do:
- Nhiều shared code (UI, types, configs)
- Cross-cutting changes thường xuyên
- Lợi ích từ affected detection & remote caching
- Team size < 50 devs
總結
- Multi-Repo:所有權清晰,CI簡單,但共享程式碼很困難
- Mono-Repo:易於共享程式碼,原子更改,需要工具(Turborepo/Nx)
- 使用Turborepo/Nx進行受影響的檢測+遠端緩存
- 程式碼擁有者 用於單一儲存庫中的程式碼所有權
下一篇文章: 第 24 課:微服務和微前端的 CI/CD 管道