Files
xiaoxia-saas/infra/docker/deploy-staging.sh
T
2026-06-21 06:48:16 +08:00

61 lines
1.8 KiB
Bash

#!/bin/sh
set -eu
ROOT_DIR="/host/var/lib/xiaoxia-saas-staging/repo"
COMPOSE_DIR="$ROOT_DIR/infra/docker"
ENV_FILE="/host/var/lib/xiaoxia-saas-staging/.env"
ensure_container_running() {
name="$1"
if ! docker inspect "$name" >/dev/null 2>&1; then
echo "❌ Required infrastructure container not found: $name"
echo " Run infra/docker/infra.yml first before deploying applications."
exit 1
fi
state="$(docker inspect -f '{{.State.Status}}' "$name")"
if [ "$state" != "running" ]; then
echo "❌ Required infrastructure container is not running: $name ($state)"
exit 1
fi
}
cd "$ROOT_DIR"
cp "$ENV_FILE" "$ROOT_DIR/.env"
mkdir -p "$ROOT_DIR/apps/web/public" /host/var/lib/xiaoxia-saas-staging/generated
[ -f "$ROOT_DIR/apps/web/public/.keep" ] || printf 'placeholder' > "$ROOT_DIR/apps/web/public/.keep"
ensure_container_running xiaoxia-postgres
ensure_container_running xiaoxia-redis
cd "$COMPOSE_DIR"
WEB_PORT="${WEB_PORT:-3001}"
export WEB_PORT
export DOCKER_BUILDKIT=0
export COMPOSE_DOCKER_CLI_BUILD=0
docker compose build --pull=false api
docker compose build --pull=false worker
docker compose run --rm --no-deps api sh -c '
cd /app &&
python - <<"PY"
import os
from sqlalchemy import create_engine, text
url = os.environ["DATABASE_URL"]
engine = create_engine(url)
with engine.begin() as conn:
has_version = conn.execute(text("SELECT to_regclass('public.alembic_version')")).scalar() is not None
has_tables = conn.execute(text("SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'public' AND tablename != 'alembic_version'")).scalar()
if has_tables and not has_version:
raise SystemExit("STAMP_REQUIRED")
PY
status=$?
if [ "$status" -eq 0 ]; then
alembic upgrade head
else
alembic stamp head && alembic upgrade head
fi
'
docker compose up -d api worker web
docker compose ps