Files
pauljd 3068f739e4
pauljd/davidson-ci: Davidson CI / ai-review (push) Successful in 1m0s
pauljd/davidson-ci: Davidson CI / quality (push) Successful in 1m18s
Improve CI reporting and Lighthouse checks
2026-10-01 12:01:57 +01:00

142 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
echo "================================"
echo " Davidson CI - Lighthouse"
echo "================================"
set_status() {
echo "DAVIDSON_LIGHTHOUSE_STATUS=$1" >> "${GITHUB_ENV:-/dev/null}"
}
cleanup() {
if [ -n "${SERVER_PID:-}" ]; then
echo "Stopping frontend server..."
kill "$SERVER_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT
# Skip repositories that are not frontend-capable.
if [ "${DAVIDSON_LIGHTHOUSE_ELIGIBLE:-false}" != "true" ]; then
echo "No supported frontend detected."
echo "Lighthouse skipped."
set_status "SKIP"
exit 0
fi
FRONTEND_TYPE="${DAVIDSON_FRONTEND_TYPE:-unknown}"
echo "Frontend detected: $FRONTEND_TYPE"
# For v1, only run actual Lighthouse against static sites.
if [ "$FRONTEND_TYPE" != "static" ]; then
echo "Frontend type '$FRONTEND_TYPE' is not supported by the Lighthouse runner yet."
echo "Lighthouse skipped."
set_status "SKIP"
exit 0
fi
# Check required tools.
if ! command -v php >/dev/null 2>&1; then
echo "ERROR: PHP CLI is not installed on the Davidson CI runner."
set_status "FAIL"
exit 1
fi
if ! command -v lighthouse >/dev/null 2>&1; then
echo "ERROR: Lighthouse CLI is not installed on the Davidson CI runner."
set_status "FAIL"
exit 1
fi
if ! command -v curl >/dev/null 2>&1; then
echo "ERROR: curl is not installed on the Davidson CI runner."
set_status "FAIL"
exit 1
fi
REPORT_DIR=".davidson-reports"
RAW_REPORT="$REPORT_DIR/lighthouse-report.json"
mkdir -p "$REPORT_DIR"
HOST="127.0.0.1"
PORT="8080"
URL="http://$HOST:$PORT/"
echo "Starting static frontend..."
# The router makes missing files return 404 instead of index.html.
php -S "$HOST:$PORT" -t . .davidson-ci/scripts/static-router.php \
> /tmp/davidson-lighthouse-server.log 2>&1 &
SERVER_PID=$!
echo "Waiting for frontend..."
READY=false
for attempt in {1..20}; do
if curl --fail --silent --output /dev/null "$URL"; then
READY=true
break
fi
sleep 1
done
if [ "$READY" != "true" ]; then
echo "ERROR: Frontend failed to start."
echo
echo "Server log:"
cat /tmp/davidson-lighthouse-server.log || true
set_status "FAIL"
exit 1
fi
echo "Frontend ready: $URL"
echo "Running Lighthouse..."
set +e
lighthouse "$URL" \
--output=json \
--output-path="$RAW_REPORT" \
--chrome-flags="--headless --no-sandbox --disable-gpu"
LIGHTHOUSE_EXIT=$?
set -e
if [ "$LIGHTHOUSE_EXIT" -ne 0 ]; then
echo "ERROR: Lighthouse failed with exit code $LIGHTHOUSE_EXIT."
set_status "FAIL"
exit "$LIGHTHOUSE_EXIT"
fi
if [ ! -s "$RAW_REPORT" ]; then
echo "ERROR: Lighthouse did not create a valid report."
set_status "FAIL"
exit 1
fi
echo
echo "Lighthouse completed successfully."
echo "Raw report: $RAW_REPORT"
echo "Parsing Lighthouse report..."
php .davidson-ci/scripts/parse-lighthouse.php \
"$RAW_REPORT" \
".davidson-reports/davidson-lighthouse.json"
echo
echo "Parsed report: .davidson-reports/davidson-lighthouse.json"
echo
echo "Davidson Lighthouse summary:"
cat .davidson-reports/davidson-lighthouse.json
set_status "PASS"