diff --git a/.github/workflows/python-uv-ci.yml b/.github/workflows/python-uv-ci.yml index 030911e..d6b7bdc 100644 --- a/.github/workflows/python-uv-ci.yml +++ b/.github/workflows/python-uv-ci.yml @@ -36,6 +36,14 @@ on: test_command: type: string default: "uv run pytest" + enable_db: + description: Enable a local database container for tests. + type: boolean + default: false + db_type: + description: Database type to start when enable_db is true. + type: string + default: "postgres" secrets: ssh_private_key: required: false @@ -119,6 +127,45 @@ jobs: if: ${{ inputs.typecheck_command != '' }} run: ${{ inputs.typecheck_command }} + - name: Start database + if: ${{ inputs.enable_db }} + run: | + set -euo pipefail + db_type="${{ inputs.db_type }}" + case "$db_type" in + postgres|postgresql) + container_name="ci-postgres" + docker run -d --name "$container_name" \ + -e POSTGRES_USER=postgres \ + -e POSTGRES_PASSWORD=postgres \ + -e POSTGRES_DB=test_db \ + -p 5432:5432 \ + --health-cmd="pg_isready -U postgres" \ + --health-interval=10s \ + --health-timeout=5s \ + --health-retries=5 \ + postgres:16 + for i in {1..30}; do + if docker exec "$container_name" pg_isready -U postgres > /dev/null 2>&1; then + break + fi + sleep 1 + done + if ! docker exec "$container_name" pg_isready -U postgres > /dev/null 2>&1; then + echo "Postgres did not become ready in time." >&2 + docker logs "$container_name" || true + exit 1 + fi + if [ -z "${DATABASE_URL:-}" ]; then + echo "DATABASE_URL=postgresql://postgres:postgres@localhost:5432/test_db" >> "$GITHUB_ENV" + fi + ;; + *) + echo "Unsupported db_type: $db_type" >&2 + exit 1 + ;; + esac + - name: Run tests if: ${{ inputs.test_command != '' }} run: ${{ inputs.test_command }}