220 lines
6.6 KiB
Bash
220 lines
6.6 KiB
Bash
#!/bin/sh
|
||
# ============================================================
|
||
# Staging 部署脚本 - Registry 拉取方式(替代 Watchtower)
|
||
# ============================================================
|
||
# 说明:原 Staging 使用 Watchtower 轮询 :staging 标签自动更新
|
||
# 本脚本替代 Watchtower,由 CI 主动触发部署,优势:
|
||
# - 部署时机精确可控,无需固定 sleep 等待
|
||
# - 部署完成立即健康检查,失败可快速回滚
|
||
# - 部署日志完整记录在 CI 中
|
||
#
|
||
# 功能:
|
||
# 1. 登录 Gitea Registry
|
||
# 2. Pull api / worker / web 三个镜像(用 commit SHA 作为 tag)
|
||
# 3. 执行数据库 Migration
|
||
# 4. 停止并重新启动三个业务容器
|
||
# 5. 健康检查等待就绪
|
||
#
|
||
# 依赖的环境变量(由 CI 通过 SSH 传入):
|
||
# IMAGE_TAG - 镜像标签(一般为 commit SHA)
|
||
# REGISTRY_TOKEN - Gitea Registry 访问令牌
|
||
# ============================================================
|
||
|
||
set -eu
|
||
|
||
IMAGE_TAG="${IMAGE_TAG:-}"
|
||
REGISTRY="${REGISTRY:-git.xiaoxiajianji.com/xiaoxia/xiaoxia-saas}"
|
||
REGISTRY_USER="${REGISTRY_USER:-xiaoxia}"
|
||
REGISTRY_TOKEN="${REGISTRY_TOKEN:-}"
|
||
|
||
ENV_FILE="${ENV_FILE:-/var/lib/xiaoxia-saas-staging/.env}"
|
||
GENERATED_DIR="${GENERATED_DIR:-/var/lib/xiaoxia-saas-staging/generated}"
|
||
|
||
if [ -z "$IMAGE_TAG" ]; then
|
||
echo "ERROR: IMAGE_TAG is required"
|
||
exit 1
|
||
fi
|
||
|
||
test -f "$ENV_FILE"
|
||
mkdir -p "$GENERATED_DIR"
|
||
|
||
# ---- 登录 Registry ----
|
||
if [ -n "$REGISTRY_TOKEN" ]; then
|
||
echo "Logging in to registry: $REGISTRY"
|
||
REGISTRY_HOST=$(echo "$REGISTRY" | cut -d/ -f1)
|
||
printf %s "$REGISTRY_TOKEN" | docker login "$REGISTRY_HOST" -u "$REGISTRY_USER" --password-stdin 2>/dev/null || {
|
||
echo "WARN: docker login failed, will try to pull anyway"
|
||
}
|
||
fi
|
||
|
||
# ---- Pull 三个镜像 ----
|
||
REGISTRY_API="${REGISTRY}/xiaoxia-saas-api:${IMAGE_TAG}"
|
||
REGISTRY_WORKER="${REGISTRY}/xiaoxia-saas-worker:${IMAGE_TAG}"
|
||
REGISTRY_WEB="${REGISTRY}/xiaoxia-saas-web:${IMAGE_TAG}"
|
||
|
||
LOCAL_API="xiaoxia-saas-api:staging-${IMAGE_TAG}"
|
||
LOCAL_WORKER="xiaoxia-saas-worker:staging-${IMAGE_TAG}"
|
||
LOCAL_WEB="xiaoxia-saas-web:staging-${IMAGE_TAG}"
|
||
|
||
echo "Pulling API image..."
|
||
docker pull "$REGISTRY_API"
|
||
echo "Pulling Worker image..."
|
||
docker pull "$REGISTRY_WORKER"
|
||
echo "Pulling Web image..."
|
||
docker pull "$REGISTRY_WEB"
|
||
|
||
# ---- Re-tag 成本地镜像名 ----
|
||
docker tag "$REGISTRY_API" "$LOCAL_API"
|
||
docker tag "$REGISTRY_WORKER" "$LOCAL_WORKER"
|
||
docker tag "$REGISTRY_WEB" "$LOCAL_WEB"
|
||
echo "All images pulled and tagged."
|
||
|
||
# ---- 检查基础设施容器 ----
|
||
echo "Checking infrastructure containers..."
|
||
for c in xiaoxia-postgres-staging xiaoxia-redis-staging; do
|
||
if ! docker inspect "$c" >/dev/null 2>&1; then
|
||
echo "ERROR: Required container not found: $c"
|
||
exit 1
|
||
fi
|
||
state=$(docker inspect -f '{{.State.Status}}' "$c")
|
||
if [ "$state" != "running" ]; then
|
||
echo "ERROR: Container not running: $c ($state)"
|
||
exit 1
|
||
fi
|
||
done
|
||
|
||
# ---- 创建 Staging 网络 ----
|
||
docker network create xiaoxia-net-staging 2>/dev/null || true
|
||
|
||
# ---- 执行数据库 Migration ----
|
||
echo "Running database migrations..."
|
||
docker run --rm \
|
||
--env-file "$ENV_FILE" \
|
||
--network xiaoxia-net-staging \
|
||
-e APP_ENV=staging \
|
||
"$LOCAL_API" sh -c "cd /app && alembic upgrade head"
|
||
echo "Migrations completed."
|
||
|
||
# ---- 停止旧容器 ----
|
||
echo "Stopping old containers..."
|
||
docker rm -f xiaoxia-api-staging 2>/dev/null || true
|
||
docker rm -f xiaoxia-worker-staging 2>/dev/null || true
|
||
docker rm -f xiaoxia-web-staging 2>/dev/null || true
|
||
|
||
# ---- 启动参数(日志配置统一) ----
|
||
LOG_OPTS="--log-driver json-file --log-opt max-size=50m --log-opt max-file=3"
|
||
|
||
# ---- 启动 API 容器 ----
|
||
echo "Starting API container..."
|
||
docker run -d \
|
||
--name xiaoxia-api-staging \
|
||
--env-file "$ENV_FILE" \
|
||
--network xiaoxia-net-staging \
|
||
-p 127.0.0.1:8000:8000 \
|
||
-e APP_ENV=staging \
|
||
-e APP_VERSION="$IMAGE_TAG" \
|
||
-e GENERATED_FILES_DIR=/app/generated \
|
||
-e GENERATED_FILES_URL_PREFIX=/generated-files \
|
||
-e PUBLIC_API_BASE_URL=https://staging-api.xiaoxiajianji.com \
|
||
-v "$GENERATED_DIR:/app/generated" \
|
||
--restart unless-stopped \
|
||
--cpus 1 \
|
||
--memory 1g \
|
||
--health-cmd "python3 -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=5)\"" \
|
||
--health-interval 30s \
|
||
--health-timeout 10s \
|
||
--health-retries 3 \
|
||
--health-start-period 30s \
|
||
$LOG_OPTS \
|
||
"$LOCAL_API"
|
||
|
||
# ---- 启动 Worker 容器 ----
|
||
echo "Starting Worker container..."
|
||
docker run -d \
|
||
--name xiaoxia-worker-staging \
|
||
--env-file "$ENV_FILE" \
|
||
--network xiaoxia-net-staging \
|
||
-e APP_ENV=staging \
|
||
-e APP_VERSION="$IMAGE_TAG" \
|
||
-e WORKER_CONCURRENCY=1 \
|
||
-e WORKER_MAX_TASKS_PER_CHILD=100 \
|
||
-e GENERATED_FILES_DIR=/app/generated \
|
||
-e GENERATED_FILES_URL_PREFIX=/generated-files \
|
||
-e PUBLIC_API_BASE_URL=https://staging-api.xiaoxiajianji.com \
|
||
-v "$GENERATED_DIR:/app/generated" \
|
||
--restart unless-stopped \
|
||
--cpus 1 \
|
||
--memory 1g \
|
||
--health-cmd "sh -c \"grep -q celery /proc/1/cmdline || exit 1\"" \
|
||
--health-interval 30s \
|
||
--health-timeout 10s \
|
||
--health-retries 3 \
|
||
--health-start-period 30s \
|
||
$LOG_OPTS \
|
||
"$LOCAL_WORKER"
|
||
|
||
# ---- 启动 Web 容器 ----
|
||
echo "Starting Web container..."
|
||
docker run -d \
|
||
--name xiaoxia-web-staging \
|
||
--network xiaoxia-net-staging \
|
||
-p 127.0.0.1:3001:80 \
|
||
--restart unless-stopped \
|
||
--cpus 0.5 \
|
||
--memory 256m \
|
||
--health-cmd "wget --spider -q http://127.0.0.1:80" \
|
||
--health-interval 30s \
|
||
--health-timeout 5s \
|
||
--health-retries 3 \
|
||
$LOG_OPTS \
|
||
"$LOCAL_WEB"
|
||
|
||
# ---- 等待 API 就绪 ----
|
||
echo "Waiting for API to become healthy..."
|
||
i=0
|
||
while [ "$i" -lt 30 ]; do
|
||
if curl -sf --max-time 5 http://127.0.0.1:8000/health >/dev/null 2>&1; then
|
||
echo "API is healthy!"
|
||
break
|
||
fi
|
||
i=$((i + 1))
|
||
echo " Waiting... ($i/30)"
|
||
sleep 2
|
||
done
|
||
|
||
if [ "$i" -ge 30 ]; then
|
||
echo "ERROR: API did not become healthy within 60s"
|
||
docker logs --tail 50 xiaoxia-api-staging
|
||
exit 1
|
||
fi
|
||
|
||
# ---- 等待 Web 就绪 ----
|
||
echo "Waiting for Web to become healthy..."
|
||
i=0
|
||
while [ "$i" -lt 15 ]; do
|
||
if curl -sf --max-time 5 http://127.0.0.1:3001/ >/dev/null 2>&1; then
|
||
echo "Web is healthy!"
|
||
break
|
||
fi
|
||
i=$((i + 1))
|
||
echo " Waiting... ($i/15)"
|
||
sleep 2
|
||
done
|
||
|
||
if [ "$i" -ge 15 ]; then
|
||
echo "ERROR: Web did not become healthy within 30s"
|
||
docker logs --tail 30 xiaoxia-web-staging
|
||
exit 1
|
||
fi
|
||
|
||
# ---- 清理旧镜像 ----
|
||
echo "Cleaning up old images..."
|
||
docker image prune -af --filter "until=72h" 2>/dev/null || true
|
||
|
||
echo ""
|
||
echo "=== Staging deployment complete ==="
|
||
echo "API: http://127.0.0.1:8000"
|
||
echo "Web: http://127.0.0.1:3001"
|
||
echo "Version: $IMAGE_TAG"
|
||
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Image}}" | grep staging
|