name: Lighthouse CI on: push: branches: [main] pull_request: branches: [main] jobs: lighthouse: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' cache: 'npm' - name: Install dependencies run: npm ci - name: Build application run: npm run build - name: Run Lighthouse CI run: npm run lighthouse env: LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }} - name: Upload Lighthouse results uses: actions/upload-artifact@v3 if: always() with: name: lighthouse-results path: lighthouse-reports - name: Comment PR with results if: github.event_name == 'pull_request' uses: actions/github-script@v6 with: script: | const fs = require('fs'); const path = require('path'); // 读取结果 const resultsPath = path.join(process.cwd(), 'lighthouse-reports', 'manifest.json'); if (fs.existsSync(resultsPath)) { const results = JSON.parse(fs.readFileSync(resultsPath, 'utf-8')); // 生成评论 const comment = `## 📊 Lighthouse CI Results ${results.map(r => ` ### ${r.url} - **Performance**: ${(r.summary.performance * 100).toFixed(0)}/100 - **Accessibility**: ${(r.summary.accessibility * 100).toFixed(0)}/100 - **Best Practices**: ${(r.summary['best-practices'] * 100).toFixed(0)}/100 - **SEO**: ${(r.summary.seo * 100).toFixed(0)}/100 `).join('\n')} [View Full Report](${results[0].url})`; // 发布评论 await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, body: comment }); }