Skip to content

Commit 3c6d027

Browse files
committed
Merge branch 'main' into mntor-3806
2 parents bea8257 + 3b0609e commit 3c6d027

File tree

4 files changed

+142
-13
lines changed

4 files changed

+142
-13
lines changed

.github/workflows/lighthouse_cron.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ on:
77
environment:
88
description: 'Environment to run LHCI against'
99
required: false
10-
default: 'prod'
10+
default: 'stage'
1111
type: choice
1212
options:
1313
- stage
1414
- prod
1515
jobs:
1616
lhci:
17-
name: Lighthouse Report - ${{ inputs.environment != null && inputs.environment || 'prod' }}
17+
name: Lighthouse Report - ${{ inputs.environment != null && inputs.environment || 'stage' }}
1818
runs-on: ubuntu-latest
19+
environment: ${{ inputs.environment != null && inputs.environment || 'stage' }}
20+
permissions:
21+
contents: read
22+
id-token: write
1923
steps:
2024
- uses: actions/checkout@v4
2125
- name: Use Node.js 20.9.x
@@ -28,5 +32,18 @@ jobs:
2832
npm run lighthouse
2933
env:
3034
LIGHTHOUSE_COLLECT_URL: ${{ secrets.LIGHTHOUSE_COLLECT_URL }}
35+
- name: Build cronjobs
36+
run: |
37+
npm ci
38+
npm run build-cronjobs
39+
- name: Authenticate to Google Cloud
40+
uses: google-github-actions/auth@v2
41+
with:
42+
workload_identity_provider: ${{ secrets.GC_LIGHTHOUSE_WORKLOAD_IDENTITY_PROVIDER }}
43+
service_account: ${{ secrets.GC_LIGHTHOUSE_SERVICE_ACCOUNT }}
3144
- name: Report results
3245
run: npm run cron:report-lighthouse-results
46+
env:
47+
BQ_LIGHTHOUSE_PROJECT: ${{ secrets.BQ_LIGHTHOUSE_PROJECT }}
48+
BQ_LIGHTHOUSE_DATASET: ${{ secrets.BQ_LIGHTHOUSE_DATASET }}
49+
BQ_LIGHTHOUSE_TABLE: ${{ secrets.BQ_LIGHTHOUSE_TABLE }}

package-lock.json

Lines changed: 66 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"react-aria": "^3.36.0",
107107
"react-cookie": "^7.2.2",
108108
"react-dom": "^18.3.1",
109-
"react-intersection-observer": "^9.14.0",
109+
"react-intersection-observer": "^9.15.1",
110110
"react-stately": "^3.34.0",
111111
"react-toastify": "^11.0.2",
112112
"server-only": "^0.0.1",
@@ -115,6 +115,7 @@
115115
},
116116
"devDependencies": {
117117
"@faker-js/faker": "^9.3.0",
118+
"@google-cloud/bigquery": "^7.9.1",
118119
"@playwright/test": "^1.49.1",
119120
"@storybook/addon-a11y": "^8.4.7",
120121
"@storybook/addon-actions": "^8.4.7",

src/scripts/cronjobs/reportLighthouseResults.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { readFile } from "node:fs/promises";
6+
import { BigQuery } from "@google-cloud/bigquery";
67
import { logger } from "../../app/functions/server/logging";
78

89
const AUDITS_TO_INCLUDE = [
@@ -29,6 +30,42 @@ type LighthouseResult = {
2930
};
3031
};
3132

33+
type LighthouseFullResult = {
34+
url: LighthouseResult["url"];
35+
fetchTime: string;
36+
isRepresentativeRun: boolean;
37+
summary: LighthouseResult["summary"];
38+
audits: {
39+
id: string;
40+
score: number;
41+
numericValue: number;
42+
}[];
43+
};
44+
45+
async function uploadLighthouseReport(results: LighthouseFullResult[]) {
46+
if (
47+
!process.env.BQ_LIGHTHOUSE_PROJECT ||
48+
!process.env.BQ_LIGHTHOUSE_DATASET ||
49+
!process.env.BQ_LIGHTHOUSE_TABLE
50+
) {
51+
console.error("Missing environment variables");
52+
return;
53+
}
54+
55+
try {
56+
const bigQueryClient = new BigQuery({
57+
projectId: process.env.BQ_LIGHTHOUSE_PROJECT,
58+
});
59+
60+
const table = bigQueryClient
61+
.dataset(process.env.BQ_LIGHTHOUSE_DATASET)
62+
.table(process.env.BQ_LIGHTHOUSE_TABLE);
63+
await table.insert(results);
64+
} catch (error) {
65+
console.error("Error uploading results", JSON.stringify(error, null, 2));
66+
}
67+
}
68+
3269
async function run() {
3370
// The Lighthouse report that will be created by running LHCI.
3471
const lighthouseResults: LighthouseResult[] =
@@ -49,7 +86,7 @@ async function run() {
4986
lighthouseResults
5087
.filter((result) => result.isRepresentativeRun === true)
5188
.map(async (medianResult) => {
52-
const { jsonPath, url, summary } = medianResult;
89+
const { jsonPath, url, isRepresentativeRun, summary } = medianResult;
5390
const fullReport = JSON.parse(
5491
await readFile(new URL(jsonPath, import.meta.url), {
5592
encoding: "utf8",
@@ -60,11 +97,26 @@ async function run() {
6097
return { id, score, numericValue };
6198
});
6299

63-
return { url, summary, audits };
100+
return {
101+
url,
102+
fetchTime: fullReport.fetchTime,
103+
isRepresentativeRun,
104+
summary,
105+
audits,
106+
};
64107
}),
65108
);
66109

67-
logger.info("lighthouse_report", lighthouseReport);
110+
const reportPreview = lighthouseReport.map((item) => {
111+
return {
112+
url: item.url,
113+
...item.summary,
114+
};
115+
});
116+
console.table(reportPreview);
117+
118+
await uploadLighthouseReport(lighthouseReport);
119+
console.info("Uploaded Lighthouse report successfully");
68120
}
69121

70122
try {

0 commit comments

Comments
 (0)