此乃真实生产式自动发布与自动部署流程以 GitHub Actions 之用。此乃团队用以持守之设也。main安且部署自若。
吾辈将之筑于三部分:
- 自创拉取请求(自特性分支→主干)
- 试于 pull requests 运 CI 检查
- 🚀 合并至主分支时自动部署
🧠 0. 所建之事
```plaintext id="flow1"
特性分支推送
↓
自动创建 PR (GitHub Action)
↓
CI 运行 (测试、代码检查)
↓
PR 合并至主分支
↓
自動部署於產製
---
# 🤖 1. Auto Create Pull Request Workflow
This automatically creates a PR when you push a feature branch.
## 📁 `.github/workflows/auto-pr.yml`
```yaml id="pr1"
name: Auto Create Pull Request
on:
push:
branches-ignore:
- main
jobs:
create-pr:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Create Pull Request
uses: repo-sync/pull-request@v2
with:
destination_branch: main
github_token: ${{ secrets.GITHUB_TOKEN }}
pr_title: "Auto PR: ${{ github.ref_name }}"
pr_body: |
## 🤖 Auto-generated PR
Branch: `${{ github.ref_name }}`
Please review changes before merging.
🧠 此舉何為:
- 推送至
feature/login - GitHub Actions觸發
- 自動開啟PR →
feature/login → main
🧪 2. CI Pipeline(於PR上運行)
是故唯潔淨之碼得合流.
📁.github/workflows/ci.yml
```yaml id="ci1"
名:检验之职
于:
拉取请愿:
分支:[主干]
职务:
测试:
运行于:ubuntu最新
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm install
- name: Run lint
run: npm run lint
- name: Run tests
run: npm test
---
## 🧠 What this does:
Before merge:
* Runs lint checks
* Runs tests
* Blocks bad code from merging
---
# 🚀 3. Auto Deploy on Merge to Main
This deploys your app when PR is merged.
## 📁 `.github/workflows/deploy.yml`
### Example: Deploy React + Node (Vercel + Render)
```yaml id="deploy1"
name: Auto Deploy
on:
push:
branches: [ main ]
jobs:
deploy-frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install frontend
working-directory: client
run: npm install
- name: Build frontend
working-directory: client
run: npm run build
- name: Deploy to Vercel
run: npx vercel --prod --token=${{ secrets.VERCEL_TOKEN }}
deploy-backend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install backend
working-directory: server
run: npm install
- name: Deploy backend (Render webhook)
run: curl -X POST ${{ secrets.RENDER_DEPLOY_HOOK }}
🔐 四、GitHub密钥配置
前往:
👉 仓库 → 设置 → 密钥 → 操作
添加:
```plaintext id="sec1"
VERCEL_TOKEN=your_vercel_token
RENDER_DEPLOY_HOOK=https://api.render.com/deploy/xxx
---
# 🧭 5. Full Workflow in action
## Developer flow:
```plaintext id="flow2"
git checkout -b feature-login
git push origin feature-login
乃 GitHub 自行:
- 🤖 提出拉取请求
- 🧪 运行 CI 检查
- 👀 等待评审(可选项)
- 🔀 合并至主干
- 🚀 自动部署前后端
🔥 6. 升级(实公司所增)
🟢 添加审批规则(推荐)
于 GitHub:
- 需1–2人评阅
- 需CI通过方得合并
护枝之设
设置 → 分支 →main
启用:
- 需先获PR方可合并
- 需检视其状
- 阻直推
增通知(Slack/Discord)
```yaml id="通知1" ```
- 名:告 Slack 行:| curl -X POST -H 'Content-type: application/json' \ --data '{"text":"新部署于生产 🚀"}' \ ${{ secrets.SLACK_WEBHOOK }} ```
🟣 添版本标记
```bash id="tag1"
git tag v1.0.0
git push origin v1.0.0
---
# ⚠️ Common mistakes
### ❌ Auto-deploy without tests
→ leads to broken production
### ❌ No branch protection
→ anyone can push to main
### ❌ Missing secrets
→ deployment fails silently
---
# 🧠 Final Architecture (Pro level)
```plaintext id="final1"
Feature Branch
↓
Auto PR Created
↓
CI (tests + lint)
↓
Review + Approval
↓
Merge to main
↓
CD Pipeline
↓
Frontend deploy (Vercel)
Backend deploy (Render/AWS)
↓
Slack/Discord notification












