Split python UV workflow with DB service

This commit is contained in:
estebanthi
2026-01-05 14:43:07 +01:00
parent 4ebffa8c94
commit c5563d9dc1
2 changed files with 138 additions and 78 deletions

View File

@@ -36,14 +36,6 @@ 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
@@ -127,76 +119,6 @@ jobs:
if: ${{ inputs.typecheck_command != '' }}
run: ${{ inputs.typecheck_command }}
- name: Cleanup existing database containers
if: ${{ inputs.enable_db }}
run: |
existing_ids="$(docker ps -aq --filter "name=^ci-postgres")"
if [ -n "$existing_ids" ]; then
docker rm -f $existing_ids >/dev/null 2>&1 || true
fi
- name: Start database
if: ${{ inputs.enable_db }}
run: |
set -euo pipefail
db_type="${{ inputs.db_type }}"
case "$db_type" in
postgres|postgresql)
run_id="${GITHUB_RUN_ID:-local}"
attempt="${GITHUB_RUN_ATTEMPT:-0}"
container_name="ci-postgres-${run_id}-${attempt}"
echo "DB_CONTAINER_NAME=$container_name" >> "$GITHUB_ENV"
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
health="$(docker inspect --format '{{.State.Health.Status}}' "$container_name" 2>/dev/null || true)"
case "$health" in
healthy)
break
;;
unhealthy)
echo "Postgres reported unhealthy." >&2
docker logs "$container_name" || true
exit 1
;;
"")
echo "Postgres health status unavailable." >&2
docker logs "$container_name" || true
exit 1
;;
esac
sleep 1
done
if [ "${health:-}" != "healthy" ]; then
echo "Postgres did not become healthy 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 }}
- name: Cleanup database
if: ${{ always() && inputs.enable_db }}
run: |
if [ -n "${DB_CONTAINER_NAME:-}" ]; then
docker rm -f "$DB_CONTAINER_NAME" >/dev/null 2>&1 || true
fi