Add optional database for tests
This commit is contained in:
47
.github/workflows/python-uv-ci.yml
vendored
47
.github/workflows/python-uv-ci.yml
vendored
@@ -36,6 +36,14 @@ on:
|
|||||||
test_command:
|
test_command:
|
||||||
type: string
|
type: string
|
||||||
default: "uv run pytest"
|
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:
|
secrets:
|
||||||
ssh_private_key:
|
ssh_private_key:
|
||||||
required: false
|
required: false
|
||||||
@@ -119,6 +127,45 @@ jobs:
|
|||||||
if: ${{ inputs.typecheck_command != '' }}
|
if: ${{ inputs.typecheck_command != '' }}
|
||||||
run: ${{ 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
|
- name: Run tests
|
||||||
if: ${{ inputs.test_command != '' }}
|
if: ${{ inputs.test_command != '' }}
|
||||||
run: ${{ inputs.test_command }}
|
run: ${{ inputs.test_command }}
|
||||||
|
|||||||
Reference in New Issue
Block a user