diff --git a/modules/lighthouse.sh b/modules/lighthouse.sh index 09671ec..3a35f0d 100755 --- a/modules/lighthouse.sh +++ b/modules/lighthouse.sh @@ -6,15 +6,123 @@ 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." - - echo "DAVIDSON_LIGHTHOUSE_STATUS=SKIP" >> "${GITHUB_ENV:-/dev/null}" + set_status "SKIP" exit 0 fi -echo "Frontend detected: ${DAVIDSON_FRONTEND_TYPE:-unknown}" -echo "Lighthouse eligible." +FRONTEND_TYPE="${DAVIDSON_FRONTEND_TYPE:-unknown}" -echo "DAVIDSON_LIGHTHOUSE_STATUS=PASS" >> "${GITHUB_ENV:-/dev/null}" \ No newline at end of file +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..." +php -S "$HOST:$PORT" -t . > /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" + +set_status "PASS" \ No newline at end of file