|
| 1 | +name: Vercel Preview URL Lighthouse Audit |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created, edited] |
| 6 | + |
| 7 | +jobs: |
| 8 | + generate_lighthouse_audit: |
| 9 | + timeout-minutes: 30 |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Get Vercel preview URL |
| 13 | + id: get_preview_url |
| 14 | + uses: actions/github-script@v3 |
| 15 | + with: |
| 16 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 17 | + script: | |
| 18 | + const comment = context.payload.comment; |
| 19 | + const regex = /https:\/\/[a-z0-9-]+\.vercel\.app/g; |
| 20 | + const matches = comment.body.match(regex); |
| 21 | + let previewUrl = ""; |
| 22 | + if (matches && matches.length) { |
| 23 | + previewUrl = matches[0]; |
| 24 | + console.log('Preview url found:', previewUrl); |
| 25 | + } |
| 26 | + console.log("No preview url found."); |
| 27 | + core.setOutput('vercel_preview_url', previewUrl); |
| 28 | + |
| 29 | + - name: Add comment to PR |
| 30 | + if: ${{ steps.get_preview_url.outputs.vercel_preview_url != '' }} |
| 31 | + id: loading_comment_to_pr |
| 32 | + uses: marocchino/sticky-pull-request-comment@v1 |
| 33 | + with: |
| 34 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + number: ${{ github.event.issue.number }} |
| 36 | + header: lighthouse |
| 37 | + message: | |
| 38 | + Running Lighthouse audit... |
| 39 | +
|
| 40 | + - name: Checkout repository |
| 41 | + if: ${{ steps.get_preview_url.outputs.vercel_preview_url != '' }} |
| 42 | + uses: actions/checkout@v3 |
| 43 | + |
| 44 | + - name: Audit preview URL with Lighthouse |
| 45 | + if: ${{ steps.get_preview_url.outputs.vercel_preview_url != '' }} |
| 46 | + id: lighthouse_audit |
| 47 | + uses: treosh/lighthouse-ci-action@v3 |
| 48 | + with: |
| 49 | + urls: | |
| 50 | + ${{ steps.get_preview_url.outputs.vercel_preview_url }} |
| 51 | + uploadArtifacts: true |
| 52 | + temporaryPublicStorage: true |
| 53 | + - name: Format lighthouse score |
| 54 | + if: ${{ steps.get_preview_url.outputs.vercel_preview_url != '' }} |
| 55 | + id: format_lighthouse_score |
| 56 | + uses: actions/github-script@v3 |
| 57 | + with: |
| 58 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 59 | + script: | |
| 60 | + const result = ${{ steps.lighthouse_audit.outputs.manifest }}[0].summary |
| 61 | + const links = ${{ steps.lighthouse_audit.outputs.links }} |
| 62 | +
|
| 63 | + const formatResult = (res) => Math.round((res * 100)) |
| 64 | + Object.keys(result).forEach(key => result[key] = formatResult(result[key])) |
| 65 | +
|
| 66 | + const score = res => res >= 90 ? '🟢' : res >= 50 ? '🟠' : '🔴' |
| 67 | +
|
| 68 | + const comment = [ |
| 69 | + `⚡️ [Lighthouse report](${Object.values(links)[0]}) for the changes in this PR:`, |
| 70 | + '| Category | Score |', |
| 71 | + '| --- | --- |', |
| 72 | + `| ${score(result.performance)} Performance | ${result.performance} |`, |
| 73 | + `| ${score(result.accessibility)} Accessibility | ${result.accessibility} |`, |
| 74 | + `| ${score(result['best-practices'])} Best practices | ${result['best-practices']} |`, |
| 75 | + `| ${score(result.seo)} SEO | ${result.seo} |`, |
| 76 | + `| ${score(result.pwa)} PWA | ${result.pwa} |`, |
| 77 | + ' ', |
| 78 | + `*Lighthouse ran on [${Object.keys(links)[0]}](${Object.keys(links)[0]})*` |
| 79 | + ].join('\n') |
| 80 | +
|
| 81 | + core.setOutput("comment", comment); |
| 82 | + |
| 83 | + - name: Add comment to PR |
| 84 | + if: ${{ steps.get_preview_url.outputs.vercel_preview_url != '' }} |
| 85 | + id: comment_to_pr |
| 86 | + uses: marocchino/sticky-pull-request-comment@v1 |
| 87 | + with: |
| 88 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 89 | + number: ${{ github.event.issue.number }} |
| 90 | + header: lighthouse |
| 91 | + message: | |
| 92 | + ${{ steps.format_lighthouse_score.outputs.comment }} |
0 commit comments