Skip to content

Commit a2d1b75

Browse files
committed
add lighthouse ci
1 parent 1917b49 commit a2d1b75

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

.github/workflows/lighthouse.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Lighthouse Report
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
lighthouse:
12+
if: github.actor != 'dependabot[bot]'
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout PR code
17+
uses: actions/checkout@v4
18+
with:
19+
ref: ${{ github.event.pull_request.head.ref }}
20+
repository: ${{ github.event.pull_request.head.repo.full_name }}
21+
22+
- name: Setup Node.js with npm cache
23+
uses: actions/setup-node@v4
24+
with:
25+
node-version: '22.x'
26+
cache: 'npm'
27+
28+
- name: Install dependencies
29+
run: npm install lighthouse
30+
31+
- name: Wait for Netlify preview to be live
32+
run: |
33+
PREVIEW_URL="https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app"
34+
echo "PREVIEW_URL=$PREVIEW_URL" >> "$GITHUB_ENV"
35+
for i in {1..2}; do
36+
if curl -s --head "$PREVIEW_URL" | grep "200 OK" > /dev/null; then
37+
echo "Preview is live!"
38+
break
39+
fi
40+
echo "Waiting for Netlify to deploy... ($i/2)"
41+
sleep 10
42+
done
43+
44+
- name: Run Lighthouse audits
45+
run: |
46+
URLS=(
47+
"$PREVIEW_URL"
48+
"$PREVIEW_URL/en/blog/posts.html"
49+
"$PREVIEW_URL/en/5x/api.html"
50+
)
51+
echo "## 🚦 Lighthouse Results" > lighthouse-report.md
52+
echo "| URL | Perf | A11y | Best Practices | SEO |" >> lighthouse-report.md
53+
echo "| --- | ---- | ---- | -------------- | --- |" >> lighthouse-report.md
54+
for url in "${URLS[@]}"; do
55+
npx lighthouse "$url" \
56+
--output json \
57+
--output-path="lighthouse-report.json" \
58+
--chrome-flags="--headless"
59+
perf=$(jq '.categories | .performance.score * 100' lighthouse-report.json)
60+
a11y=$(jq '.categories | .accessibility.score * 100' lighthouse-report.json)
61+
bp=$(jq '.categories | .["best-practices"].score * 100' lighthouse-report.json)
62+
seo=$(jq '.categories | .seo.score * 100' lighthouse-report.json)
63+
stoplight() {
64+
if (( $(echo "$1 >= 90" | bc -l) )); then echo "🟢";
65+
elif (( $(echo "$1 >= 75" | bc -l) )); then echo "🟠";
66+
else echo "🔴"; fi
67+
}
68+
perf_stoplight=$(stoplight $perf)
69+
a11y_stoplight=$(stoplight $a11y)
70+
bp_stoplight=$(stoplight $bp)
71+
seo_stoplight=$(stoplight $seo)
72+
path=$(echo "$url" | sed "s|$PREVIEW_URL||")
73+
if [ -z "$path" ]; then path="/"; fi
74+
echo "| $path | $perf_stoplight $(printf "%.0f" $perf) | $a11y_stoplight $(printf "%.0f" $a11y) | $bp_stoplight $(printf "%.0f" $bp) | $seo_stoplight $(printf "%.0f" $seo) |" >> lighthouse-report.md
75+
done
76+
cat lighthouse-report.md
77+
78+
- name: Log Lighthouse report
79+
run: |
80+
cat lighthouse-report.md
81+
82+
- name: Upload Lighthouse report
83+
uses: actions/upload-artifact@v4
84+
with:
85+
name: lighthouse-report
86+
path: lighthouse-report.md

0 commit comments

Comments
 (0)