Skip to content

Commit

Permalink
Merge pull request #5448 from mozilla/lighthouse-report
Browse files Browse the repository at this point in the history
Record Lighthouse results
  • Loading branch information
flozia authored Jan 21, 2025
2 parents edd2e66 + 586cc1c commit 49563a7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/lighthouse_cron.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ on:
environment:
description: 'Environment to run LHCI against'
required: false
default: 'prod'
default: 'stage'
type: choice
options:
- stage
- prod
jobs:
lhci:
name: Lighthouse Report - ${{ inputs.environment != null && inputs.environment || 'prod' }}
name: Lighthouse Report - ${{ inputs.environment != null && inputs.environment || 'stage' }}
runs-on: ubuntu-latest
environment: ${{ inputs.environment != null && inputs.environment || 'stage' }}
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

0 comments on commit 49563a7

Please sign in to comment.