diff --git a/scripts/ci/run_integration_tests.sh b/scripts/ci/run_integration_tests.sh index eef2e5104..b7fd23b42 100755 --- a/scripts/ci/run_integration_tests.sh +++ b/scripts/ci/run_integration_tests.sh @@ -52,70 +52,45 @@ bash scripts/ci/step_install_ffmpeg.sh # 需要用宿主机IP访问映射端口 # 检测策略:host.docker.internal -> docker0桥接IP -> 容器IP直连 -> 默认网关 -> 127.0.0.1 detect_docker_host() { - local test_port="${1:-${CI_LOCAL_PG_PORT}}" - - # 候选IP列表 - local candidates=() + # 目标:找到宿主机IP(DooD模式下CI容器访问宿主机上其他容器用) + # 不依赖特定端口TCP探测,直接用网络拓扑信息 # 1. host.docker.internal(runner配置了--add-host时可用) if python3 -c "import socket; socket.gethostbyname('host.docker.internal')" 2>/dev/null; then - candidates+=("host.docker.internal") + echo "host.docker.internal" + return 0 fi - # 2. docker0 桥接网关 (172.17.0.1) - candidates+=("172.17.0.1") - - # 3. 默认网关(容器网络的网关即宿主机) + # 2. 默认网关(Docker bridge模式下网关即宿主机) local gw="" gw=$(ip route 2>/dev/null | grep default | awk '{print $3}' | head -1) if [ -n "$gw" ] && [ "$gw" != "127.0.0.1" ]; then - candidates+=("$gw") + echo "$gw" + return 0 fi - # 4. 宿主机可能的IP:容器同网段的.1或.254 - local my_ip="" - my_ip=$(hostname -I 2>/dev/null | awk '{print $1}') - if [ -n "$my_ip" ]; then - # 尝试同网段的常见宿主机IP - local subnet=$(echo "$my_ip" | cut -d. -f1-3) - candidates+=("${subnet}.1") - candidates+=("${subnet}.254") + # 3. docker0 桥接网关 + if [ -n "$(ip addr show docker0 2>/dev/null)" ]; then + echo "172.17.0.1" + return 0 fi - # 5. 127.0.0.1 最后尝试 - candidates+=("127.0.0.1") + # 4. 通过 git server hostname 反查(runner 配置了 ExtraHosts host-gateway) + local git_host_ip="" + git_host_ip=$(python3 -c "import socket; print(socket.gethostbyname('git.xiaoxiajianji.com'))" 2>/dev/null || true) + if [ -n "$git_host_ip" ] && [ "$git_host_ip" != "127.0.0.1" ]; then + echo "$git_host_ip" + return 0 + fi - # 测试每个候选IP - for candidate in "${candidates[@]}"; do - if python3 -c " -import socket -s = socket.socket() -s.settimeout(2) -try: - s.connect(('$candidate', $test_port)) - s.close() - print('ok') -except: - pass -" 2>/dev/null | grep -q ok; then - echo "$candidate" - return 0 - fi - done - - # 都失败则返回127.0.0.1 + # 5. 最终 fallback echo "127.0.0.1" return 0 } # 获取宿主机IP(先尝试用共享PG端口5433测试,再回退到其他端口) if [ -S /var/run/docker.sock ]; then - # 先用共享PG端口5433探测 - DOCKER_HOST_IP=$(detect_docker_host "${CI_SHARED_PG_PORT}") - if [ "$DOCKER_HOST_IP" = "127.0.0.1" ]; then - # 如果共享PG端口探测失败,说明不在DooD或共享PG不可用,再试其他端口 - DOCKER_HOST_IP=$(detect_docker_host 22) - fi + DOCKER_HOST_IP=$(detect_docker_host) echo "检测到DooD模式(/var/run/docker.sock已挂载),宿主机地址: $DOCKER_HOST_IP" else DOCKER_HOST_IP="127.0.0.1"