[T.10] 团队项目:CI/CD实践
| 项目 | 内容 |
|---|---|
| 这个作业属于哪个课程 | 软件工程 |
| 这个作业的要求在哪里 | [T.10]CI/CD实践 |
| 我在这个课程的目标是 | 掌握软件工程的核心理论,协作完成软件项目开发 |
| 这个作业在哪个具体方面帮助我实现目标 | 完成CI/CD实践,规范代码 |
一、选项及选择的理由
我们选择了方案 A:GitHub Actions,并完成任务三:代码/提交/PR 规范检查
选择理由:
- 项目代码已托管在 GitHub 上,使用 GitHub Actions 无需额外配置
- 规范检查能统一团队协作标准,减少 Code Review 中的低级问题,提升代码可维护性
二、配置文件
我们在仓库根目录创建了 .github/workflows/deploy.yml
name: Mind Island CI/CD Pipeline
on:
push:
branches: [ "main", "develop" ] # 严格遵循图片:push 触发范围
pull_request:
branches: [ "main" ] # 严格遵循图片:PR 到 main 触发
types: [opened, edited, synchronize, reopened]
jobs:
# 执行动作 1 & 2: 代码风格检查 (Lint) 与 格式检查 (Format-check)
frontend-quality:
name: Frontend Lint & Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: frontend/package-lock.json
- name: Install dependencies
run: npm ci
working-directory: frontend
- name: Lint frontend
run: npm run lint:check # 对应执行动作: lint
working-directory: frontend
- name: Check formatting
run: npm run format:check # 对应执行动作: format-check
working-directory: frontend
# 执行动作 3: 提交信息格式检查 (commitlint)
commit-message-check:
name: Commit Message Check
if: github.event_name == 'pull_request' # 理由:避免不规范 PR 进入 main
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate commit messages
uses: wagoid/commitlint-github-action@v6
with:
configFile: commitlint.config.cjs
# 执行动作 4: PR 标题/内容检查 (check-pr)
pr-metadata-check:
name: PR Metadata Check
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Validate PR title (Conventional Commits)
uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Validate PR description
uses: actions/github-script@v7
with:
script: |
const body = context.payload.pull_request?.body || ''
const minLength = 20
if (body.trim().length < minLength) {
core.setFailed(`PR 描述过短(当前${body.trim().length}字),请至少填写 20 个字符说明变更内容。`)
}
# 后端校验(保留你原有的 Docker 检查逻辑)
backend-check:
name: Backend Validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Docker Build Check
run: docker build ./backend -t mind-island-backend:test
# 自动化部署 (仅在 push 到 main 且质量检查通过后执行)
deploy:
name: Deploy to Aliyun
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [frontend-quality, backend-check] # 确保部署前通过了代码质量检查
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Build and Upload Frontend
run: |
cd frontend
npm ci
npm run build
- name: Copy to Server via SCP
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.ALIYUN_SERVER_IP }}
username: root
key: ${{ secrets.ALIYUN_SSH_KEY }}
source: "frontend/dist/*"
target: "/var/www/mind-island/"
strip_components: 2
- name: Restart Backend via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.ALIYUN_SERVER_IP }}
username: root
key: ${{ secrets.ALIYUN_SSH_KEY }}
script: |
cd ${{ secrets.REMOTE_TARGET }}
git pull origin main
cd backend
if [ ! -d "venv" ]; then python3 -m venv venv; fi
source venv/bin/activate
pip install -r requirements.txt
pkill gunicorn || true
gunicorn -w 2 -b 127.0.0.1:5000 main:app --daemon
三、实现方式及选择理由
分支范围:main、develop
触发条件:push 、pull_request
执行动作:
lint:check(ESLint)format:check(Prettier)commitlint- PR 标题描述检查
- Docker 构建测试
- 部署(构建+SCP+SSH)
完整覆盖代码风格、commit、PR 规范,并增加了后端验证和自动部署,形成生产可用的 CI/CD 流水线。
四、CI/CD 触发结果展示


浙公网安备 33010602011771号