|
| 1 | +name: Lighthouse CI |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + push: |
| 7 | + branches: [master] |
| 8 | + |
| 9 | +jobs: |
| 10 | + lighthouse: |
| 11 | + name: Lighthouse Performance Audit |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + pull-requests: write |
| 15 | + contents: read |
| 16 | + issues: write |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + ref: ${{ github.event.pull_request.head.sha || github.sha }} |
| 24 | + |
| 25 | + - name: Setup Node.js |
| 26 | + uses: actions/setup-node@v4 |
| 27 | + with: |
| 28 | + node-version: '20' |
| 29 | + cache: 'npm' |
| 30 | + |
| 31 | + - name: Install dependencies |
| 32 | + run: npm ci |
| 33 | + |
| 34 | + - name: Install Lighthouse CI |
| 35 | + run: npm install -g @lhci/cli@0.14.x |
| 36 | + |
| 37 | + - name: Run Lighthouse CI |
| 38 | + id: lighthouse |
| 39 | + run: | |
| 40 | + lhci autorun --config=./lighthouserc.js 2>&1 | tee lighthouse-output.txt |
| 41 | + echo "LIGHTHOUSE_EXIT_CODE=$?" >> $GITHUB_ENV |
| 42 | +
|
| 43 | + - name: Parse Lighthouse Results |
| 44 | + id: parse |
| 45 | + run: | |
| 46 | + # Extract scores from the lighthouse output |
| 47 | + # Create a results summary |
| 48 | + mkdir -p lighthouse-results |
| 49 | + |
| 50 | + # Find the latest report |
| 51 | + REPORT_DIR=".lighthouseci" |
| 52 | + if [ -d "$REPORT_DIR" ]; then |
| 53 | + # Get the manifest file |
| 54 | + MANIFEST="$REPORT_DIR/manifest.json" |
| 55 | + if [ -f "$MANIFEST" ]; then |
| 56 | + # Extract average scores from manifest |
| 57 | + PERF_SCORE=$(jq '[.[].summary.performance] | add / length * 100 | floor' "$MANIFEST" 2>/dev/null || echo "N/A") |
| 58 | + A11Y_SCORE=$(jq '[.[].summary.accessibility] | add / length * 100 | floor' "$MANIFEST" 2>/dev/null || echo "N/A") |
| 59 | + BP_SCORE=$(jq '[.[].summary["best-practices"]] | add / length * 100 | floor' "$MANIFEST" 2>/dev/null || echo "N/A") |
| 60 | + SEO_SCORE=$(jq '[.[].summary.seo] | add / length * 100 | floor' "$MANIFEST" 2>/dev/null || echo "N/A") |
| 61 | + |
| 62 | + # Get report URL from temporary-public-storage |
| 63 | + REPORT_URL=$(jq -r '.[0].url // "N/A"' "$MANIFEST" 2>/dev/null) |
| 64 | + |
| 65 | + echo "PERF_SCORE=$PERF_SCORE" >> $GITHUB_OUTPUT |
| 66 | + echo "A11Y_SCORE=$A11Y_SCORE" >> $GITHUB_OUTPUT |
| 67 | + echo "BP_SCORE=$BP_SCORE" >> $GITHUB_OUTPUT |
| 68 | + echo "SEO_SCORE=$SEO_SCORE" >> $GITHUB_OUTPUT |
| 69 | + echo "REPORT_URL=$REPORT_URL" >> $GITHUB_OUTPUT |
| 70 | + fi |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Get Score Emoji |
| 74 | + id: emoji |
| 75 | + run: | |
| 76 | + get_emoji() { |
| 77 | + score=$1 |
| 78 | + if [ "$score" = "N/A" ]; then |
| 79 | + echo "⚪" |
| 80 | + elif [ "$score" -ge 90 ]; then |
| 81 | + echo "🟢" |
| 82 | + elif [ "$score" -ge 50 ]; then |
| 83 | + echo "🟠" |
| 84 | + else |
| 85 | + echo "🔴" |
| 86 | + fi |
| 87 | + } |
| 88 | + |
| 89 | + echo "PERF_EMOJI=$(get_emoji '${{ steps.parse.outputs.PERF_SCORE }}')" >> $GITHUB_OUTPUT |
| 90 | + echo "A11Y_EMOJI=$(get_emoji '${{ steps.parse.outputs.A11Y_SCORE }}')" >> $GITHUB_OUTPUT |
| 91 | + echo "BP_EMOJI=$(get_emoji '${{ steps.parse.outputs.BP_SCORE }}')" >> $GITHUB_OUTPUT |
| 92 | + echo "SEO_EMOJI=$(get_emoji '${{ steps.parse.outputs.SEO_SCORE }}')" >> $GITHUB_OUTPUT |
| 93 | +
|
| 94 | + - name: Comment on PR |
| 95 | + if: github.event_name == 'pull_request' |
| 96 | + uses: thollander/actions-comment-pull-request@v3 |
| 97 | + with: |
| 98 | + message: | |
| 99 | + ## 🔦 Lighthouse Performance Report |
| 100 | +
|
| 101 | + | Category | Score | Status | |
| 102 | + |----------|-------|--------| |
| 103 | + | ⚡ Performance | **${{ steps.parse.outputs.PERF_SCORE }}** | ${{ steps.emoji.outputs.PERF_EMOJI }} | |
| 104 | + | ♿ Accessibility | **${{ steps.parse.outputs.A11Y_SCORE }}** | ${{ steps.emoji.outputs.A11Y_EMOJI }} | |
| 105 | + | ✅ Best Practices | **${{ steps.parse.outputs.BP_SCORE }}** | ${{ steps.emoji.outputs.BP_EMOJI }} | |
| 106 | + | 🔍 SEO | **${{ steps.parse.outputs.SEO_SCORE }}** | ${{ steps.emoji.outputs.SEO_EMOJI }} | |
| 107 | +
|
| 108 | + ### Score Legend |
| 109 | + - 🟢 90-100: Excellent |
| 110 | + - 🟠 50-89: Needs Improvement |
| 111 | + - 🔴 0-49: Poor |
| 112 | +
|
| 113 | + > 📊 [View Full Lighthouse Report](${{ steps.parse.outputs.REPORT_URL }}) |
| 114 | + |
| 115 | + --- |
| 116 | + *This performance audit helps ensure PRs don't negatively impact user experience.* |
| 117 | + comment-tag: lighthouse-report |
| 118 | + mode: recreate |
| 119 | + pr-number: ${{ github.event.pull_request.number }} |
| 120 | + |
| 121 | + - name: Upload Lighthouse Reports |
| 122 | + uses: actions/upload-artifact@v4 |
| 123 | + if: always() |
| 124 | + with: |
| 125 | + name: lighthouse-reports |
| 126 | + path: .lighthouseci/ |
| 127 | + retention-days: 30 |
0 commit comments