ci: run app directly with health check polling before systemd restart
Some checks failed
Deploy API Server / build-and-deploy (push) Failing after 32s

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 <noreply@anthropic.com>
This commit is contained in:
WangDL 2026-05-24 12:17:48 +08:00
parent b71371cd1c
commit 257693676c

View File

@ -29,7 +29,6 @@ jobs:
cd /tmp/api-server cd /tmp/api-server
npx prisma generate npx prisma generate
npm run build npm run build
# Normalize output: ensure main.js is at dist/main.js
if [ ! -f dist/main.js ] && [ -f dist/src/main.js ]; then if [ ! -f dist/main.js ] && [ -f dist/src/main.js ]; then
echo "[build] Moving dist/src/* to dist/" echo "[build] Moving dist/src/* to dist/"
mv dist/src/* dist/ mv dist/src/* dist/
@ -86,20 +85,41 @@ jobs:
run: | run: |
cd /opt/zhixi/backend && npx prisma generate cd /opt/zhixi/backend && npx prisma generate
- name: Test-run API directly to capture errors - name: Test-run and health check
run: | 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 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 - name: Restart API service
run: | run: |
sudo systemctl restart zhixi-api sudo systemctl restart zhixi-api
sleep 3 sleep 5
if sudo systemctl is-active zhixi-api; then if sudo systemctl is-active zhixi-api; then
echo "[deploy] zhixi-api active OK" echo "[deploy] zhixi-api active OK"
else else
echo "[deploy] zhixi-api FAILED to start — checking logs:" echo "[deploy] zhixi-api FAILED to start"
sudo journalctl -u zhixi-api --no-pager -n 100
exit 1 exit 1
fi fi