This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
Deriv Lighthouse Audit #33669
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: Deriv Lighthouse Audit | |
permissions: | |
actions: write | |
checks: write | |
contents: write | |
deployments: write | |
pull-requests: write | |
statuses: write | |
on: | |
workflow_run: | |
workflows: ['Generate preview link'] | |
types: | |
- completed | |
jobs: | |
generate_lighthouse_audit: | |
timeout-minutes: 30 | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
steps: | |
- name: Download artifact | |
id: download-artifact | |
uses: dawidd6/action-download-artifact@v2 | |
with: | |
workflow_conclusion: success | |
run_id: ${{ github.event.workflow_run.id }} | |
name: 'pr-${{ github.event.workflow_run.id }}' | |
path: pr | |
- name: Retrieve pull request | |
id: pr_information | |
run: | | |
echo "issue_number=$(cat ./pr/NR)" >> $GITHUB_OUTPUT | |
echo "preview_url=$(cat ./pr/PREVIEW_URL)" >> $GITHUB_OUTPUT | |
- name: Add comment to PR | |
id: loading_comment_to_pr | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
number: ${{ steps.pr_information.outputs.issue_number }} | |
header: lighthouse | |
message: | | |
Running Lighthouse audit... | |
- uses: actions/checkout@v3 | |
- name: Audit preview URL with Lighthouse | |
id: lighthouse_audit | |
uses: treosh/lighthouse-ci-action@v9 | |
with: | |
urls: | | |
${{ steps.pr_information.outputs.preview_url }} | |
uploadArtifacts: true | |
temporaryPublicStorage: true | |
runs: 5 | |
- name: Format lighthouse score | |
id: format_lighthouse_score | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const result = ${{ steps.lighthouse_audit.outputs.manifest }}[4].summary | |
const links = ${{ steps.lighthouse_audit.outputs.links }} | |
const formatResult = (res) => Math.round((res * 100)) | |
Object.keys(result).forEach(key => result[key] = formatResult(result[key])) | |
const score = res => res >= 90 ? 'π’' : res >= 50 ? 'π ' : 'π΄' | |
const full_report = Object.values(links)[0] | |
const comment = [ | |
`β‘οΈ [Lighthouse report](${full_report}) for the changes in this PR:`, | |
'| Category | Score |', | |
'| --- | --- |', | |
`| ${score(result.performance)} Performance | ${result.performance} |`, | |
`| ${score(result.accessibility)} Accessibility | ${result.accessibility} |`, | |
`| ${score(result['best-practices'])} Best practices | ${result['best-practices']} |`, | |
`| ${score(result.seo)} SEO | ${result.seo} |`, | |
`| ${score(result.pwa)} PWA | ${result.pwa} |`, | |
' ', | |
`*Lighthouse ran on [${Object.keys(links)[0]}](${Object.keys(links)[0]})*` | |
].join('\n') | |
core.setOutput("comment", comment); | |
core.setOutput("performance", result.performance); | |
core.setOutput("seo", result.seo); | |
core.setOutput("pwa", result.pwa); | |
core.setOutput("accessibility", result.accessibility); | |
core.setOutput("best_practices", result['best-practices']); | |
core.setOutput("performance_color", score(result.performance)); | |
core.setOutput("seo_color", score(result.seo)); | |
core.setOutput("pwa_color", score(result.pwa)); | |
core.setOutput("accessibility_color", score(result.accessibility)); | |
core.setOutput("best_practices_color", score(result['best-practices'])); | |
core.setOutput("full_report", full_report); | |
- name: Add comment to PR | |
id: comment_to_pr | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
number: ${{ github.event.issue.number }} | |
header: lighthouse | |
message: | | |
${{ steps.format_lighthouse_score.outputs.comment }} | |
- name: Slack Notification π£ | |
uses: 8398a7/action-slack@v3 | |
if: ${{ steps.format_lighthouse_score.outputs.performance < 0 }} | |
with: | |
status: custom | |
fields: workflow,job,commit,repo | |
custom_payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "plain_text", | |
"emoji": true, | |
"text": "It appears that this pull request has not met the required performance score." | |
} | |
}, | |
{ | |
"type": "divider" | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "*${{ steps.format_lighthouse_score.outputs.performance_color }} PERFORMANCE:* ${{ steps.format_lighthouse_score.outputs.performance }}\n\n*${{ steps.format_lighthouse_score.outputs.accessibility_color }} ACCESSIBILITY:* ${{ steps.format_lighthouse_score.outputs.accessibility }}\n\n*${{ steps.format_lighthouse_score.outputs.best_practices_color }} BEST PRACTICES:* ${{ steps.format_lighthouse_score.outputs.best_practices }}\n\n*${{ steps.format_lighthouse_score.outputs.seo_color }} SEO:* ${{ steps.format_lighthouse_score.outputs.seo }} \n\n*${{ steps.format_lighthouse_score.outputs.pwa_color }} PWA:* ${{ steps.format_lighthouse_score.outputs.pwa }}" | |
} | |
}, | |
{ | |
"type": "divider" | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "*User: <${{ github.event.issue.user.url }}|${{ github.event.issue.user.login }}>*\n*Link:* ${{ github.event.issue.pull_request.html_url }}\n*Title:* ${{ github.event.issue.title }}\n*Status:* ${{ github.event.issue.state }}" | |
}, | |
"accessory": { | |
"type": "image", | |
"image_url": "${{ github.event.issue.user.avatar_url }}", | |
"alt_text": "${{ github.event.issue.user.login }}" | |
} | |
}, | |
{ | |
"type": "divider" | |
}, | |
{ | |
"type": "context", | |
"elements": [ | |
{ | |
"type": "image", | |
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/notificationsWarningIcon.png", | |
"alt_text": "notifications warning icon" | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": "*<!subteam^S04RV6RFCTW> please check the PR*" | |
} | |
] | |
}, | |
{ | |
"type": "divider" | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "Want the full lighthouse report?" | |
}, | |
"accessory": { | |
"type": "button", | |
"text": { | |
"type": "plain_text", | |
"text": "View", | |
"emoji": true | |
}, | |
"value": "click_me_123", | |
"url": "${{ steps.format_lighthouse_score.outputs.full_report }}", | |
"action_id": "button-action" | |
} | |
}, | |
{ | |
"type": "divider" | |
} | |
] | |
} | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_LIGHTHOUSE }} |