|
8 | 8 | jobs: |
9 | 9 | lighthouse: |
10 | 10 | runs-on: ubuntu-latest |
11 | | - permissions: |
12 | | - pull-requests: write |
13 | 11 | steps: |
14 | 12 | - name: Checkout |
15 | 13 | uses: actions/checkout@v4 |
|
33 | 31 | run: npm run build |
34 | 32 |
|
35 | 33 | - name: Lighthouse CI |
36 | | - id: lhci |
37 | 34 | uses: treosh/lighthouse-ci-action@v12 |
38 | 35 | with: |
39 | 36 | configPath: ./lighthouserc.json |
40 | 37 | uploadArtifacts: true |
41 | | - |
42 | | - - name: Post Lighthouse scores to PR |
43 | | - uses: actions/github-script@v7 |
44 | | - with: |
45 | | - script: | |
46 | | - const fs = require('fs'); |
47 | | - const path = require('path'); |
48 | | - const dir = '.lighthouseci'; |
49 | | - const files = fs.readdirSync(dir).filter(f => f.endsWith('.json') && !f.startsWith('manifest')); |
50 | | - const rows = []; |
51 | | - for (const f of files) { |
52 | | - const raw = JSON.parse(fs.readFileSync(path.join(dir, f), 'utf8')); |
53 | | - const r = raw.lhr || raw; |
54 | | - if (!r.categories) continue; |
55 | | - const rawUrl = r.finalDisplayedUrl || r.finalUrl || r.requestedUrl || ''; |
56 | | - const pagePath = rawUrl.startsWith('http') ? new URL(rawUrl).pathname : rawUrl || f; |
57 | | - const scores = r.categories; |
58 | | - const fmt = (cat) => { |
59 | | - if (!scores[cat]) return 'N/A'; |
60 | | - const s = Math.round(scores[cat].score * 100); |
61 | | - const icon = s >= 90 ? '🟢' : s >= 50 ? '🟠' : '🔴'; |
62 | | - return `${icon} ${s}`; |
63 | | - }; |
64 | | - rows.push(`| ${pagePath} | ${fmt('performance')} | ${fmt('accessibility')} | ${fmt('best-practices')} | ${fmt('seo')} |`); |
65 | | - } |
66 | | - const body = [ |
67 | | - '## Lighthouse Results', |
68 | | - '', |
69 | | - '| Page | Performance | Accessibility | Best Practices | SEO |', |
70 | | - '|------|------------|---------------|----------------|-----|', |
71 | | - ...rows, |
72 | | - '', |
73 | | - '_Scores: 🟢 90+ · 🟠 50-89 · 🔴 0-49_' |
74 | | - ].join('\n'); |
75 | | - const { data: comments } = await github.rest.issues.listComments({ |
76 | | - owner: context.repo.owner, |
77 | | - repo: context.repo.repo, |
78 | | - issue_number: context.issue.number, |
79 | | - }); |
80 | | - const existing = comments.find(c => c.body.includes('## Lighthouse Results')); |
81 | | - if (existing) { |
82 | | - await github.rest.issues.updateComment({ |
83 | | - owner: context.repo.owner, |
84 | | - repo: context.repo.repo, |
85 | | - comment_id: existing.id, |
86 | | - body, |
87 | | - }); |
88 | | - } else { |
89 | | - await github.rest.issues.createComment({ |
90 | | - owner: context.repo.owner, |
91 | | - repo: context.repo.repo, |
92 | | - issue_number: context.issue.number, |
93 | | - body, |
94 | | - }); |
95 | | - } |
0 commit comments