fix(deploy): charger .env.docker avant le healthcheck Postgres
Le script utilisait POSTGRES_USER=memento par défaut sans sourcer .env.docker, ce qui faisait échouer pg_isready en prod. Ajoute wait --wait, credentials container-side et logs diagnostiques en cas d'échec. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -62,12 +62,55 @@ ROOT="${DEPLOY_ROOT:-/opt/memento}"
|
||||
ARTIFACT_TGZ="${ARTIFACT_TGZ:-}"
|
||||
EXPECTED_COMMIT="${EXPECTED_COMMIT:-}"
|
||||
|
||||
load_env_docker() {
|
||||
local env_file="$ROOT/.env.docker"
|
||||
if [ -f "$env_file" ]; then
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
source "$env_file"
|
||||
set +a
|
||||
fi
|
||||
}
|
||||
|
||||
wait_for_postgres() {
|
||||
local pg_user="${POSTGRES_USER:-memento}"
|
||||
local pg_db="${POSTGRES_DB:-memento}"
|
||||
|
||||
if docker compose up -d --wait postgres 2>/dev/null; then
|
||||
echo "Postgres healthy (compose --wait)"
|
||||
return 0
|
||||
fi
|
||||
|
||||
docker compose up -d postgres
|
||||
|
||||
for i in $(seq 1 30); do
|
||||
# Use credentials from inside the container (authoritative)
|
||||
if docker compose exec -T postgres sh -c 'pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"' >/dev/null 2>&1; then
|
||||
echo "Postgres ready (${i}/30)"
|
||||
return 0
|
||||
fi
|
||||
# Fallback: host env after load_env_docker
|
||||
if docker compose exec -T postgres pg_isready -U "$pg_user" -d "$pg_db" >/dev/null 2>&1; then
|
||||
echo "Postgres ready via host env (${i}/30)"
|
||||
return 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "Postgres not ready after 60s"
|
||||
docker compose ps postgres 2>/dev/null || true
|
||||
docker compose logs postgres --tail=50 2>/dev/null || true
|
||||
docker compose exec -T postgres sh -c 'pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB"' 2>&1 || true
|
||||
return 1
|
||||
}
|
||||
|
||||
# Health check configuration: 24 iterations × 5 seconds = 2 minutes total timeout
|
||||
# This allows Next.js cold start time while keeping feedback fast
|
||||
HEALTH_CHECK_MAX_ITERATIONS=24
|
||||
HEALTH_CHECK_SLEEP_SECONDS=5
|
||||
|
||||
cd "$ROOT"
|
||||
load_env_docker
|
||||
git config --global --add safe.directory "$ROOT" 2>/dev/null || true
|
||||
git fetch origin main
|
||||
git reset --hard origin/main
|
||||
@@ -79,14 +122,9 @@ if [ -n "$EXPECTED_COMMIT" ] && [ "$GIT_COMMIT" != "$EXPECTED_COMMIT" ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker compose up -d postgres
|
||||
for i in $(seq 1 30); do
|
||||
docker compose exec -T postgres pg_isready -U "${POSTGRES_USER:-memento}" >/dev/null 2>&1 && break
|
||||
[ "$i" -eq 30 ] && { echo "Postgres not ready"; exit 1; }
|
||||
sleep 2
|
||||
done
|
||||
wait_for_postgres || exit 1
|
||||
|
||||
docker compose exec -T postgres psql -U "${POSTGRES_USER:-memento}" -d "${POSTGRES_DB:-memento}" -c "CREATE EXTENSION IF NOT EXISTS vector;" >/dev/null
|
||||
docker compose exec -T postgres sh -c 'psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "CREATE EXTENSION IF NOT EXISTS vector;"' >/dev/null
|
||||
|
||||
if git diff --name-only HEAD~1 HEAD 2>/dev/null | grep -q '^memento-note/prisma/migrations/'; then
|
||||
DUMP_FILE="/opt/memento/backups/pre-migrate-$(date +%Y%m%d-%H%M%S).sql.gz"
|
||||
@@ -154,9 +192,7 @@ for i in $(seq 1 "$HEALTH_CHECK_MAX_ITERATIONS"); do
|
||||
git checkout monitoring/metrics-token || echo "default-token-value" > monitoring/metrics-token
|
||||
fi
|
||||
|
||||
if [ -f /opt/memento/.env.docker ]; then
|
||||
export $(cat /opt/memento/.env.docker | grep -v '^#' | xargs)
|
||||
fi
|
||||
load_env_docker
|
||||
if [ -n "${TELEGRAM_BOT_TOKEN:-}" ] && [ -n "${TELEGRAM_CHAT_ID:-}" ]; then
|
||||
echo "=== Starting Monitoring Stack (with Telegram bot) ==="
|
||||
docker compose -f monitoring/docker-compose.monitoring.yml --profile telegram up -d --remove-orphans 2>&1 || echo "WARN: Failed to bring up monitoring stack"
|
||||
|
||||
Reference in New Issue
Block a user