CI/CD(Continuous Integration / Continuous Deployment)是一套自動化流程,主要目的:
好處:
簡單講,CI/CD 就像「每次你寫完程式碼,它自動幫你檢查、打包、發佈」。
Git Push / PR
↓
CI: Build + Test
↓
CD: Deploy (Firebase Hosting / Backend)
對於 React + Firebase 專案,常見流程:
常見工具:
工具 | 適合場景 |
---|---|
GitHub Actions | Github 專案,整合 GitHub Repository 最簡單 |
GitLab CI | GitLab 專案,自動化流程彈性大 |
Bitbucket Pipelines | Bitbucket 專案 |
Jenkins / CircleCI / Travis CI | 跨平台、彈性高,適合大型專案 |
這裡以 GitHub Actions 做範例示範,方便整合 Firebase Hosting + Functions。
.github/workflows
下新增 ci-cd.yml
:name: CI/CD React + Firebase
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# 1. 取得程式碼
- name: Checkout repo
uses: actions/checkout@v3
# 2. Node.js 環境
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18
# 3. 安裝套件
- name: Install dependencies
run: npm ci
# 4. 執行 lint
- name: Run ESLint
run: npm run lint
# 5. 執行測試
- name: Run tests
run: npm test
# 6. Build React
- name: Build React App
run: npm run build
# 7. Deploy Firebase
- name: Deploy to Firebase
uses: w9jds/firebase-action@v2.2.0
with:
args: deploy --only hosting,functions
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
firebase login:ci
取得 token,再設定到 GitHub Secretsdeploy --only hosting,functions
:可以指定要部署 Firebase Hosting、Functions 或其他服務