Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record Lighthouse results #5448

Merged
merged 12 commits into from
Jan 21, 2025
17 changes: 17 additions & 0 deletions .github/workflows/lighthouse_cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
lhci:
name: Lighthouse Report - ${{ inputs.environment != null && inputs.environment || 'prod' }}
runs-on: ubuntu-latest
environment: ${{ inputs.environment != null && inputs.environment || 'prod' }}
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20.9.x
Expand All @@ -28,5 +32,18 @@ jobs:
npm run lighthouse
env:
LIGHTHOUSE_COLLECT_URL: ${{ secrets.LIGHTHOUSE_COLLECT_URL }}
- name: Build cronjobs
run: |
npm ci
npm run build-cronjobs
- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.GC_LIGHTHOUSE_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.GC_LIGHTHOUSE_SERVICE_ACCOUNT }}
- name: Report results
run: npm run cron:report-lighthouse-results
env:
BQ_LIGHTHOUSE_PROJECT: ${{ secrets.BQ_LIGHTHOUSE_PROJECT }}
BQ_LIGHTHOUSE_DATASET: ${{ secrets.BQ_LIGHTHOUSE_DATASET }}
BQ_LIGHTHOUSE_TABLE: ${{ secrets.BQ_LIGHTHOUSE_TABLE }}
64 changes: 61 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
},
"devDependencies": {
"@faker-js/faker": "^9.3.0",
"@google-cloud/bigquery": "^7.9.1",
"@playwright/test": "^1.49.1",
"@storybook/addon-a11y": "^8.4.7",
"@storybook/addon-actions": "^8.4.7",
Expand Down
58 changes: 55 additions & 3 deletions src/scripts/cronjobs/reportLighthouseResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

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

const AUDITS_TO_INCLUDE = [
Expand All @@ -29,6 +30,42 @@ type LighthouseResult = {
};
};

type LighthouseFullResult = {
url: LighthouseResult["url"];
fetchTime: string;
isRepresentativeRun: boolean;
summary: LighthouseResult["summary"];
audits: {
id: string;
score: number;
numericValue: number;
}[];
};

async function uploadLighthouseReport(results: LighthouseFullResult[]) {
if (
!process.env.BQ_LIGHTHOUSE_PROJECT ||
!process.env.BQ_LIGHTHOUSE_DATASET ||
!process.env.BQ_LIGHTHOUSE_TABLE
) {
console.error("Missing environment variables");
return;
}

try {
const bigQueryClient = new BigQuery({
projectId: process.env.BQ_LIGHTHOUSE_PROJECT,
});

const table = bigQueryClient
.dataset(process.env.BQ_LIGHTHOUSE_DATASET)
.table(process.env.BQ_LIGHTHOUSE_TABLE);
await table.insert(results);
} catch (error) {
console.error("Error uploading results", JSON.stringify(error, null, 2));
}
}

async function run() {
// The Lighthouse report that will be created by running LHCI.
const lighthouseResults: LighthouseResult[] =
Expand All @@ -49,7 +86,7 @@ async function run() {
lighthouseResults
.filter((result) => result.isRepresentativeRun === true)
.map(async (medianResult) => {
const { jsonPath, url, summary } = medianResult;
const { jsonPath, url, isRepresentativeRun, summary } = medianResult;
const fullReport = JSON.parse(
await readFile(new URL(jsonPath, import.meta.url), {
encoding: "utf8",
Expand All @@ -60,11 +97,26 @@ async function run() {
return { id, score, numericValue };
});

return { url, summary, audits };
return {
url,
fetchTime: fullReport.fetchTime,
isRepresentativeRun,
summary,
audits,
};
}),
);

logger.info("lighthouse_report", lighthouseReport);
const reportPreview = lighthouseReport.map((item) => {
return {
url: item.url,
...item.summary,
};
});
console.table(reportPreview);

await uploadLighthouseReport(lighthouseReport);
console.info("Uploaded Lighthouse report successfully");
}

try {
Expand Down
Loading