name: 代码质量门禁 description: 在每次部署前强制执行自动化质量检查。通过五阶段质量门禁系统(预提交、PR检查、预览、端到端测试、生产)防止生产故障。在代码更改、部署、PR审核、构建失败时激活。
代码质量门禁
这个技能通过五阶段质量门禁系统防止生产故障。
五个质量门禁
- 预提交(本地): TypeScript、Lint、格式化 - 在错误时阻止提交
- PR检查(GitHub Actions): 单元测试、构建 - 在错误时阻止合并
- 预览部署: Vercel/Netlify 预览URL用于视觉审查
- 端到端测试: 使用Playwright对预览进行测试,Lighthouse性能审计
- 生产部署: 仅当所有门禁通过时
关键规则
- 关键: 在GitHub Actions中,永远不要为TypeScript检查使用
continue-on-error: true! - Husky设置:
npm install -D husky lint-staged && npx husky init - 回滚:
vercel rollback
示例:预提交钩子 (.husky/pre-commit)
#!/bin/sh
npx lint-staged
npx tsc --noEmit
示例:lint-staged.config.js
module.exports = {
'*.{ts,tsx}': ['eslint --fix', 'prettier --write'],
'*.{json,md}': ['prettier --write'],
};
示例:GitHub Actions工作流
name: Quality Gate
on: [push, pull_request]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
# Gate 1: TypeScript (NEVER skip!)
- name: TypeScript Check
run: npx tsc --noEmit
# Gate 2: Linting
- name: ESLint
run: npm run lint
# Gate 3: Unit Tests
- name: Unit Tests
run: npm run test
# Gate 4: Build
- name: Build
run: npm run build
何时激活
- 影响生产代码的代码更改
- 部署请求
- PR审核
- 构建失败(用于调试)
实际影响
在 fabrikIQ.com,这个质量门禁系统捕获了:
- 2个TypeScript错误,这些错误会导致运行时崩溃
- 1个部署中缺失的环境变量
- 3个性能回归,在它们影响生产之前被检测到