From ee799f390375eeaccd5821a84d1b6f83d7188ca0 Mon Sep 17 00:00:00 2001 From: Adriano Date: Fri, 29 May 2026 12:27:41 +0000 Subject: [PATCH] fix(deploy): health-check via docker network, not host port MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- scripts/deploy-vps.sh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts/deploy-vps.sh b/scripts/deploy-vps.sh index 4af869d..cd0171d 100755 --- a/scripts/deploy-vps.sh +++ b/scripts/deploy-vps.sh @@ -42,7 +42,17 @@ if [[ -z "${PORT:-}" ]]; then fi fi 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 ─────────────────────────────────────────────────────────── 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)" deadline=$(( $(date +%s) + HEALTH_TIMEOUT_SECONDS )) while [[ $(date +%s) -lt $deadline ]]; do - if curl -fsS "$HEALTH_URL" >/dev/null 2>&1; then + if app_health; then echo 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 "==> deploy DONE (SHA $CURRENT_SHA → $NEW_SHA, branch $BRANCH)"