Skip to content

tooling/lighthouse

tooling/lighthouse #30

Workflow file for this run

name: Lighthouse CI
on:
pull_request:
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
lighthouse:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Install Lighthouse CI
run: npm install -g @lhci/cli
- name: Start HTTP Server
run: npx http-server ./dist -p 4173 &
shell: bash
- name: Wait for HTTP Server to start
run: sleep 10
- name: Run Lighthouse CI
id: lhci
run: lhci autorun --config=.lighthouserc.cjs
continue-on-error: true
- name: Save Lighthouse JSON Results
run: |
mkdir -p lighthouse-results
lhci collect --config=.lighthouserc.cjs --numberOfRuns=1 --output=json --output-path=./lighthouse-results/results.json
continue-on-error: true
- name: Extract Scores and Determine Outcome
id: check_scores
run: |
SCORE=$(node -pe "const results = JSON.parse(require('fs').readFileSync('./lighthouse-results/results.json').toString()); const score = results[0].categories.accessibility.score * 100; console.log(score); score;")
if [[ $(echo "$SCORE < 100" | bc) -eq 1 ]]; then
echo "::set-output name=status::failed"
echo "::set-output name=accessibility_score::$SCORE"
else
echo "::set-output name=status::passed"
echo "::set-output name=accessibility_score::$SCORE"
fi
continue-on-error: true
- name: Post Comment on PR
uses: peter-evans/create-or-update-comment@v1
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
${{ steps.check_scores.outputs.status == 'failed' && '❌ Lighthouse checks failed with an accessibility score of ${{ steps.check_scores.outputs.accessibility_score }}%. Please review the Lighthouse report in the Artifacts.' || '✅ Lighthouse checks passed successfully with an accessibility score of ${{ steps.check_scores.outputs.accessibility_score }}%!' }}
Report URL: [View Lighthouse Report](${{ steps.extract_url.outputs.report_url }})
token: ${{ secrets.GITHUB_TOKEN }}
- name: Fail the Workflow if Lighthouse Failed
if: steps.check_scores.outputs.status == 'failed'
run: exit 1