Lighthouse CI集成实际项目总结

创建项目

bash 复制代码
mkdir esim-lighthouse-ci
cd esim-lighthouse-ci/
npm init -y

安装依赖

bash 复制代码
npm install -g @lhci/cli

创建配置文件

bash 复制代码
touch lighthouserc.js

编写代码

javascript 复制代码
module.exports = {
    ci: {
      collect: {
        url: ['https://esimnum.com/home'],
        numberOfRuns: 3,
        settings: {
          chromeFlags: '--no-sandbox --disable-dev-shm-usage --headless',
          preset: 'desktop',
        },
      },
      assert: {
        assertions: {
          'categories:performance': ['error', { minScore: 0.3 }],
          'categories:accessibility': ['warn', { minScore: 0.3 }],
        },
      },
      upload: {
        target: 'temporary-public-storage',
      },
    },
  };
json 复制代码
{
  "name": "esim-lighthouse-ci",
  "version": "1.0.0",
  "description": "Lighthouse CI tests for esimnum.com",
  "main": "index.js",
  "scripts": {
    "test": "lhci autorun --config=lighthouserc.js",
    "test:ci": "LHCI_BUILD_CONTEXT__CURRENT_HASH=github-actions lhci autorun --config=lighthouserc.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs",
  "dependencies": {
    "@lhci/cli": "^0.15.1"
  }
}

本地测试

bash 复制代码
lhci autorun --config=lighthouserc.js

执行日志

html报告

关于上传报告Error解决方案

方案1:在Git仓库运行

bash 复制代码
git init
git add .
git commit -m "init"
lhci autorun --config=lighthouserc.js

方案2:绕过Git bash

bash 复制代码
LHCI_BUILD_CONTEXT__CURRENT_HASH=github-actions lhci autorun --config=lighthouserc.js

Github Actions集成

bash 复制代码
mkdir -p .github/workflows && cd .github/workflows
touch lighthouse.yml

lighthouse.yml

yaml 复制代码
name: Lighthouse CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  lighthouse:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      statuses: write
      checks: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 20

      - name: Install Lighthouse CI
        run: npm install -g @lhci/cli

      - name: Run Lighthouse Collect
        run: |
          LHCI_BUILD_CONTEXT__CURRENT_HASH=github-actions \
          lhci collect --config=lighthouserc.js || true

      - name: Run Lighthouse Upload
        env:
          LHCI_GITHUB_APP_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          LHCI_BUILD_CONTEXT__CURRENT_HASH=github-actions \
          lhci upload --config=lighthouserc.js 2>&1 | tee upload_output.txt || true

      - name: Display Report URL
        run: |
          REPORT_URL=$(grep -oP 'https://storage\.googleapis\.com/[^\s]+\.html' upload_output.txt | head -1)
          
          if [ -n "$REPORT_URL" ]; then
            echo "## 📊 Lighthouse Report" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "🔗 [View Online Report]($REPORT_URL)" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "Report includes averages from all runs" >> $GITHUB_STEP_SUMMARY
            echo "✅ Report URL: $REPORT_URL"
          else
            echo "⚠️ No report URL found"
          fi

添加定时任务和飞书通知

yaml 复制代码
name: Lighthouse CI

on:
  push:
    branches: [main]
  pull_request:
  schedule:
    # 每周三 24:00
    - cron: '0 16 * * 3'

jobs:
  lighthouse:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      statuses: write
      checks: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 20

      - name: Install Lighthouse CI
        run: npm install -g @lhci/cli

      - name: Run Lighthouse Collect
        run: |
          LHCI_BUILD_CONTEXT__CURRENT_HASH=github-actions \
          lhci collect --config=lighthouserc.js || true

      - name: Run Lighthouse Upload
        env:
          LHCI_GITHUB_APP_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          LHCI_BUILD_CONTEXT__CURRENT_HASH=github-actions \
          lhci upload --config=lighthouserc.js 2>&1 | tee upload_output.txt || true

      - name: Display Report URL
        id: report
        run: |
          REPORT_URL=$(grep -oP 'https://storage\.googleapis\.com/[^\s]+\.html' upload_output.txt | head -1)
          
          if [ -n "$REPORT_URL" ]; then
            echo "## 📊 Lighthouse Report" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "🔗 [View Online Report]($REPORT_URL)" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "Report includes averages from all runs" >> $GITHUB_STEP_SUMMARY
            echo "✅ Report URL: $REPORT_URL"
            echo "report_url=$REPORT_URL" >> $GITHUB_OUTPUT
          else
            echo "⚠️ No report URL found"
            echo "report_url=" >> $GITHUB_OUTPUT
          fi

      - name: Send Feishu Notification
        if: steps.report.outputs.report_url != ''
        continue-on-error: true
        run: |
          FEISHU_WEBHOOK_URL="${{ secrets.FEISHU_WEBHOOK_URL }}"
          REPORT_URL="${{ steps.report.outputs.report_url }}"
          
          if [ -z "$FEISHU_WEBHOOK_URL" ]; then
            echo "⚠️ FEISHU_WEBHOOK_URL not configured, skipping notification"
            exit 0
          fi
          
          # 获取北京时间
          BUILD_TIME=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S')
          
          curl -X POST "$FEISHU_WEBHOOK_URL" \
            -H 'Content-Type: application/json' \
            -d "{
              \"msg_type\": \"interactive\",
              \"card\": {
                \"header\": {
                  \"title\": {
                    \"content\": \"📊 eSIM Lighthouse CI 报告\",
                    \"tag\": \"plain_text\"
                  },
                  \"template\": \"blue\"
                },
                \"elements\": [
                  {
                    \"tag\": \"div\",
                    \"text\": {
                      \"content\": \"性能测试已完成,点击下方按钮查看详细报告\",
                      \"tag\": \"plain_text\"
                    }
                  },
                  {
                    \"tag\": \"div\",
                    \"text\": {
                      \"content\": \"⏰ **构建时间**: $BUILD_TIME\",
                      \"tag\": \"lark_md\"
                    }
                  },
                  {
                    \"tag\": \"hr\"
                  },
                  {
                    \"tag\": \"action\",
                    \"actions\": [
                      {
                        \"tag\": \"button\",
                        \"text\": {
                          \"content\": \"🔗 查看详细报告\",
                          \"tag\": \"plain_text\"
                        },
                        \"url\": \"$REPORT_URL\",
                        \"type\": \"primary\"
                      }
                    ]
                  }
                ]
              }
            }"
          
          echo "✅ Feishu notification sent"

