Skip to content

Commit 87bd3a6

Browse files
committed
add lighthouse ci
1 parent 1917b49 commit 87bd3a6

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

.github/workflows/lighthouse.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Lighthouse Audit
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
7+
jobs:
8+
lighthouse:
9+
if: github.actor != 'dependabot[bot]'
10+
runs-on: ubuntu-latest
11+
env:
12+
PREVIEW_URL: "https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app"
13+
strategy:
14+
matrix:
15+
url: [
16+
"https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app",
17+
"https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app/en/blog/posts.html",
18+
"https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app/en/support/",
19+
"https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app/en/resources/glossary.html",
20+
"https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app/en/5x/api.html"
21+
]
22+
steps:
23+
- name: ⬇️ Checkout repo
24+
uses: actions/checkout@v4
25+
26+
- name: ⏳ Wait for Netlify preview to be live
27+
run: |
28+
echo "Waiting for Netlify preview to be live..."
29+
for i in {1..3}; do
30+
if curl -s --head "${{ env.PREVIEW_URL }}" | grep "200 OK" > /dev/null; then
31+
echo "✅ Preview is live!"
32+
break
33+
fi
34+
echo "Waiting for Netlify to deploy... ($i/3)"
35+
sleep 10
36+
done
37+
38+
- name: 🛠️ Install Lighthouse CLI
39+
run: |
40+
npm install -g lighthouse
41+
42+
- name: 🕵️ Run Lighthouse and Save Reports
43+
run: |
44+
mkdir -p lighthouse-report
45+
46+
# Sanitize URL into filename-friendly slug
47+
SANITIZED_NAME=$(echo "${{ matrix.url }}" | sed 's|https\?://||g; s|/|_|g')
48+
echo "SANITIZED_NAME=$SANITIZED_NAME" >> $GITHUB_ENV
49+
50+
lighthouse "${{ matrix.url }}" \
51+
--output json \
52+
--output html \
53+
--output-path="./lighthouse-report/${SANITIZED_NAME}" \
54+
--chrome-flags="--headless"
55+
56+
- name: ⬆️ Upload Lighthouse report
57+
uses: actions/upload-artifact@v4
58+
with:
59+
name: lighthouse-report-${{ env.SANITIZED_NAME }}
60+
path: lighthouse-report/
61+
62+
aggregate:
63+
runs-on: ubuntu-latest
64+
needs: lighthouse
65+
steps:
66+
- name: ⬇️ Download Lighthouse reports
67+
uses: actions/download-artifact@v4
68+
with:
69+
path: lighthouse-reports/
70+
pattern: lighthouse-report-*
71+
72+
- name: 📊 Create comment with Lighthouse scores
73+
uses: actions/github-script@v7
74+
with:
75+
script: |
76+
const fs = require('fs');
77+
const path = require('path');
78+
const dir = 'lighthouse-reports/';
79+
let comment = `### 🚦 Lighthouse Scores for Preview [Link](${process.env.PREVIEW_URL})\n`;
80+
81+
fs.readdirSync(dir).forEach(folder => {
82+
const files = fs.readdirSync(path.join(dir, folder));
83+
files.forEach(file => {
84+
if (file.endsWith('.json')) {
85+
const fullPath = path.join(dir, folder, file);
86+
const json = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
87+
const scores = {
88+
performance: json.categories.performance.score * 100,
89+
accessibility: json.categories.accessibility.score * 100,
90+
bestPractices: json.categories['best-practices'].score * 100,
91+
seo: json.categories.seo.score * 100,
92+
};
93+
94+
comment += `
95+
#### [${json.finalUrl}](${json.finalUrl})
96+
| Category | Score |
97+
|------------------|-------|
98+
| ⚡ Performance | ${scores.performance.toFixed(0)} |
99+
| ♿ Accessibility | ${scores.accessibility.toFixed(0)} |
100+
| ✅ Best Practices | ${scores.bestPractices.toFixed(0)} |
101+
| 🔍 SEO | ${scores.seo.toFixed(0)} |
102+
`;
103+
}
104+
});
105+
});
106+
107+
github.rest.issues.createComment({
108+
issue_number: context.issue.number,
109+
owner: context.repo.owner,
110+
repo: context.repo.repo,
111+
body: comment
112+
});

0 commit comments

Comments
 (0)