-
-
Notifications
You must be signed in to change notification settings - Fork 53
feat: add Lighthouse CI workflow and configuration for automated perf… #110
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
Open
kpj2006
wants to merge
9
commits into
AOSSIE-Org:main
Choose a base branch
from
kpj2006:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d0000fa
feat: add Lighthouse CI workflow and configuration for automated perf…
kpj2006 243d12c
Update .github/workflows/lighthouse.yml
kpj2006 f6c1a0d
Update .github/workflows/lighthouse.yml
kpj2006 fbd9b75
pin correct sha
kpj2006 82014e7
automated Lighthouse performance reporting badges
kpj2006 92c25c2
Update .github/scripts/generate-lighthouse-badges.cjs
kpj2006 535de6d
Update .github/workflows/lighthouse.yml
kpj2006 80dbf1c
remove the redundant .replace(/%20/g, '-')
kpj2006 57b308f
Merge branch 'patch-1' of https://github.com/kpj2006/OrgExplorer into…
kpj2006 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const https = require('https'); | ||
|
|
||
| // Path to LHCI manifest and output directory | ||
| const manifestPath = path.join(__dirname, '../../.lighthouseci/manifest.json'); | ||
| const outputDir = path.join(__dirname, '../../badges'); | ||
|
|
||
| // Ensure output directory exists | ||
| if (!fs.existsSync(outputDir)) { | ||
| fs.mkdirSync(outputDir, { recursive: true }); | ||
| } | ||
|
|
||
| /** | ||
| * Downloads a badge from Shields.io | ||
| * @param {string} label | ||
| * @param {string} value | ||
| * @param {string} color | ||
| * @returns {Promise<void>} | ||
| */ | ||
| function downloadBadge(label, value, color) { | ||
| const url = `https://img.shields.io/badge/${encodeURIComponent(label)}-${encodeURIComponent(value)}-${color}`; | ||
| const filename = `${label.toLowerCase().replace(/ /g, '-')}.svg`; | ||
| const filePath = path.join(outputDir, filename); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const req = https.get(url, { headers: { 'User-Agent': 'NodeJS/LighthouseBadgeGenerator' }, timeout: 10000 }, (res) => { | ||
| if (res.statusCode !== 200) { | ||
| res.resume(); | ||
| reject(new Error(`Failed to download ${label} badge: Status code ${res.statusCode}`)); | ||
| return; | ||
| } | ||
| const fileStream = fs.createWriteStream(filePath); | ||
| res.on('error', reject); | ||
| fileStream.on('error', reject); | ||
| res.pipe(fileStream); | ||
| fileStream.on('finish', () => { | ||
| fileStream.close(); | ||
| console.log(`Successfully downloaded: ${filename} (${value})`); | ||
| resolve(); | ||
| }); | ||
| }); | ||
| req.on('error', reject); | ||
| req.on('timeout', () => req.destroy(new Error(`Timed out downloading ${label} badge`))); | ||
| }); | ||
| } | ||
|
|
||
| async function main() { | ||
| try { | ||
| if (!fs.existsSync(manifestPath)) { | ||
| console.error(`Error: Manifest file not found at ${manifestPath}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); | ||
| if (!Array.isArray(manifest) || manifest.length === 0) { | ||
| console.error('Error: Manifest is empty or invalid.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // Find the representative run (median run) or fallback to first run | ||
| const run = manifest.find(r => r.isRepresentativeRun) || manifest[0]; | ||
| const summary = run.summary; | ||
|
|
||
| if (!summary) { | ||
| console.error('Error: No summary found in the representative run.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // Categories to generate badges for | ||
| const categories = { | ||
| 'Performance': summary.performance, | ||
| 'Accessibility': summary.accessibility, | ||
| 'Best Practices': summary['best-practices'], | ||
| 'SEO': summary.seo, | ||
| 'PWA': summary.pwa | ||
| }; | ||
|
kpj2006 marked this conversation as resolved.
|
||
|
|
||
| let totalScore = 0; | ||
| let count = 0; | ||
|
|
||
| for (const [name, score] of Object.entries(categories)) { | ||
| if (score === undefined || score === null) { | ||
| console.log(`Skipping category ${name} (no score available)`); | ||
| continue; | ||
| } | ||
|
|
||
| const percentage = Math.round(score * 100); | ||
| totalScore += percentage; | ||
| count++; | ||
|
|
||
| // Determine color according to Lighthouse standards | ||
| let color = 'ff3333'; // Red (0-49) | ||
| if (percentage >= 90) { | ||
| color = '00cc66'; // Green (90-100) | ||
| } else if (percentage >= 50) { | ||
| color = 'ffaa33'; // Orange (50-89) | ||
| } | ||
|
|
||
| await downloadBadge(`Lighthouse ${name}`, `${percentage}%`, color); | ||
| } | ||
|
|
||
| // Generate single aggregate score badge | ||
| if (count > 0) { | ||
| const average = Math.round(totalScore / count); | ||
| let color = 'ff3333'; | ||
| if (average >= 90) { | ||
| color = '00cc66'; | ||
| } else if (average >= 50) { | ||
| color = 'ffaa33'; | ||
| } | ||
| await downloadBadge('Lighthouse', `${average}%`, color); | ||
| } | ||
|
|
||
| console.log('Lighthouse badges generated successfully!'); | ||
| } catch (error) { | ||
| console.error('Error generating badges:', error); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| name: Lighthouse CI Audit | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| workflow_dispatch: | ||
|
|
||
| concurrency: | ||
| group: lighthouse-${{ github.ref }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| lighthouse: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 | ||
| with: | ||
| node-version: 22 | ||
| cache: npm | ||
|
|
||
| - name: Install dependencies | ||
| run: npm ci | ||
|
|
||
| - name: Build application | ||
| run: npm run build | ||
|
|
||
| - name: Run Lighthouse CI | ||
| uses: treosh/lighthouse-ci-action@3e7e23fb74242897f95c0ba9cabad3d0227b9b18 | ||
| with: | ||
| configPath: './lighthouserc.json' | ||
| temporaryPublicStorage: true | ||
|
|
||
| - name: Generate Lighthouse Badges | ||
| run: node .github/scripts/generate-lighthouse-badges.cjs | ||
|
|
||
| - name: Commit and Push Badges | ||
| if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' | ||
| env: | ||
| REF_NAME: ${{ github.ref_name }} | ||
| run: | | ||
| git config --local user.email "github-actions[bot]`@users.noreply.github.com`" | ||
| git config --local user.name "github-actions[bot]" | ||
| git add badges/ | ||
| git commit -m "chore: update lighthouse badges [skip ci]" || echo "No changes to commit" | ||
| git push origin HEAD:"$REF_NAME" | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "ci": { | ||
| "collect": { | ||
| "staticDistDir": "./dist", | ||
| "numberOfRuns": 3 | ||
| }, | ||
| "upload": { | ||
| "target": "temporary-public-storage" | ||
| }, | ||
| "assert": { | ||
| "assertions": { | ||
| "categories:performance": ["warn", { "minScore": 0.75 }], | ||
| "categories:accessibility": ["error", { "minScore": 0.8 }], | ||
| "categories:best-practices": ["error", { "minScore": 0.85 }], | ||
| "categories:seo": ["error", { "minScore": 0.85 }] | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.