在 CI 中跑 Lighthouse CI 并阻断回归

解读

面试官想知道三件事:

  1. 你是否把性能、可访问性、SEO 等质量指标当成“功能”一样纳入版本控制;
  2. 能否用国内可落地的 CI 服务(GitLab-CI、Jenkins、GitHub Actions 私有化、阿里 Yunxiao、腾讯 CODING、字节 FlowCI 等)把 Lighthouse CI 跑通;
  3. 当得分低于基线时,能否阻断合并请求并给出可回溯的报告,而不是“跑个参考”。

知识点

  • Lighthouse CI(LHCI) 官方 CLI 与 Server 架构
  • 性能预算(Performance Budget) 与 assertions 语法
  • CI 环境无头 Chrome 启动参数 --no-sandbox --disable-dev-shm-usage
  • 国内镜像源(npmmirror、Chrome for Testing 镜像)加速依赖
  • 产物存储:把 .lighthouseci 报告上传至企业 OSS、COS、MinIO 或 CI 内置制品库
  • MR/PR 检查:GitLab only: merge_requests、GitHub pull_request 事件、Jenkins gitlabMergeRequestIid 变量
  • 阻断策略:非零 exit code + CI 状态 API;或调用企业微信/飞书机器人发卡片消息并@责任人
  • 基线漂移:用 .lighthouserc.js 的 assert.assertions 对象锁定 FCP、LCP、TTI、CLS、SpeedIndex 等阈值
  • 渐进式迁移:先 warn 后 error,避免老项目一次性全失败
  • 并行优化:把 Lighthouse 跑在独立 Job,缓存 node_modules 与 .lighthouseci,不阻塞单元测试

答案

以 GitLab-CI 为例,给出可直接落地的 .gitlab-ci.yml 片段,并解释每一步如何“阻断回归”。

# .gitlab-ci.yml
variables:
  LH_CHROME_PATH: "/usr/bin/chromium-browser"
  NPM_CONFIG_REGISTRY: "https://registry.npmmirror.com"

stages:
  - build
  - lighthouse

# 1. 先构建出真正要测的静态产物
build:
  stage: build
  image: node:18-alpine
  script:
    - npm ci --prefer-offline
    - npm run build
  artifacts:
    paths:
      - dist
    expire_in: 1 day

# 2. 独立 Job 跑 Lighthouse CI
lighthouse:
  stage: lighthouse
  image: cypress/browsers:node18.12.0-chrome107
  needs: [build]
  only:
    - merge_requests
  before_script:
    # 国内加速 Chrome for Testing
    - npm install -g @lhci/cli@0.12.x --registry=$NPM_CONFIG_REGISTRY
  script:
    # 启动静态服务器,指向刚才的产物
    - npx serve dist -l 8080 &
    - sleep 5
    # 运行 Lighthouse CI 并断言
    - lhci autorun --config=lighthouserc.json
  allow_failure: false   # 关键:MR 必须绿灯
  artifacts:
    when: always
    paths:
      - .lighthouseci
    reports:
      junit: .lighthouseci/lhr-*.xml   # GitLab 可解析
    expire_in: 7 days

对应的 lighthouserc.json 示例,锁定核心指标:

{
  "ci": {
    "collect": {
      "url": ["http://localhost:8080/"],
      "numberOfRuns": 3,
      "startServerCommand": ""
    },
    "assert": {
      "preset": "lighthouse:no-pwa",
      "assertions": {
        "first-contentful-paint": ["error", {"maxNumericValue": 1800}],
        "largest-contentful-paint": ["error", {"maxNumericValue": 2500}],
        "cumulative-layout-shift": ["error", {"maxNumericValue": 0.1}],
        "categories:performance": ["error", {"minScore": 0.9}],
        "categories:accessibility": ["error", {"minScore": 0.95}]
      }
    },
    "upload": {
      "target": "filesystem",
      "outputDir": ".lighthouseci"
    }
  }
}

解释阻断逻辑:

  1. allow_failure: false 保证一旦 LHCI exit code ≠ 0,GitLab 会把 MR 标红,禁止合并;
  2. 指标低于 lighthouserc.json 中定义的阈值时,LHCI 自动返回非零 exit code;
  3. 报告以 artifacts 形式留存,可在 GitLab UI 直接下载 trace,方便回溯;
  4. 若公司用 Jenkins,可在 stage("lighthouse") 里加 gitlabCommitStatus("lighthouse") 把状态写回 MR,实现同样阻断。

拓展思考

  1. 多环境对比:在 CI 里同时跑“预览域名”与“生产基线域名”,用 Lighthouse CI 的 compareUrl 模式,直接给出“回归差值”而非绝对分值,减少环境差异带来的误杀。
  2. 分布式跑分:把 Lighthouse 任务拆成独立 Kubernetes Job,利用集群并行跑 5 组采样,缩短 MR 等待时间;结果统一上报到自建 LHCI Server(PostgreSQL 存储),实现跨版本趋势图。
  3. 自定义审计:公司业务强依赖首屏广告渲染,可写自定义 Lighthouse Gatherer 与 Audit,把“广告可见时间”纳入断言,真正做到业务导向的阻断。
  4. 渐进式降级:对老项目设置 warn 级别,低于阈值只评论提醒,不阻断;新模块使用 error 级别,实现“老代码老办法,新代码高标准”。
  5. 合规留痕:把 .lighthouseci/lhr-*.json 上传至国内合规对象存储,保存 6 个月,满足审计要求;同时利用飞书机器人把得分卡片推送到前端技术群,形成绩效可视化闭环。