diff --git a/.gitea/workflows/ci-cd.yml b/.gitea/workflows/ci-cd.yml old mode 100755 new mode 100644 index fb6388924..d6bc3003a --- a/.gitea/workflows/ci-cd.yml +++ b/.gitea/workflows/ci-cd.yml @@ -19,21 +19,6 @@ jobs: name: Validate Code Quality And Tests runs-on: ubuntu-22.04 - services: - postgres: - image: postgres:16 - env: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - POSTGRES_DB: xiaoxia_saas - ports: - - 5433:5432 - options: >- - --health-cmd "pg_isready -U postgres" - --health-interval 10s - --health-timeout 5s - --health-retries 5 - env: DATABASE_URL: postgresql+psycopg://postgres:postgres@127.0.0.1:5433/xiaoxia_saas USE_IN_MEMORY_DB: "false" @@ -139,6 +124,34 @@ jobs: set -eu PYTHONPATH="$PWD/apps/api:$PWD" python3 -m pytest tests/unit -q + - name: Start PostgreSQL for integration tests + shell: sh + run: | + set -eu + # 清理可能残留的旧容器 + docker rm -f ci-pg-validate 2>/dev/null || true + # 启动 PG 容器 + docker run -d --name ci-pg-validate \ + -e POSTGRES_USER=postgres \ + -e POSTGRES_PASSWORD=postgres \ + -e POSTGRES_DB=xiaoxia_saas \ + -p 5433:5432 \ + --health-cmd "pg_isready -U postgres" \ + --health-interval 5s \ + --health-timeout 5s \ + --health-retries 12 \ + postgres:16 + # 等待健康检查通过 + for i in $(seq 1 30); do + if docker inspect --format='{{.State.Health.Status}}' ci-pg-validate 2>/dev/null | grep -q healthy; then + echo "PostgreSQL is ready" + break + fi + echo "Waiting for PostgreSQL... ($i/30)" + sleep 2 + done + docker inspect --format='{{.State.Health.Status}}' ci-pg-validate | grep -q healthy + - name: Apply migrations for integration tests shell: sh run: | @@ -151,6 +164,13 @@ jobs: set -eu PYTHONPATH="$PWD/apps/api:$PWD" python3 -m pytest tests/integration -q --timeout=60 -x + - name: Cleanup PostgreSQL + if: always() + shell: sh + run: | + docker rm -f ci-pg-validate 2>/dev/null || true + echo "PostgreSQL container cleaned up" + - name: Build summary if: github.ref == 'refs/heads/develop' || github.ref == 'refs/heads/main' shell: sh