Lighthouse #727
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: Lighthouse | |
on: | |
deployment_status: | |
jobs: | |
lighthouse-run-preview: | |
name: Audit (Preview) | |
if: github.event.deployment_status.state == 'success' && github.event.deployment_status.environment == 'Preview' | |
runs-on: ubuntu-latest | |
env: | |
LIGHTHOUSE_CONFIG: 'preview' | |
LIGHTHOUSE_URLS: | | |
${{ github.event.deployment_status.target_url }} | |
${{ github.event.deployment_status.target_url }}/product/__lighthouse | |
${{ github.event.deployment_status.target_url }}/collection/__lighthouse | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Get PR number | |
id: pr | |
run: | | |
number=$(gh pr list --search "${{ github.sha }}" --state open --json number --jq ".[0].number") | |
echo "::set-output name=number::$number" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Audit URLs using Lighthouse | |
if: steps.pr.outputs.number != '' | |
id: lighthouse_audit | |
uses: treosh/lighthouse-ci-action@v9 | |
with: | |
urls: ${{ env.LIGHTHOUSE_URLS }} | |
configPath: ./.lighthouse/lighthouserc.js | |
uploadArtifacts: true | |
temporaryPublicStorage: true | |
runs: 5 | |
- name: Format Lighthouse score | |
if: always() && steps.pr.outputs.number != '' | |
id: format_lighthouse_score | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const lighthouseCommentMaker = require('./.lighthouse/lighthouseCommentMaker.js'); | |
const lighthouseOutputs = { | |
manifest: ${{ steps.lighthouse_audit.outputs.manifest }}, | |
links: ${{ steps.lighthouse_audit.outputs.links }} | |
}; | |
const comment = lighthouseCommentMaker({ lighthouseOutputs }); | |
core.setOutput("comment", comment); | |
- name: Add Lighthouse stats as comment | |
if: always() && steps.pr.outputs.number != '' | |
uses: marocchino/[email protected] | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
number: ${{ steps.pr.outputs.number }} | |
header: lighthouse | |
message: ${{ steps.format_lighthouse_score.outputs.comment }} | |
lighthouse-run-production: | |
name: Audit (Production) | |
if: github.event.deployment_status.state == 'success' && github.event.deployment_status.environment == 'Production' | |
runs-on: ubuntu-latest | |
env: | |
LIGHTHOUSE_CONFIG: 'production' | |
LIGHTHOUSE_BRANCH: 'main' | |
LIGHTHOUSE_FILE: 'README.md' | |
LIGHTHOUSE_BEGIN_TOKEN: '<!-- LIGHTHOUSE:BEGIN -->' | |
LIGHTHOUSE_END_TOKEN: '<!-- LIGHTHOUSE:END -->' | |
LIGHTHOUSE_URLS: | | |
${{ github.event.deployment_status.target_url }} | |
${{ github.event.deployment_status.target_url }}/product/__lighthouse | |
${{ github.event.deployment_status.target_url }}/collection/__lighthouse | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
# Required due to branch protection rules - must be a repo admin | |
token: ${{ secrets.PROTECTED_PUSH_TOKEN }} | |
ref: ${{ env.LIGHTHOUSE_BRANCH }} | |
- name: Audit URLs using Lighthouse | |
id: lighthouse_audit | |
uses: treosh/lighthouse-ci-action@v9 | |
with: | |
urls: ${{ env.LIGHTHOUSE_URLS }} | |
configPath: ./.lighthouse/lighthouserc.js | |
uploadArtifacts: true | |
temporaryPublicStorage: true | |
runs: 5 | |
- name: Format Lighthouse score | |
id: format_lighthouse_score | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const lighthouseCommentMaker = require('./.lighthouse/lighthouseCommentMaker.js'); | |
const lighthouseOutputs = { | |
manifest: ${{ steps.lighthouse_audit.outputs.manifest }}, | |
links: ${{ steps.lighthouse_audit.outputs.links }} | |
}; | |
const comment = lighthouseCommentMaker({ lighthouseOutputs }); | |
core.setOutput("comment", comment); | |
- name: Add Lighthouse to README.md | |
if: always() | |
run: | | |
begin=$LIGHTHOUSE_BEGIN_TOKEN | |
end=$LIGHTHOUSE_END_TOKEN | |
file=$LIGHTHOUSE_FILE | |
data="${{ steps.format_lighthouse_score.outputs.comment }}" | |
awk -i inplace \ | |
-v begin="$begin" \ | |
-v end="$end" \ | |
-v data="$data" '$0~end{f=0} !f{print} $0~begin{print data;f=1}' \ | |
"$file" | |
- name: Commit to repo | |
if: always() | |
uses: stefanzweifel/git-auto-commit-action@v4 | |
env: | |
GITHUB_TOKEN: ${{ secrets.PROTECTED_PUSH_TOKEN }} | |
with: | |
commit_message: Lighthouse Score [skip ci] | |
skip_dirty_check: true | |
file_pattern: README.md | |
commit_user_name: lighthouse-action | |
commit_user_email: [email protected] | |
commit_author: lighthouse-action <[email protected]> |