devops: run lhci on bot and user landings #57
This file contains 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
name: Vercel Preview URL Lighthouse Audit | |
on: | |
issue_comment: | |
types: [edited] | |
jobs: | |
generate_lighthouse_audit: | |
timeout-minutes: 30 | |
runs-on: ubuntu-latest | |
steps: | |
- name: Add comment to PR | |
id: loading_comment_to_pr | |
uses: marocchino/sticky-pull-request-comment@v2.9.0 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
number: ${{ github.event.issue.number }} | |
header: lighthouse | |
message: | | |
Running Lighthouse audit... | |
- name: Capture Vercel preview URL | |
id: vercel_preview_url | |
uses: zentered/vercel-preview-url@v1.1.9 | |
env: | |
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} | |
with: | |
vercel_project_id: prj_k5bYwz7n5hAUFdrWE986D91RiMbW | |
vercel_team_id: team_7PAgeqjbXkY6qdxNIEKLbVSC | |
- name: Get URL | |
run: echo "https://${{ steps.vercel_preview_url.outputs.preview_url }}" | |
- uses: actions/checkout@v4 | |
- name: Audit preview URL with Lighthouse | |
id: lighthouse_audit | |
uses: treosh/lighthouse-ci-action@11.4.0 | |
with: | |
urls: | | |
"https://${{ steps.vercel_preview_url.outputs.preview_url }}" | |
"https://${{ steps.vercel_preview_url.outputs.preview_url }}/b/moneybot" | |
"https://${{ steps.vercel_preview_url.outputs.preview_url }}/u/slug-1" | |
"https://${{ steps.vercel_preview_url.outputs.preview_url }}/p" | |
# budgetPath: '.github/lighthouse/budget.json' | |
uploadArtifacts: true | |
temporaryPublicStorage: true | |
- name: Format lighthouse score | |
id: format_lighthouse_score | |
uses: actions/github-script@v7.0.1 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const manifest = ${{ toJSON(steps.lighthouse_audit.outputs.manifest) }}; | |
const links = ${{ toJSON(steps.lighthouse_audit.outputs.links) }}; | |
if (!manifest || Object.keys(manifest).length === 0) { | |
core.setFailed('Lighthouse audit did not generate any results.'); | |
return; | |
} | |
const formatResult = (res) => Math.round((res * 100)); | |
const score = (res) => (res >= 90 ? '🟢' : res >= 50 ? '🟠' : '🔴'); | |
let comment | |
Object.keys(manifest).forEach((page) => { | |
comment += `*Lighthouse ran on [${pageLink}](${pageLink})*\n\n`; | |
comment += '| Category | Score |\n'; | |
comment += '| --- | --- |\n'; | |
const result = manifest[page].summary; | |
const pageLink = manifest[page].url; | |
Object.keys(result).forEach((key) => { | |
const formattedScore = formatResult(result[key]); | |
comment += `| ${score(result[key])} ${key} | ${formattedScore} |\n`; | |
}); | |
}); | |
core.setOutput('comment', comment); | |
- name: Add comment to PR | |
id: comment_to_pr | |
uses: marocchino/sticky-pull-request-comment@v2.9.0 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
number: ${{ github.event.issue.number }} | |
header: lighthouse | |
message: | | |
${{ steps.format_lighthouse_score.outputs.comment }} |