Skip to content

Commit 612cabe

Browse files
author
nginx
committed
Pre OSS commit
1 parent 4c8ec46 commit 612cabe

File tree

5 files changed

+1831
-51
lines changed

5 files changed

+1831
-51
lines changed

.github/workflows/lighthouse.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Lighthouse
2+
on:
3+
workflow_run:
4+
workflows: [Build and deploy (docs)]
5+
types:
6+
- completed
7+
env:
8+
FRONT_DOOR_USERNAME: ${{ secrets.FRONT_DOOR_USERNAME }}
9+
FRONT_DOOR_PASSWORD: ${{ secrets.FRONT_DOOR_PASSWORD }}
10+
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
11+
jobs:
12+
lighthouseci:
13+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v3
17+
- uses: actions/setup-node@v3
18+
with:
19+
node-version: 18
20+
- name: Installing packages
21+
run: npm install
22+
- name: Generating lighthouse reports for PR and main...
23+
run: |
24+
node lighthouse-script.js
25+
- name: Compare the artifacts for negative differences in performance
26+
run: |
27+
FIELDS=("performance" "accessibility")
28+
for FIELD in "${FIELDS[@]}"; do
29+
PR_VALUE=$(cat lighthouse-reports/pr-report.json | jq -r ".categories.$FIELD.score")
30+
MAIN_VALUE=$(cat lighthouse-reports/main-report.json | jq -r ".categories.$FIELD.score")
31+
echo "$FIELD: PR - $PR_VALUE | Main - $MAIN_VALUE"
32+
33+
if [ $FIELD = "performance" ]; then
34+
LOWER_BOUND=$(echo "$MAIN_VALUE - 0.05" | bc)
35+
UPPER_BOUND=$(echo "$MAIN_VALUE + 0.05" | bc)
36+
if (( $(echo "$PR_VALUE < $LOWER_BOUND" | bc -l) || $(echo "$PR_VALUE > $UPPER_BOUND" | bc -l) )); then
37+
echo "Error: $FIELD score in PR ($PR_VALUE) is less than in MAIN ($MAIN_VALUE)"
38+
exit 1
39+
fi
40+
else
41+
if (( $(echo "$PR_VALUE < $MAIN_VALUE" | bc -l) )); then
42+
echo "Error: $FIELD score in PR ($PR_VALUE) is less than in MAIN ($MAIN_VALUE)"
43+
exit 1
44+
fi
45+
fi
46+
done
47+
- uses: actions/upload-artifact@v4
48+
if: ${{ !cancelled() }}
49+
with:
50+
name: lighthouse-reports
51+
path: lighthouse-reports/
52+
retention-days: 30

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ resources/
3535
.netlify/plugins/*
3636
.netlify/
3737

38+
# Local Lighthouse artifacts
39+
lighthouse-reports
40+
3841
*.pyc
3942
build
4043
*.swp

lighthouse-script.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const puppeteer = require('puppeteer');
2+
const fs = require('fs');
3+
4+
const PORT = 8041;
5+
const PR_NUMBER = process.env.GITHUB_PR_NUMBER;
6+
const environments = [
7+
{
8+
title: "pr",
9+
url: `https://frontdoor-test-docs.nginx.com/previews/docs/${PR_NUMBER}/`
10+
},
11+
{
12+
title: "main",
13+
url: "https://docs.nginx.com/"
14+
}];
15+
const OUTPUT_DIR = './lighthouse-reports';
16+
17+
const signIntoFrontDoor = async (browser, env) => {
18+
const page = await browser.newPage();
19+
await page.authenticate({
20+
username: process.env.FRONT_DOOR_USERNAME,
21+
password: process.env.FRONT_DOOR_PASSWORD
22+
});
23+
24+
await page.goto(env['url']);
25+
await page.waitForSelector('.navbar');
26+
console.log("Logged in...");
27+
await page.close();
28+
}
29+
30+
const runLighthouse = async (env) => {
31+
const OUTPUT_FILE= `${env['title']}-report.json`;
32+
33+
const lighthouse = (await import('lighthouse')).default;
34+
console.log(`Running Lighthouse for ${env['title']}...`)
35+
const result = await lighthouse(env['url'], { port: PORT });
36+
fs.writeFileSync(`${OUTPUT_DIR}/${OUTPUT_FILE}`, result.report);
37+
}
38+
39+
(async () => {
40+
const browser = await puppeteer.launch({ args: [`--remote-debugging-port=${PORT}`], headless: true });
41+
if (!fs.existsSync(OUTPUT_DIR)){
42+
fs.mkdirSync(OUTPUT_DIR);
43+
}
44+
45+
for(const env of environments){
46+
if(env['title'] == 'pr') {
47+
await signIntoFrontDoor(browser, env);
48+
}
49+
await runLighthouse(env);
50+
}
51+
52+
await browser.close();
53+
})();

0 commit comments

Comments
 (0)