前端工程化 词条目录
前端Wiki/词条/CI/CD

CI/CD

定义持续集成与持续交付/部署:代码提交后自动跑检查、测试、构建、发布

CI(持续集成):每次 push/PR 自动触发「安装依赖 → lint → 类型检查 → 单测 → 构建」流水线,问题在合并前就暴露,保证主干始终健康。CD(持续交付/部署):构建产物自动部署到预发/生产,甚至灰度放量。CI 是「质量门禁」,CD 是「发布自动化」,二者合成一条流水线。

常见载体:GitHub Actions、GitLab CI、Jenkins。典型前端 Action:install(缓存 node_modules)→ lint & test(跑在容器 matrix)→ build → 部署到静态托管(Vercel/对象存储/服务器)。关键实践:失败必须阻断、产物带 commit hash、发布可回滚(保留上一版随时切回)。

JavaScript
# .github/workflows/ci.yml
name: CI
on:
  push: { branches: [main] }
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'                 # ★ 缓存依赖
      - run: npm ci                     # 锁文件严格安装
      - run: npm run lint               # 质量门禁 1:规范
      - run: npm run test               # 质量门禁 2:单测
      - run: npm run build              # 构建
      # CD:产物上传 → 触发部署
      - uses: actions/upload-artifact@v4
        with: { name: dist, path: dist }
      # - name: deploy
      #   run: npx wrangler pages deploy dist   # 例:静态站点部署
⚠ 常见误区
🔗 相关词条PM2 部署脚手架搭建Git Hooks 与规范
← 返回来源章节CI/CD 流水线