fix(deploy): health-check via docker network, not host port
The VPS doesn't publish the app port to the host (Traefik reaches it over the docker network), so curl http://localhost:PORT/health always failed — a false negative that could trigger an unwanted automatic rollback on a successful deploy. Check /health from INSIDE the container via `docker compose exec` instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+14
-3
@@ -42,7 +42,17 @@ if [[ -z "${PORT:-}" ]]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
PORT="${PORT:-9000}"
|
PORT="${PORT:-9000}"
|
||||||
HEALTH_URL="http://localhost:${PORT}/health"
|
# Health verificato DENTRO la rete del container: in produzione la porta NON è
|
||||||
|
# pubblicata sull'host (Traefik la raggiunge via rete docker), quindi un curl da
|
||||||
|
# localhost darebbe un falso negativo (e rischierebbe un rollback inutile).
|
||||||
|
HEALTH_URL="http://127.0.0.1:${PORT}/health"
|
||||||
|
|
||||||
|
# 0 se /health risponde 200 dall'interno del container.
|
||||||
|
app_health() {
|
||||||
|
docker compose exec -T "$SERVICE" python -c \
|
||||||
|
"import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('${HEALTH_URL}').status==200 else 1)" \
|
||||||
|
>/dev/null 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
# ─── Pre-check ───────────────────────────────────────────────────────────
|
# ─── Pre-check ───────────────────────────────────────────────────────────
|
||||||
command -v git >/dev/null || { echo "FATAL: git non installato"; exit 1; }
|
command -v git >/dev/null || { echo "FATAL: git non installato"; exit 1; }
|
||||||
@@ -125,10 +135,11 @@ docker compose up -d
|
|||||||
echo "==> attendo /health (timeout ${HEALTH_TIMEOUT_SECONDS}s, retry ogni ${HEALTH_INTERVAL}s)"
|
echo "==> attendo /health (timeout ${HEALTH_TIMEOUT_SECONDS}s, retry ogni ${HEALTH_INTERVAL}s)"
|
||||||
deadline=$(( $(date +%s) + HEALTH_TIMEOUT_SECONDS ))
|
deadline=$(( $(date +%s) + HEALTH_TIMEOUT_SECONDS ))
|
||||||
while [[ $(date +%s) -lt $deadline ]]; do
|
while [[ $(date +%s) -lt $deadline ]]; do
|
||||||
if curl -fsS "$HEALTH_URL" >/dev/null 2>&1; then
|
if app_health; then
|
||||||
echo
|
echo
|
||||||
echo "==> health OK"
|
echo "==> health OK"
|
||||||
curl -s "$HEALTH_URL"
|
docker compose exec -T "$SERVICE" python -c \
|
||||||
|
"import urllib.request; print(urllib.request.urlopen('${HEALTH_URL}').read().decode())" 2>/dev/null || true
|
||||||
echo
|
echo
|
||||||
echo
|
echo
|
||||||
echo "==> deploy DONE (SHA $CURRENT_SHA → $NEW_SHA, branch $BRANCH)"
|
echo "==> deploy DONE (SHA $CURRENT_SHA → $NEW_SHA, branch $BRANCH)"
|
||||||
|
|||||||
Reference in New Issue
Block a user