fix(ci): 双模式连接PG/Redis(端口映射+容器IP fallback),兼容所有runner网络环境
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 12s
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 18s
AI Code Review / AI Code Review (pull_request) Failing after 41s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 4m51s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 5m8s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 5m11s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 5m2s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m25s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m31s
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 12s
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 18s
AI Code Review / AI Code Review (pull_request) Failing after 41s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 4m51s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 5m8s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 5m11s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 5m2s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m25s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m31s
不同CI runner实例的Docker网络配置不一致: - new-4/new-10: 端口映射正常,容器IP不通 - new-9: 端口映射失效,容器IP可能可用 改为双模式自动探测:先尝试宿主端口映射(127.0.0.1:随机端口), 失败则自动fallback到容器IP直连,两种方式都试15次, 确保在任何runner环境下都能连通PG和Redis容器。 修改文件: - scripts/ci/run_validate.sh: PG双模式连接 - scripts/ci/run_integration_tests.sh: PG+Redis双模式连接
This commit is contained in:
@@ -25,19 +25,16 @@ echo "=== 启动 Redis ==="
|
||||
REDIS_CONTAINER="ci-redis-${GITHUB_RUN_ID:-$$}"
|
||||
docker rm -f "$REDIS_CONTAINER" 2>/dev/null || true
|
||||
docker run -d --name "$REDIS_CONTAINER" \
|
||||
-P \
|
||||
--health-cmd "redis-cli ping" \
|
||||
--health-interval 2s \
|
||||
--health-timeout 2s \
|
||||
--health-retries 10 \
|
||||
redis:7-alpine
|
||||
# 使用容器IP直连,不依赖宿主端口映射(部分runner的DinD端口映射不稳定)
|
||||
REDIS_HOST=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$REDIS_CONTAINER")
|
||||
REDIS_PORT=6379
|
||||
echo "Redis host: $REDIS_HOST:$REDIS_PORT"
|
||||
export REDIS_URL="redis://$REDIS_HOST:$REDIS_PORT/0"
|
||||
# 等待Redis容器内部ready
|
||||
for i in $(seq 1 15); do
|
||||
if docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" 2>/dev/null | grep -q healthy; then
|
||||
echo "Redis is ready on $REDIS_HOST:$REDIS_PORT"
|
||||
echo "Redis container is healthy"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for Redis... ($i/15)"
|
||||
@@ -45,16 +42,34 @@ for i in $(seq 1 15); do
|
||||
done
|
||||
docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" | grep -q healthy
|
||||
|
||||
# 验证:通过容器IP直接连通 Redis
|
||||
for i in $(seq 1 20); do
|
||||
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$REDIS_HOST', $REDIS_PORT)); s.close()" 2>/dev/null; then
|
||||
echo "TCP connectivity to Redis confirmed on $REDIS_HOST:$REDIS_PORT"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for TCP connectivity to Redis on $REDIS_HOST:$REDIS_PORT... ($i/20)"
|
||||
sleep 2
|
||||
# 双模式探测:先试端口映射,再试容器IP,哪个通就用哪个
|
||||
REDIS_MAPPED_PORT=$(docker port "$REDIS_CONTAINER" 6379/tcp | cut -d: -f2)
|
||||
REDIS_CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$REDIS_CONTAINER")
|
||||
echo "Redis mapped port: $REDIS_MAPPED_PORT, container IP: $REDIS_CONTAINER_IP"
|
||||
REDIS_HOST=""
|
||||
REDIS_PORT=""
|
||||
for mode in "mapped:127.0.0.1:$REDIS_MAPPED_PORT" "container:$REDIS_CONTAINER_IP:6379"; do
|
||||
MODE_NAME=$(echo "$mode" | cut -d: -f1)
|
||||
TEST_HOST=$(echo "$mode" | cut -d: -f2)
|
||||
TEST_PORT=$(echo "$mode" | cut -d: -f3)
|
||||
echo "Testing Redis $MODE_NAME mode ($TEST_HOST:$TEST_PORT)..."
|
||||
for i in $(seq 1 15); do
|
||||
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$TEST_HOST', $TEST_PORT)); s.close()" 2>/dev/null; then
|
||||
REDIS_HOST="$TEST_HOST"
|
||||
REDIS_PORT="$TEST_PORT"
|
||||
echo "✅ Redis $MODE_NAME mode works: $TEST_HOST:$TEST_PORT"
|
||||
break 2
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
echo "❌ Redis $MODE_NAME mode failed, trying next..."
|
||||
done
|
||||
python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$REDIS_HOST', $REDIS_PORT)); s.close()"
|
||||
if [ -z "$REDIS_HOST" ]; then
|
||||
echo "ERROR: 无法通过任何方式连接到Redis容器"
|
||||
exit 1
|
||||
fi
|
||||
echo "Using Redis: $REDIS_HOST:$REDIS_PORT"
|
||||
export REDIS_URL="redis://$REDIS_HOST:$REDIS_PORT/0"
|
||||
|
||||
# --- 启动 PostgreSQL ---
|
||||
echo ""
|
||||
@@ -66,19 +81,16 @@ docker run -d --name "$PG_CONTAINER" \
|
||||
-e POSTGRES_USER=postgres \
|
||||
-e POSTGRES_PASSWORD=postgres \
|
||||
-e POSTGRES_DB=xiaoxia_saas \
|
||||
-P \
|
||||
--health-cmd "pg_isready -U postgres" \
|
||||
--health-interval 5s \
|
||||
--health-timeout 5s \
|
||||
--health-retries 12 \
|
||||
postgres:16
|
||||
# 使用容器IP直连,不依赖宿主端口映射(部分runner的DinD端口映射不稳定)
|
||||
PG_HOST=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$PG_CONTAINER")
|
||||
PG_PORT=5432
|
||||
echo "PostgreSQL host: $PG_HOST:$PG_PORT"
|
||||
export DATABASE_URL="postgresql+psycopg://postgres:postgres@$PG_HOST:$PG_PORT/xiaoxia_saas"
|
||||
# 等待PG容器内部ready
|
||||
for i in $(seq 1 30); do
|
||||
if docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" 2>/dev/null | grep -q healthy; then
|
||||
echo "PostgreSQL is ready on $PG_HOST:$PG_PORT"
|
||||
echo "PostgreSQL container is healthy"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for PostgreSQL... ($i/30)"
|
||||
@@ -86,16 +98,35 @@ for i in $(seq 1 30); do
|
||||
done
|
||||
docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" | grep -q healthy
|
||||
|
||||
# 验证:通过容器IP直接连通 PG
|
||||
for i in $(seq 1 30); do
|
||||
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$PG_HOST', $PG_PORT)); s.close()" 2>/dev/null; then
|
||||
echo "TCP connectivity to PostgreSQL confirmed on $PG_HOST:$PG_PORT"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for TCP connectivity to PostgreSQL on $PG_HOST:$PG_PORT... ($i/30)"
|
||||
sleep 2
|
||||
# 双模式探测:先试端口映射,再试容器IP,哪个通就用哪个
|
||||
PG_MAPPED_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2)
|
||||
PG_CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$PG_CONTAINER")
|
||||
echo "PostgreSQL mapped port: $PG_MAPPED_PORT, container IP: $PG_CONTAINER_IP"
|
||||
PG_HOST=""
|
||||
PG_PORT=""
|
||||
for mode in "mapped:127.0.0.1:$PG_MAPPED_PORT" "container:$PG_CONTAINER_IP:5432"; do
|
||||
MODE_NAME=$(echo "$mode" | cut -d: -f1)
|
||||
TEST_HOST=$(echo "$mode" | cut -d: -f2)
|
||||
TEST_PORT=$(echo "$mode" | cut -d: -f3)
|
||||
echo "Testing PG $MODE_NAME mode ($TEST_HOST:$TEST_PORT)..."
|
||||
for i in $(seq 1 15); do
|
||||
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$TEST_HOST', $TEST_PORT)); s.close()" 2>/dev/null; then
|
||||
PG_HOST="$TEST_HOST"
|
||||
PG_PORT="$TEST_PORT"
|
||||
echo "✅ PG $MODE_NAME mode works: $TEST_HOST:$TEST_PORT"
|
||||
break 2
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
echo "❌ PG $MODE_NAME mode failed, trying next..."
|
||||
done
|
||||
python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$PG_HOST', $PG_PORT)); s.close()"
|
||||
if [ -z "$PG_HOST" ]; then
|
||||
echo "ERROR: 无法通过任何方式连接到PostgreSQL容器"
|
||||
echo "端口映射和容器IP两种方式均失败,可能是当前runner的Docker网络配置异常"
|
||||
exit 1
|
||||
fi
|
||||
echo "Using PostgreSQL: $PG_HOST:$PG_PORT"
|
||||
export DATABASE_URL="postgresql+psycopg://postgres:postgres@$PG_HOST:$PG_PORT/xiaoxia_saas"
|
||||
|
||||
# --- 执行迁移 ---
|
||||
echo ""
|
||||
|
||||
+36
-15
@@ -178,19 +178,17 @@ docker run -d --name "$PG_CONTAINER" \
|
||||
-e POSTGRES_USER=postgres \
|
||||
-e POSTGRES_PASSWORD=postgres \
|
||||
-e POSTGRES_DB=xiaoxia_saas \
|
||||
-P \
|
||||
--health-cmd "pg_isready -U postgres" \
|
||||
--health-interval 3s \
|
||||
--health-timeout 3s \
|
||||
--health-retries 20 \
|
||||
postgres:16-alpine
|
||||
# 使用容器IP直连,不依赖宿主端口映射(部分runner的DinD端口映射不稳定)
|
||||
PG_HOST=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$PG_CONTAINER")
|
||||
PG_PORT=5432
|
||||
echo "PostgreSQL host: $PG_HOST:$PG_PORT"
|
||||
export DATABASE_URL=postgresql+psycopg://postgres:postgres@$PG_HOST:$PG_PORT/xiaoxia_saas
|
||||
|
||||
# 等待PG容器内部ready
|
||||
for i in $(seq 1 30); do
|
||||
if docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" 2>/dev/null | grep -q healthy; then
|
||||
echo "PostgreSQL is ready on $PG_HOST:$PG_PORT"
|
||||
echo "PostgreSQL container is healthy"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for PostgreSQL... ($i/30)"
|
||||
@@ -198,16 +196,39 @@ for i in $(seq 1 30); do
|
||||
done
|
||||
docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" | grep -q healthy
|
||||
|
||||
# 验证:通过容器IP直接连通
|
||||
for i in $(seq 1 30); do
|
||||
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$PG_HOST', $PG_PORT)); s.close()" 2>/dev/null; then
|
||||
echo "TCP connectivity to PostgreSQL confirmed on $PG_HOST:$PG_PORT"
|
||||
break
|
||||
fi
|
||||
echo "Waiting for TCP connectivity to PostgreSQL on $PG_HOST:$PG_PORT... ($i/30)"
|
||||
sleep 2
|
||||
# 获取两种连接方式
|
||||
PG_MAPPED_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2)
|
||||
PG_CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$PG_CONTAINER")
|
||||
echo "Mapped port: $PG_MAPPED_PORT, Container IP: $PG_CONTAINER_IP"
|
||||
|
||||
# 双模式探测:先试端口映射,再试容器IP,哪个通就用哪个
|
||||
PG_HOST=""
|
||||
PG_PORT=""
|
||||
for mode in "mapped:127.0.0.1:$PG_MAPPED_PORT" "container:$PG_CONTAINER_IP:5432"; do
|
||||
MODE_NAME=$(echo "$mode" | cut -d: -f1)
|
||||
TEST_HOST=$(echo "$mode" | cut -d: -f2)
|
||||
TEST_PORT=$(echo "$mode" | cut -d: -f3)
|
||||
echo "Testing $MODE_NAME mode ($TEST_HOST:$TEST_PORT)..."
|
||||
for i in $(seq 1 15); do
|
||||
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$TEST_HOST', $TEST_PORT)); s.close()" 2>/dev/null; then
|
||||
PG_HOST="$TEST_HOST"
|
||||
PG_PORT="$TEST_PORT"
|
||||
echo "✅ $MODE_NAME mode works: $TEST_HOST:$TEST_PORT"
|
||||
break 2
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
echo "❌ $MODE_NAME mode failed after 15 attempts, trying next..."
|
||||
done
|
||||
python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$PG_HOST', $PG_PORT)); s.close()"
|
||||
|
||||
if [ -z "$PG_HOST" ]; then
|
||||
echo "ERROR: 无法通过任何方式连接到PostgreSQL容器"
|
||||
echo "端口映射和容器IP两种方式均失败,可能是当前runner的Docker网络配置异常"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Using PostgreSQL: $PG_HOST:$PG_PORT"
|
||||
export DATABASE_URL="postgresql+psycopg://postgres:postgres@$PG_HOST:$PG_PORT/xiaoxia_saas"
|
||||
|
||||
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
|
||||
echo "✅ Alembic migrations applied successfully"
|
||||
|
||||
Reference in New Issue
Block a user