配置飞书链接

通知结果

相关推荐
IAR Systems4 天前
松下电工借助IAR CI/CD解决方案,实现品质与效率双重飞跃
ci/cd
Cherry的跨界思维4 天前
【AI测试全栈:质量】47、Vue+Prometheus+Grafana实战:打造全方位AI监控面板开发指南
vue.js·人工智能·ci/cd·grafana·prometheus·ai测试·ai全栈
古斯塔夫歼星炮4 天前
Dify + Jenkins 实现AI应用持续集成与自动化部署
ci/cd·jenkins·dify
codingWhat4 天前
手把手系列之——前端工程化
ci/cd·devops·前端工程化
测试渣4 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 51--CI/CD 4--推送本地代码到Git远程仓库
python·学习·测试工具·ci/cd·pytest
deephub6 天前
并行多智能体系统的协调测试实战:从轨迹捕获到CI/CD的六个步骤
人工智能·ci/cd·大语言模型·aiagent
你的论文学长7 天前
文本处理的 CI/CD:用 NLP 静态分析解决查重飘红与 Format Error
人工智能·ci/cd·自然语言处理·重构·论文·学习方法
Elastic 中国社区官方博客8 天前
Agentic CI/CD:使用 Kubernetes 部署门控,结合 Elastic MCP Server
大数据·人工智能·elasticsearch·搜索引擎·ci/cd·容器·kubernetes
莫比乌斯之梦9 天前
使用 Docker 运行 Jenkins:快速搭建高效 CI/CD 环境指南
ci/cd·docker·jenkins