fix(ci): B5 改在 compose 網路內的臨時 container 跑 pytest
Some checks failed
CD Pipeline / build-and-deploy (push) Failing after 1m54s

根本問題: runner DinD 環境無法直連 compose 內部網路
解法: docker run --network api_default 讓 pytest container 與 postgres-test 同網路
      用 hostname postgres-test:5432 直連,不需要 port binding 或 IP 操作

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-10 12:44:43 +08:00
parent c589cc6966
commit 3ebfca62a2

View File

@@ -129,16 +129,23 @@ jobs:
run: |
cd apps/api
docker compose -f docker-compose.test.yml up -d --wait
# psql 從 postgres container 執行,不需要主機安裝 psql
# schema 初始化
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 內可直連 container IP
CONTAINER_NAME=$(docker ps --filter name=postgres-test --format '{{.Names}}' | head -1)
PG_IP=$(docker inspect "$CONTAINER_NAME" \
--format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' | awk '{print $1}')
echo "postgres-test: $CONTAINER_NAME @ $PG_IP:5432"
TEST_DATABASE_URL="postgresql+asyncpg://awoooi:awoooi_test_2026@${PG_IP}:5432/awoooi_test?ssl=disable" \
# compose 網路名 (目錄名=api → api_default)
COMPOSE_NET=$(docker network ls --format '{{.Name}}' | grep '^api_default$' || \
docker network ls --format '{{.Name}}' | grep 'default' | head -1)
echo "compose network: $COMPOSE_NET"
# 在同一 compose 網路的臨時 python container 內跑 pytest
# 掛載原始碼 + venvhostname=postgres-test 直連 5432
docker run --rm \
--network "$COMPOSE_NET" \
-v /opt/api-venv:/opt/api-venv \
-v "$(pwd):/workspace" \
-w /workspace \
-e TEST_DATABASE_URL="postgresql+asyncpg://awoooi:awoooi_test_2026@postgres-test:5432/awoooi_test?ssl=disable" \
python:3.11-slim \
/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