From 257693676c89b081306cb4f24834e3c8ce6726e4 Mon Sep 17 00:00:00 2001 From: WangDL Date: Sun, 24 May 2026 12:17:48 +0800 Subject: [PATCH] ci: run app directly with health check polling before systemd restart Runs node dist/main.js in background, polls localhost:3000/api for up to 30s. If app crashes, prints startup log. This definitively identifies whether it's a code error or systemd timeout issue. Co-Authored-By: Claude Opus 4.7 --- .gitea/workflows/deploy.yml | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index caa4fac..f7cb632 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -29,7 +29,6 @@ jobs: cd /tmp/api-server npx prisma generate npm run build - # Normalize output: ensure main.js is at dist/main.js if [ ! -f dist/main.js ] && [ -f dist/src/main.js ]; then echo "[build] Moving dist/src/* to dist/" mv dist/src/* dist/ @@ -86,20 +85,41 @@ jobs: run: | cd /opt/zhixi/backend && npx prisma generate - - name: Test-run API directly to capture errors + - name: Test-run and health check run: | + # Stop systemd service first to free the port + sudo systemctl stop zhixi-api 2>/dev/null || true + sleep 1 + # Run app directly in background cd /opt/zhixi/backend - timeout 10 node dist/main.js 2>&1 || true + PORT=3000 node dist/main.js > /tmp/zhixi-startup.log 2>&1 & + APP_PID=$! + echo "[deploy] App PID: $APP_PID" + # Wait for app to start (up to 30s) + for i in $(seq 1 30); do + sleep 1 + if curl -sf http://localhost:3000/api > /dev/null 2>&1; then + echo "[deploy] API healthy after ${i}s!" + break + fi + if ! kill -0 $APP_PID 2>/dev/null; then + echo "[deploy] App crashed after ${i}s — startup log:" + cat /tmp/zhixi-startup.log + exit 1 + fi + done + # Kill test instance + kill $APP_PID 2>/dev/null + wait $APP_PID 2>/dev/null - name: Restart API service run: | sudo systemctl restart zhixi-api - sleep 3 + sleep 5 if sudo systemctl is-active zhixi-api; then echo "[deploy] zhixi-api active OK" else - echo "[deploy] zhixi-api FAILED to start — checking logs:" - sudo journalctl -u zhixi-api --no-pager -n 100 + echo "[deploy] zhixi-api FAILED to start" exit 1 fi