Skip to content

Commit 857517b

Browse files
committed
Lighthouse: Added workflow to generate the main report artifact on push to main
1 parent fb8b4f2 commit 857517b

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

.github/workflows/build-push.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ jobs:
4141
node-version: 18
4242
- name: Installing packages
4343
run: cd performance && npm ci
44-
- name: Generating lighthouse reports for PR and main...
44+
- name: Generating lighthouse reports for PR...
45+
env:
46+
REPORT_NAME: "pr"
4547
run: |
4648
node performance/lighthouse-script.js
49+
- name: Download the artifact for main
50+
uses: actions/download-artifact@v3
51+
with:
52+
name: lighthouse-reports
53+
path: ./main-report
54+
- name: Move the main report artifact to same directory as pr report
55+
run: |
56+
mv ./main-report/main-report.json ./lighthouse-reports
4757
- name: Compare the artifacts for negative differences in performance
4858
continue-on-error: true
4959
run: |
@@ -72,4 +82,4 @@ jobs:
7282
with:
7383
name: lighthouse-reports
7484
path: lighthouse-reports/
75-
retention-days: 30
85+
retention-days: 3

.github/workflows/lighthouse.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Update lighthouse report artifact for main branch
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
env:
10+
OWNER: nginxinc
11+
REPO: nginx-hugo-theme
12+
jobs:
13+
generate-lighthouse-report:
14+
runs-on: ubuntu-22.04
15+
steps:
16+
- uses: actions/checkout@v5
17+
with:
18+
ref: ${{ github.event.workflow_run.head_branch }}
19+
- uses: actions/setup-node@v5
20+
with:
21+
node-version: 18
22+
- name: Installing packages
23+
run: cd performance && npm ci
24+
- name: Get PR number being merged
25+
run: |
26+
RESPONSE=$(curl -L \
27+
-X GET \
28+
-H "Accept: application/vnd.github+json" \
29+
-H "X-GitHub-Api-Version: 2022-11-28" \
30+
-s "https://api.github.com/repos/${{ env.OWNER }}/${{ env.REPO }}/pulls?state=closed&direction=desc&page=1&per_page=5")
31+
LATEST_PR_NUMBER=$(echo "$RESPONSE" | jq -r '.[] | select(.merged_at != null) | .number' | head -1)
32+
echo "GITHUB_PR_NUMBER=$LATEST_PR_NUMBER" >> $GITHUB_ENV
33+
- name: Generating lighthouse reports for main...
34+
env:
35+
REPORT_NAME: "main"
36+
GITHUB_PR_NUMBER: ${{ env.GITHUB_PR_NUMBER }}
37+
run: |
38+
node performance/lighthouse-script.js
39+
- uses: actions/upload-artifact@v4
40+
with:
41+
name: lighthouse-reports
42+
path: lighthouse-reports/
43+
retention-days: 30

performance/lighthouse-script.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ const puppeteer = require('puppeteer');
22
const fs = require('fs');
33

44
const PORT = 8041;
5+
const REPORT_NAME = process.env.REPORT_NAME;
56
const PR_NUMBER = process.env.GITHUB_PR_NUMBER;
67
const OUTPUT_DIR = './lighthouse-reports';
7-
const ENVIRONMENTS = [
8-
{
9-
title: 'pr',
10-
url: `https://frontdoor-test-docs.nginx.com/previews/nginx-hugo-theme/${PR_NUMBER}/`,
11-
},
12-
];
138

149
const signIntoFrontDoor = async (browser, env) => {
1510
const page = await browser.newPage();
@@ -36,21 +31,26 @@ const generateLighthouseReport = async (env) => {
3631
console.log(`Generated report for ${env['title']}...`);
3732
};
3833

39-
(async () => {
40-
const browser = await puppeteer.launch({
34+
const launchBrowser = async () => {
35+
return await puppeteer.launch({
4136
args: [`--remote-debugging-port=${PORT}`],
4237
headless: true,
4338
});
39+
};
40+
41+
(async () => {
42+
const browser = await launchBrowser();
4443
if (!fs.existsSync(OUTPUT_DIR)) {
4544
fs.mkdirSync(OUTPUT_DIR);
4645
}
4746

48-
for (const env of ENVIRONMENTS) {
49-
if (env['title'] === 'pr') {
50-
await signIntoFrontDoor(browser, env);
51-
}
52-
await generateLighthouseReport(env);
53-
}
47+
const environment = {
48+
title: `${REPORT_NAME}`,
49+
url: `https://frontdoor-test-docs.nginx.com/previews/nginx-hugo-theme/${PR_NUMBER}/`,
50+
};
51+
52+
await signIntoFrontDoor(browser, environment);
53+
await generateLighthouseReport(environment);
5454

5555
browser.close();
5656
})();

0 commit comments

Comments
 (0)