Skip to content

Commit 12b3d5a

Browse files
committed
Chore:Update Lighthouse Integration
Signed-off-by: Achanandhi-M <achanandhi.m@gmail.com>
1 parent e02dd9f commit 12b3d5a

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const dir = '.lighthouseci';
5+
const files = fs.readdirSync(dir).filter(file => file.endsWith('.report.json'));
6+
7+
if (files.length < 2) {
8+
console.error('❌ Not enough Lighthouse reports found.');
9+
process.exit(1);
10+
}
11+
12+
let mainReport = '';
13+
let prReport = '';
14+
15+
for (const file of files) {
16+
const json = JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
17+
const url = json.finalUrl;
18+
19+
if (url.includes('3000')) mainReport = json;
20+
else if (url.includes('3001')) prReport = json;
21+
}
22+
23+
function extract(report) {
24+
return {
25+
performance: report.categories.performance.score * 100,
26+
accessibility: report.categories.accessibility.score * 100,
27+
bestPractices: report.categories['best-practices'].score * 100,
28+
seo: report.categories.seo.score * 100,
29+
};
30+
}
31+
32+
const main = extract(mainReport);
33+
const pr = extract(prReport);
34+
35+
const md = `
36+
**🔍 Lighthouse Scores**
37+
38+
<table>
39+
<tr>
40+
<th>Metric</th>
41+
<th>⚡ PR Branch</th>
42+
<th>📦 Main Branch</th>
43+
</tr>
44+
<tr>
45+
<td>Performance</td>
46+
<td>${pr.performance}</td>
47+
<td>${main.performance}</td>
48+
</tr>
49+
<tr>
50+
<td>Accessibility</td>
51+
<td>${pr.accessibility}</td>
52+
<td>${main.accessibility}</td>
53+
</tr>
54+
<tr>
55+
<td>Best Practices</td>
56+
<td>${pr.bestPractices}</td>
57+
<td>${main.bestPractices}</td>
58+
</tr>
59+
<tr>
60+
<td>SEO</td>
61+
<td>${pr.seo}</td>
62+
<td>${main.seo}</td>
63+
</tr>
64+
</table>
65+
`;
66+
67+
68+
fs.writeFileSync('lighthouse-comment.md', md);
69+
console.log('✅ Comment written to lighthouse-comment.md');

.github/workflows/lighthouse.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Lighthouse score
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- '**'
7+
8+
jobs:
9+
lighthouse:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Set up Node.js
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: 18
18+
19+
- run: npm install -g serve
20+
21+
- name: Serve PR branch (port 3001)
22+
run: |
23+
serve . -l 3001 &
24+
sleep 5
25+
26+
- name: Checkout main branch into separate folder
27+
run: |
28+
git fetch origin main
29+
git worktree add main-branch origin/main
30+
31+
- name: Serve Main branch (port 3000)
32+
run: |
33+
cd main-branch
34+
serve . -l 3000 &
35+
cd ..
36+
sleep 5
37+
38+
- name: Run Lighthouse audits
39+
uses: treosh/lighthouse-ci-action@v12
40+
with:
41+
urls: |
42+
http://localhost:3000/
43+
http://localhost:3001/
44+
uploadArtifacts: true
45+
temporaryPublicStorage: true
46+
47+
- name: Parse reports and generate comment
48+
run: node .github/scripts/lighthouse-report.js
49+
50+
- name: Comment on PR with Lighthouse scores
51+
uses: peter-evans/create-or-update-comment@v4
52+
with:
53+
token: ${{ secrets.GITHUB_TOKEN }}
54+
issue-number: ${{ github.event.pull_request.number }}
55+
body-path: lighthouse-comment.md

0 commit comments

Comments
 (0)