From cd509192599c0e9843330af0e772f89b241aaa5d Mon Sep 17 00:00:00 2001 From: OG T Date: Fri, 10 Apr 2026 12:16:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(ci):=20B5=20=E6=95=B4=E5=90=88=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6=20=E2=80=94=20runner=20=E5=8A=A0=E5=85=A5=20compose?= =?UTF-8?q?=20=E7=B6=B2=E8=B7=AF=E6=89=8D=E8=83=BD=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E5=88=B0=20postgres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 問題: act runner 在 Docker 容器內,compose 網路 172.30.0.x 無法直接路由 修正: docker network connect 讓 runner 加入 api_default compose 網路, 測試後 disconnect 清理 Co-Authored-By: Claude Sonnet 4.6 --- .gitea/workflows/cd.yaml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.gitea/workflows/cd.yaml b/.gitea/workflows/cd.yaml index 5e53aa62..cf935f1b 100644 --- a/.gitea/workflows/cd.yaml +++ b/.gitea/workflows/cd.yaml @@ -133,24 +133,24 @@ jobs: docker compose -f docker-compose.test.yml exec -T postgres-test \ psql -U awoooi -d awoooi_test \ -f /dev/stdin < tests/integration/setup_test_schema.sql - # 取得 postgres container IP: 找 api_ 開頭的 compose 網路,取第一個有效 IP - # docker inspect 多網路時會拼接,用 json + jq 確保只取一個 + # Runner 在 Docker 容器內,需加入 compose 網路才能連到 postgres-test + # compose 預設網路名: {project_name}_default,project = 目錄名 = api + COMPOSE_NETWORK=$(docker network ls --filter name=api --format '{{.Name}}' | grep default | head -1) + RUNNER_CONTAINER=$(hostname) + echo "compose network: $COMPOSE_NETWORK, runner: $RUNNER_CONTAINER" + if [ -n "$COMPOSE_NETWORK" ]; then + docker network connect "$COMPOSE_NETWORK" "$RUNNER_CONTAINER" 2>/dev/null || true + fi + # 取 postgres container IP (compose 網路內) CONTAINER_NAME=$(docker ps --filter name=postgres-test --format '{{.Names}}' | head -1) - echo "postgres-test container: $CONTAINER_NAME" PG_IP=$(docker inspect "$CONTAINER_NAME" \ --format '{{json .NetworkSettings.Networks}}' \ | python3 -c "import sys,json; nets=json.load(sys.stdin); print(list(nets.values())[0]['IPAddress'])" 2>/dev/null) - # fallback: host 上 exposed port 15432 - if [ -z "$PG_IP" ]; then - PG_IP="127.0.0.1" - PG_PORT="15432" - else - PG_PORT="5432" - fi - echo "postgres-test IP: $PG_IP:$PG_PORT" - TEST_DATABASE_URL="postgresql+asyncpg://awoooi:awoooi_test_2026@${PG_IP}:${PG_PORT}/awoooi_test?ssl=disable" \ + echo "postgres-test: $CONTAINER_NAME @ $PG_IP:5432" + TEST_DATABASE_URL="postgresql+asyncpg://awoooi:awoooi_test_2026@${PG_IP}:5432/awoooi_test?ssl=disable" \ /opt/api-venv/bin/pytest tests/integration/test_b5_core_flows.py -v --tb=short TEST_EXIT=$? + docker network disconnect "$COMPOSE_NETWORK" "$RUNNER_CONTAINER" 2>/dev/null || true docker compose -f docker-compose.test.yml down -v 2>/dev/null || true exit $TEST_EXIT