#!/usr/bin/env bash # ci-cd.yml › Build & Deploy › Verify backend — wait until erpcore.service answers /health, and fail the job otherwise. # # EF migrations + seeding run BEFORE the app starts listening, so a slow start is normal. Only a # crash (unit failed, or systemd auto-restarted it) or the ceiling counts as failure — a crashed # service fails within seconds, and a slow but live migration is never cut short by a tight timer. # # Env: SERVICE (erpcore.service) · BACKEND_PORT (5220) · HEALTH_TIMEOUT seconds (300) # RESTARTS_AT_START — NRestarts captured right after `systemctl start` (the Start step exports it) set -euo pipefail service="${SERVICE:-erpcore.service}" port="${BACKEND_PORT:-5220}" timeout="${HEALTH_TIMEOUT:-300}" restarts_at_start="${RESTARTS_AT_START:-$(systemctl show -p NRestarts --value "$service")}" show_journal() { journalctl -u "$service" -n 80 --no-pager || true; } deadline=$((SECONDS + timeout)) while ((SECONDS < deadline)); do if curl -fsS --max-time 5 -o /dev/null "http://127.0.0.1:$port/health" 2>/dev/null; then echo "✅ $service is healthy (after ${SECONDS}s)" exit 0 fi state="$(systemctl show -p ActiveState --value "$service")" restarts="$(systemctl show -p NRestarts --value "$service")" if [[ "$state" == failed || "$restarts" != "$restarts_at_start" ]]; then echo "❌ $service crashed during startup (state=$state, restarts $restarts_at_start → $restarts)" >&2 show_journal exit 1 fi sleep 3 done echo "❌ $service did not answer /health within ${timeout}s (still starting? DB reachable?)" >&2 show_journal exit 1