Files
xiaoxia-saas/scripts/ci/run_integration_tests.sh
T
CI Bot 069e4ee518
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web 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 Web Image (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (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 18s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m7s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 3m51s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 4m11s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 21s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 4m16s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 4m37s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m43s
AI Code Review / AI Code Review (pull_request) Successful in 7m11s
fix(ci): PG/Redis改用host网络模式,彻底解决Docker端口映射/网桥不稳定问题
不同runner实例的Docker网络配置差异巨大:
- new-4/new-10: 端口映射正常,容器IP不通
- new-9: 端口映射失效
- new-11: PG容器根本起不来(网桥问题)

经过多轮尝试(端口映射→容器IP→双模式fallback),最终改用
host网络模式,PG用15432端口,Redis用16379端口,直接共享宿主
网络栈,完全绕过Docker网桥和端口映射,从根本上解决所有
runner环境的网络连通性问题。

修改文件:
- scripts/ci/run_validate.sh: PG改用host网络+15432端口
- scripts/ci/run_integration_tests.sh: PG+Redis均改用host网络
2026-07-19 23:03:38 +08:00

160 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
# CI Integration Tests Job 主脚本
# 包含:依赖安装、ffmpeg安装、Redis启动、PG启动、迁移、测试、清理、覆盖率
set -eu
echo "=== CI Integration Tests 开始 ==="
# --- 安装依赖 ---
echo ""
echo "=== 安装 Python 依赖 ==="
python3 -m pip install -q -r requirements-base.txt
python3 -m pip install -q -r requirements.txt
python3 -m pip install -q -r requirements-dev.txt
python3 -m pip install -q pytest-rerunfailures
pytest --version
# --- 安装 ffmpeg ---
echo ""
echo "=== 安装 ffmpeg ==="
bash scripts/ci/step_install_ffmpeg.sh
# --- 启动 Redis ---
echo ""
echo "=== 启动 Redis ==="
# 使用host网络模式,非默认端口避免冲突
REDIS_PORT=16379
PG_PORT=15432
REDIS_CONTAINER="ci-redis-${GITHUB_RUN_ID:-$$}"
docker rm -f "$REDIS_CONTAINER" 2>/dev/null || true
docker run -d --name "$REDIS_CONTAINER" \
--network=host \
--health-cmd "redis-cli -p $REDIS_PORT ping" \
--health-interval 2s \
--health-timeout 2s \
--health-retries 10 \
redis:7-alpine \
redis-server --port $REDIS_PORT
# 等Redis就绪
for i in $(seq 1 20); do
HEALTH=$(docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" 2>/dev/null || echo "starting")
if echo "$HEALTH" | grep -q healthy; then
echo "Redis is healthy on host port $REDIS_PORT"
break
fi
echo "Waiting for Redis... ($i/20) health=$HEALTH"
sleep 2
done
docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" | grep -q healthy
# 验证连通
REDIS_HOST="127.0.0.1"
for i in $(seq 1 10); 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 "✅ Redis connection confirmed"
break
fi
sleep 2
done
python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$REDIS_HOST', $REDIS_PORT)); s.close()"
export REDIS_URL="redis://$REDIS_HOST:$REDIS_PORT/0"
# --- 启动 PostgreSQL ---
echo ""
echo "=== 启动 PostgreSQL ==="
PG_CONTAINER="ci-pg-${GITHUB_RUN_ID:-$$}"
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
docker run -d --name "$PG_CONTAINER" \
--network=host \
--shm-size=256m \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=xiaoxia_saas \
-e PGPORT=$PG_PORT \
--health-cmd "pg_isready -U postgres -p $PG_PORT" \
--health-interval 3s \
--health-timeout 3s \
--health-retries 20 \
postgres:16
# 等PG就绪
for i in $(seq 1 40); do
HEALTH=$(docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" 2>/dev/null || echo "starting")
if echo "$HEALTH" | grep -q healthy; then
echo "PostgreSQL is healthy on host port $PG_PORT"
break
fi
echo "Waiting for PostgreSQL... ($i/40) health=$HEALTH"
sleep 2
done
docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" | grep -q healthy
# 验证连通
PG_HOST="127.0.0.1"
for i in $(seq 1 15); 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 "✅ PostgreSQL connection confirmed"
break
fi
sleep 2
done
python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$PG_HOST', $PG_PORT)); s.close()"
export DATABASE_URL="postgresql+psycopg://postgres:postgres@$PG_HOST:$PG_PORT/xiaoxia_saas"
# --- 执行迁移 ---
echo ""
echo "=== 执行 Alembic 迁移 ==="
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
echo "✅ 迁移完成"
# --- 运行集成测试 ---
echo ""
echo "=== 运行集成测试 ==="
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m coverage run \
--source=apps/api/app,packages \
--omit="*/migrations/*,*/tests/*,*/test_*.py,*/site-packages/*" \
--branch \
-m pytest tests/integration -q --timeout=60 -x --reruns 2 --reruns-delay 1 -m "not performance"
python3 -m coverage report --show-missing
python3 -m coverage xml -o coverage.xml
python3 -m coverage report --fail-under=40 > /dev/null
echo "✅ 集成测试通过"
# --- API 性能基线测试(仅告警) ---
echo ""
echo "=== API 性能基线测试(仅告警) ==="
set +e
PERF_OUTPUT=$(mktemp)
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m pytest tests/integration/test_api_performance.py \
-v --timeout=120 -p no:cacheprovider 2>&1 | tee "$PERF_OUTPUT"
echo ""
echo "=== 性能测试摘要 ==="
grep "PERF_STATS:" "$PERF_OUTPUT" || echo "PERF_STATS: 未找到统计数据"
grep "PERF_RESULT:" "$PERF_OUTPUT" || echo "PERF_RESULT: 未找到详细结果"
TOTAL=$(grep -c "PERF_RESULT:" "$PERF_OUTPUT" || echo 0)
PASSED=$(grep "PERF_RESULT: PASS" "$PERF_OUTPUT" | wc -l)
FAILED=$(grep "PERF_RESULT: FAIL" "$PERF_OUTPUT" | wc -l)
echo ""
echo "性能测试结果: $PASSED/$TOTAL 通过, $FAILED 未达标"
if [ "$FAILED" -gt 0 ]; then
echo ""
echo "⚠️ 警告: $FAILED 个接口性能未达标"
fi
rm -f "$PERF_OUTPUT"
set -e
# --- 清理 ---
echo ""
echo "=== 清理容器 ==="
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
docker rm -f "$REDIS_CONTAINER" 2>/dev/null || true
echo "✅ 清理完成"
# --- 覆盖率汇总 ---
echo ""
echo "=== 覆盖率汇总 ==="
set +e
python3 scripts/ci_coverage_summary.py
set -e
echo ""
echo "=== CI Integration Tests 全部通过 ✅ ==="