Wait for Docker Compose health status during deploy instead of curl.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-19 17:46:00 +02:00
parent e64e67947c
commit 70079d17b8
2 changed files with 34 additions and 18 deletions
+31 -15
View File
@@ -5,7 +5,7 @@ set -euo pipefail
REMOTE_DIR="$1" REMOTE_DIR="$1"
BRANCH="$2" BRANCH="$2"
EXPECTED_SHA="$3" EXPECTED_SHA="$3"
HEALTH_URL="$4" COMPOSE_SERVICE="$4"
HEALTH_RETRIES="$5" HEALTH_RETRIES="$5"
HEALTH_INTERVAL="$6" HEALTH_INTERVAL="$6"
@@ -14,7 +14,6 @@ die() { printf 'ERROR: [remote] %s\n' "$*" >&2; exit 1; }
command -v docker >/dev/null 2>&1 || die "docker not found on remote host" command -v docker >/dev/null 2>&1 || die "docker not found on remote host"
docker compose version >/dev/null 2>&1 || die "docker compose not available on remote host" docker compose version >/dev/null 2>&1 || die "docker compose not available on remote host"
command -v curl >/dev/null 2>&1 || die "curl not found on remote host"
[[ -d "$REMOTE_DIR/.git" ]] || die "Directory is not a git repo: $REMOTE_DIR" [[ -d "$REMOTE_DIR/.git" ]] || die "Directory is not a git repo: $REMOTE_DIR"
@@ -44,21 +43,38 @@ fi
info "Rebuilding and starting containers…" info "Rebuilding and starting containers…"
docker compose up -d --build --remove-orphans docker compose up -d --build --remove-orphans
info "Waiting for health check ($HEALTH_URL)…" wait_for_docker_health() {
ok=0 info "Waiting for Docker health check (service: $COMPOSE_SERVICE)…"
for ((i = 1; i <= HEALTH_RETRIES; i++)); do local cid=""
if curl -fsS -o /dev/null "$HEALTH_URL" 2>/dev/null; then local status="starting"
ok=1
break
fi
sleep "$HEALTH_INTERVAL"
done
if [[ "$ok" -ne 1 ]]; then for ((i = 1; i <= HEALTH_RETRIES; i++)); do
die "Health check failed after $((HEALTH_RETRIES * HEALTH_INTERVAL))s." cid="$(docker compose ps -q "$COMPOSE_SERVICE" 2>/dev/null | head -1)"
fi if [[ -z "$cid" ]]; then
status="no container"
sleep "$HEALTH_INTERVAL"
continue
fi
info "Health check OK" status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$cid")"
case "$status" in
healthy)
info "Docker health check OK"
return 0
;;
unhealthy)
die "Docker health check reported unhealthy."
;;
esac
sleep "$HEALTH_INTERVAL"
done
die "Docker health check did not become healthy after $((HEALTH_RETRIES * HEALTH_INTERVAL))s (last status: $status)."
}
wait_for_docker_health
info "Pruning stopped containers…" info "Pruning stopped containers…"
docker container prune -f >/dev/null docker container prune -f >/dev/null
+3 -3
View File
@@ -8,8 +8,8 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REMOTE_HOST="${DEPLOY_HOST:-root@10.0.0.5}" REMOTE_HOST="${DEPLOY_HOST:-root@10.0.0.5}"
REMOTE_DIR="${DEPLOY_DIR:-/opt/apps/Idle-Fantasy-Save-Viewer}" REMOTE_DIR="${DEPLOY_DIR:-/opt/apps/Idle-Fantasy-Save-Viewer}"
HEALTH_URL="${DEPLOY_HEALTH_URL:-http://127.0.0.1:5000/}" COMPOSE_SERVICE="${DEPLOY_SERVICE:-viewer}"
HEALTH_RETRIES="${DEPLOY_HEALTH_RETRIES:-20}" HEALTH_RETRIES="${DEPLOY_HEALTH_RETRIES:-30}"
HEALTH_INTERVAL="${DEPLOY_HEALTH_INTERVAL:-2}" HEALTH_INTERVAL="${DEPLOY_HEALTH_INTERVAL:-2}"
SSH_OPTS=(-o BatchMode=yes -o StrictHostKeyChecking=accept-new) SSH_OPTS=(-o BatchMode=yes -o StrictHostKeyChecking=accept-new)
@@ -57,7 +57,7 @@ ssh "${SSH_OPTS[@]}" "$REMOTE_HOST" bash -s -- \
"$REMOTE_DIR" \ "$REMOTE_DIR" \
"$BRANCH" \ "$BRANCH" \
"$LOCAL_SHA" \ "$LOCAL_SHA" \
"$HEALTH_URL" \ "$COMPOSE_SERVICE" \
"$HEALTH_RETRIES" \ "$HEALTH_RETRIES" \
"$HEALTH_INTERVAL" < "$SCRIPT_DIR/deploy-remote.sh" "$HEALTH_INTERVAL" < "$SCRIPT_DIR/deploy-remote.sh"