|
| 1 | +name: Lighthouse Audit |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write # 🔑 Required to post PR comment |
| 10 | + |
| 11 | +jobs: |
| 12 | + lighthouse: |
| 13 | + if: github.actor != 'dependabot[bot]' |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: ⬇️ Checkout code |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + ref: ${{ github.event.pull_request.head.ref }} |
| 21 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 22 | + |
| 23 | + - name: ⏳ Wait for Netlify preview to be live |
| 24 | + run: | |
| 25 | + PREVIEW_URL="https://deploy-preview-${{ github.event.pull_request.number }}--expressjscom-preview.netlify.app" |
| 26 | + echo "PREVIEW_URL=$PREVIEW_URL" >> "$GITHUB_ENV" |
| 27 | + for i in {1..3}; do |
| 28 | + if curl -s --head "$PREVIEW_URL" | grep "200 OK" > /dev/null; then |
| 29 | + echo "Preview is live!" |
| 30 | + break |
| 31 | + fi |
| 32 | + echo "Waiting for Netlify to deploy... ($i/3)" |
| 33 | + sleep 10 |
| 34 | + done |
| 35 | +
|
| 36 | + - name: 🕵️ Run Lighthouse audits |
| 37 | + run: | |
| 38 | + npm install -g lighthouse |
| 39 | + mkdir lighthouse-report |
| 40 | + URLS=( |
| 41 | + "$PREVIEW_URL" |
| 42 | + "$PREVIEW_URL/en/blog/posts.html" |
| 43 | + "$PREVIEW_URL/en/support/" |
| 44 | + "$PREVIEW_URL/en/resources/glossary.html" |
| 45 | + "$PREVIEW_URL/en/5x/api.html" |
| 46 | + ) |
| 47 | + for url in "${URLS[@]}"; do |
| 48 | + name=$(echo "$url" | sed 's|https://||;s|[/.]|_|g') |
| 49 | + lighthouse "$url" \ |
| 50 | + --output json \ |
| 51 | + --output-path="./lighthouse-report/$name.report.json" \ |
| 52 | + --chrome-flags="--headless" |
| 53 | + done |
| 54 | +
|
| 55 | + - name: 📦 Upload artifact |
| 56 | + uses: actions/upload-artifact@v4 |
| 57 | + with: |
| 58 | + name: lighthouse-report |
| 59 | + path: lighthouse-report/ |
| 60 | + |
| 61 | + - name: 📝 Summarize results and comment |
| 62 | + uses: actions/github-script@v7 |
| 63 | + env: |
| 64 | + PREVIEW_URL: ${{ env.PREVIEW_URL }} |
| 65 | + with: |
| 66 | + script: | |
| 67 | + const fs = require('fs'); |
| 68 | + const path = require('path'); |
| 69 | +
|
| 70 | + const files = fs.readdirSync('./lighthouse-report'); |
| 71 | + const rows = []; |
| 72 | +
|
| 73 | + for (const file of files) { |
| 74 | + const report = JSON.parse(fs.readFileSync(path.join('./lighthouse-report', file), 'utf8')); |
| 75 | + const url = report.finalUrl; |
| 76 | + const scores = { |
| 77 | + performance: Math.round(report.categories.performance.score * 100), |
| 78 | + accessibility: Math.round(report.categories.accessibility.score * 100), |
| 79 | + bestPractices: Math.round(report.categories['best-practices'].score * 100), |
| 80 | + seo: Math.round(report.categories.seo.score * 100), |
| 81 | + }; |
| 82 | +
|
| 83 | + rows.push( |
| 84 | + `#### [${url}](${url})\n` + |
| 85 | + '| Category | Score |\n' + |
| 86 | + '|----------|-------|\n' + |
| 87 | + `| ⚡ Performance | ${scores.performance} |\n` + |
| 88 | + `| ♿ Accessibility | ${scores.accessibility} |\n` + |
| 89 | + `| ✅ Best Practices | ${scores.bestPractices} |\n` + |
| 90 | + `| 🔍 SEO | ${scores.seo} |\n` |
| 91 | + ); |
| 92 | + } |
| 93 | +
|
| 94 | + const body = [ |
| 95 | + `### 🚦 Lighthouse Scores for Preview [Link](${process.env.PREVIEW_URL})`, |
| 96 | + ...rows |
| 97 | + ].join('\n\n'); |
| 98 | +
|
| 99 | + github.rest.issues.createComment({ |
| 100 | + issue_number: context.payload.pull_request.number, |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + body |
| 104 | + }); |
0 commit comments