Lighthouse performance metrics setup #36
Workflow file for this run
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: Performance Testing | |
on: | |
pull_request: | |
branches: | |
- main | |
env: | |
NODE_OPTIONS: '--max-old-space-size=6144 --dns-result-order=ipv4first' | |
LATEST_VERSION: '2.17.0' | |
jobs: | |
lighthouse: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Setup Node | |
uses: actions/setup-node@v2 | |
with: | |
node-version-file: '.nvmrc' | |
registry-url: 'https://registry.npmjs.org' | |
- name: Setup Yarn | |
run: | | |
npm uninstall -g yarn | |
npm i -g [email protected] | |
- name: Run bootstrap | |
run: yarn osd bootstrap | |
- name: Download OpenSearch | |
uses: suisei-cn/[email protected] | |
with: | |
url: https://artifacts.opensearch.org/releases/bundle/opensearch/${{ env.LATEST_VERSION }}/opensearch-${{ env.LATEST_VERSION }}-linux-x64.tar.gz | |
- name: Extract OpenSearch | |
run: | | |
tar -xzf opensearch-*.tar.gz | |
rm -f opensearch-*.tar.gz | |
shell: bash | |
- name: Remove security plugin | |
run: | | |
/bin/bash -c "yes | ./opensearch-${{ env.LATEST_VERSION }}/bin/opensearch-plugin remove opensearch-security" | |
shell: bash | |
- name: Run OpenSearch | |
run: | | |
/bin/bash -c "./opensearch-${{ env.LATEST_VERSION }}/opensearch-tar-install.sh &" | |
sleep 30 | |
shell: bash | |
- name: Install Lighthouse CI | |
run: yarn add --dev @lhci/cli | |
- name: Run bootstrap | |
run: yarn osd bootstrap | |
- name: Build plugins | |
run: node scripts/build_opensearch_dashboards_platform_plugins --no-examples --workers 12 | |
- name: Wait for OpenSearch to be ready | |
run: | | |
until curl -s http://localhost:9200 >/dev/null; do | |
echo "Waiting for OpenSearch..." | |
sleep 10 | |
done | |
echo "OpenSearch is up!" | |
- name: Start OpenSearch Dashboards | |
run: | | |
yarn start --no-base-path & | |
until curl -s http://localhost:5601 >/dev/null; do | |
echo "Waiting for OpenSearch Dashboards..." | |
sleep 10 | |
done | |
echo "OpenSearch Dashboards is up!" | |
- name: Mock data | |
run: | | |
curl 'http://localhost:5601/api/sample_data/ecommerce' -X 'POST' -H 'osd-version: 3.0.0' -H 'osd-xsrf: osd-fetch' | |
- name: Run Lighthouse CI | |
run: | | |
export LHCI_GITHUB_APP_TOKEN=${{ secrets.GITHUB_TOKEN }} | |
yarn lhci autorun --verbose || echo "Lighthouse assertion failed, check results" | |
env: | |
LHCI_GITHUB_APP_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Verify Lighthouse Results | |
run: | | |
if [ ! -d ".lighthouseci" ] || [ -z "$(ls -A .lighthouseci)" ]; then | |
echo "❌ Lighthouse CI did not generate reports." | |
exit 1 | |
fi | |
- name: Post Lighthouse Results into comment | |
run: | | |
# Validate JSON output | |
if [ ! -s .lighthouseci/assertion-results.json ]; then | |
echo "❌ Lighthouse assertion results are empty or invalid." | |
cat .lighthouseci/assertion-results.json # Print for debugging | |
exit 1 | |
fi | |
# Ensure JSON is properly formatted | |
if ! jq empty .lighthouseci/assertion-results.json; then | |
echo "❌ Invalid JSON format in Lighthouse assertion results." | |
cat .lighthouseci/assertion-results.json # Print for debugging | |
exit 1 | |
fi | |
# Extract failed assertions | |
FAILURES=$(jq -r '[.[] | select(.passed==false) | {metric: .auditId, reason: .auditTitle, url: .url}]' .lighthouseci/assertion-results.json) | |
# Upload reports and extract report URLs | |
yarn lhci upload --target=temporary-public-storage | tee lhci_output.txt | |
REPORT_URLS=$(grep -Eo 'https://storage.googleapis.com/lighthouse-infrastructure-[^\s]+' lhci_output.txt) | |
# Map URLs to reports | |
URL_REPORT_MAP=$(jq -n --argjson failures "$FAILURES" --arg report_urls "$REPORT_URLS" ' | |
$failures | map(.url) as $urls | | |
$report_urls | split("\n") as $reports | | |
reduce range(0; $urls | length) as $i ( | |
{}; | |
.[$urls[$i]] = $reports[$i] | |
) | |
') | |
# Append report URLs to failed assertions | |
FAILURES_WITH_REPORTS=$(jq --argjson url_report_map "$URL_REPORT_MAP" ' | |
map(. + {reportUrl: $url_report_map[.url]}) | |
' <<< "$FAILURES") | |
# Format PR comment | |
if [[ "$FAILURES_WITH_REPORTS" == "[]" ]]; then | |
echo "✅ **All Lighthouse metrics passed!** 🎉" > comment.txt | |
else | |
echo "❌ **Failed Lighthouse Metrics:**" > comment.txt | |
echo "" >> comment.txt | |
echo "| Metric | Issue | Page URL | Report |" >> comment.txt | |
echo "|--------|-------|----------|--------|" >> comment.txt | |
echo "$FAILURES_WITH_REPORTS" | jq -r '.[] | "| \(.metric) | \(.reason) | \(.url) | [Report](\(.reportUrl)) |"' >> comment.txt | |
fi | |
# Post comment on PR | |
gh pr comment ${{ github.event.pull_request.number }} --body "$(cat comment.txt)" | |
env: | |
LHCI_GITHUB_APP_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Cleanup Lighthouse Reports | |
run: rm -f .lhci_output.json .lighthouseci |