From e9256b09a39d91a88093619f8434a433d5d91267 Mon Sep 17 00:00:00 2001 From: OG T Date: Fri, 10 Apr 2026 12:06:37 +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=20postgres=20IP=20=E8=A7=A3=E6=9E=90=E7=A9=A9?= =?UTF-8?q?=E5=AE=9A=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 問題: docker inspect 多網路時 {{range}} 拼接多個 IP → asyncpg 逾時 修正: 用 python3 json 解析取第一個網路 IP, container name 動態查詢 (filter name=postgres-test), fallback 到 127.0.0.1:15432 (host exposed port) Co-Authored-By: Claude Sonnet 4.6 --- .gitea/workflows/cd.yaml | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/cd.yaml b/.gitea/workflows/cd.yaml index 52949ecd..5e53aa62 100644 --- a/.gitea/workflows/cd.yaml +++ b/.gitea/workflows/cd.yaml @@ -133,11 +133,22 @@ 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 (runner 在 docker 內,127.0.0.1 不可靠) - PG_IP=$(docker inspect api-postgres-test-1 \ - --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}') - echo "postgres-test IP: $PG_IP" - TEST_DATABASE_URL="postgresql+asyncpg://awoooi:awoooi_test_2026@${PG_IP}:5432/awoooi_test?ssl=disable" \ + # 取得 postgres container IP: 找 api_ 開頭的 compose 網路,取第一個有效 IP + # docker inspect 多網路時會拼接,用 json + jq 確保只取一個 + 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" \ /opt/api-venv/bin/pytest tests/integration/test_b5_core_flows.py -v --tb=short TEST_EXIT=$? docker compose -f docker-compose.test.yml down -v 2>/dev/null || true