Skip to content

Commit 963c7a2

Browse files
feat: lighthouse report on frontend PRs (#1203)
* lighthouse check on pr
1 parent 40a2f19 commit 963c7a2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.github/lighthouserc.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let previewBaseUrl = '';
2+
try {
3+
const previewUrlBase64 = process.env.LHCI_PREVIEW_URL_BASE64;
4+
console.log("environemtnt variable LHCI_PREVIEW_URL:", previewUrlBase64);
5+
if (previewUrlBase64) {
6+
const decodedUrl = Buffer.from(previewUrlBase64, 'base64').toString('utf-8');
7+
console.log("Decoded preview URL:", decodedUrl);
8+
previewBaseUrl = decodedUrl;
9+
} else {
10+
console.error("LHCI_PREVIEW_URL_BASE64 environment variable is not set.");
11+
}
12+
} catch (error) {
13+
console.error("Error decoding LHCI_PREVIEW_URL_BASE64:", error);
14+
}
15+
16+
module.exports = {
17+
ci: {
18+
collect: {
19+
url: [
20+
`${previewBaseUrl}/`,
21+
`${previewBaseUrl}/feeds`,
22+
`${previewBaseUrl}/feeds/gtfs/mdb-2126`,
23+
`${previewBaseUrl}/feeds/gtfs_rt/mdb-2585`,
24+
`${previewBaseUrl}/gbfs/gbfs-flamingo_porirua`
25+
],
26+
numberOfRuns: 1, // 1 to speed up the CI process but can be increased for more reliable results
27+
settings: {
28+
formFactor: 'desktop',
29+
throttlingMethod: 'provided',
30+
skipAudits: ['robots-txt', 'is-crawlable'],
31+
screenEmulation: {
32+
mobile: false,
33+
width: 1350,
34+
height: 940,
35+
deviceScaleRatio: 1,
36+
disabled: false
37+
}
38+
}
39+
},
40+
upload: {
41+
target: 'temporary-public-storage'
42+
}
43+
}
44+
};

.github/workflows/web-app-deployer.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,59 @@ jobs:
321321
HOSTING_EXPIRATION_DATE=$(npx firebase hosting:channel:list | grep ${{ inputs.PREVIEW_HOST_NAME }} | awk '{printf "%s %s\n",$9,$10;}')
322322
echo "hosting_url=$HOSTING_URL" >> "$GITHUB_OUTPUT"
323323
echo "hosting_expiration_date=$HOSTING_EXPIRATION_DATE" >> "$GITHUB_OUTPUT"
324+
325+
- name: Lighthouse Check
326+
id: lighthouse-check
327+
if: ${{ inputs.PREVIEW_DEPLOYMENT }}
328+
uses: treosh/lighthouse-ci-action@v12
329+
# Runs on: Homepage, Search page, GTFS page, GTFS-RT page, and GBFS page
330+
with:
331+
configPath: ./.github/lighthouserc.js
332+
temporaryPublicStorage: true
333+
env:
334+
LHCI_PREVIEW_URL_BASE64: ${{ steps.deploy-preview.outputs.hosting_url }}
335+
336+
337+
- name: Format lighthouse score
338+
id: format_lighthouse_score
339+
if: ${{ inputs.PREVIEW_DEPLOYMENT }}
340+
uses: actions/github-script@v3
341+
with:
342+
github-token: ${{secrets.GITHUB_TOKEN}}
343+
script: |
344+
const results = ${{ steps.lighthouse-check.outputs.manifest }}
345+
const links = ${{ steps.lighthouse-check.outputs.links }}
346+
let comment = []
347+
results.forEach((resultData, index) => {
348+
const result = resultData.summary;
349+
350+
const formatResult = (res) => Math.round((res * 100))
351+
Object.keys(result).forEach(key => result[key] = formatResult(result[key]))
352+
353+
const score = res => res >= 90 ? '🟢' : res >= 50 ? '🟠' : '🔴'
354+
const link = Object.keys(links)[index] ?? 'Unknown URL';
355+
const linkUrl = links[link] ?? '#';
356+
comment = comment.concat(...[
357+
`*Lighthouse ran on ${link} * (Desktop)`,
358+
`⚡️ HTML Report [Lighthouse report](${linkUrl}) for the changes in this PR:`,
359+
'| Performance | Accessibility | Best Practices | SEO |',
360+
'| --- | --- | --- | --- |',
361+
`| ${score(result.performance)} ${result.performance} | ${score(result.accessibility)} ${result.accessibility} | ${score(result['best-practices'])} ${result['best-practices']} | ${score(result.seo)} ${result.seo} |`,
362+
' ',
363+
' ',
364+
])
365+
})
366+
const finalComment = comment.join('\n')
367+
core.setOutput("comment", finalComment);
368+
369+
- name: Add lighthouse comment to PR
370+
id: comment_to_pr
371+
if: ${{ inputs.PREVIEW_DEPLOYMENT }}
372+
uses: marocchino/sticky-pull-request-comment@v1
373+
with:
374+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
375+
number: ${{ github.event.issue.number }}
376+
header: lighthouse
377+
message: |
378+
${{ steps.format_lighthouse_score.outputs.comment }}
324379

0 commit comments

Comments
 (0)