Skip to content

Commit ff74b34

Browse files
committed
feat: add postgres option for ci-python-library
Refs: RATY-348
1 parent 4de6bb1 commit ff74b34

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

.github/workflows/ci-python-library-README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This reusable workflow is part of the City of Helsinki's GitHub Actions setup, s
77
- **Commit Linting**: Enforces commit message standards using [commitlint](https://commitlint.js.org/).
88
- **Code Style Checks**: Verifies code style and formatting using [pre-commit](https://pre-commit.com/).
99
- **Automated Testing**: Runs project tests across multiple Python versions using [hatch](https://hatch.pypa.io/).
10+
- **Database Testing**: Optionally starts a PostgreSQL (with or without PostGIS) service and sets `DATABASE_URL` for tests that require a database.
1011
- **Code Quality Analysis**: Performs a [SonarQube Cloud](https://sonarcloud.io/) scan.
1112

1213
## Requirements for Projects Using the Workflow
@@ -28,6 +29,8 @@ To use this reusable workflow, create a project-specific workflow file in your `
2829

2930
- **`python-version`** (string): Python version to use for pre-commit checks. Defaults to `"3.x"`.
3031
- **`enable-sonar`** (boolean): Whether to run the SonarQube Cloud Scan job after tests. Defaults to `true`.
32+
- **`postgres-major-version`** (string): PostgreSQL major version to use for testing. Supported versions: `13`, `14`, `17`. Optional - omit if no database is needed.
33+
- **`use-postgis`** (boolean): Set to `true` to use the PostGIS extension. Requires `postgres-major-version` to be set. Defaults to `false`.
3134

3235
### Secrets
3336

@@ -59,3 +62,32 @@ jobs:
5962
with:
6063
python-matrix: "['3.10', '3.11', '3.12', '3.13', '3.14']"
6164
```
65+
66+
### Example usage with PostgreSQL (`<own project>/.github/workflows/ci.yml`)
67+
68+
```yaml
69+
name: CI
70+
71+
on:
72+
push:
73+
branches: [main]
74+
pull_request:
75+
branches: [main]
76+
workflow_dispatch:
77+
78+
concurrency:
79+
group: ${{ github.workflow }}-${{ github.ref }}
80+
cancel-in-progress: true
81+
82+
jobs:
83+
common:
84+
uses: City-of-Helsinki/.github/.github/workflows/ci-python-library.yml@main
85+
secrets:
86+
sonar-token: ${{ secrets.SONAR_TOKEN }}
87+
github-token: ${{ secrets.GITHUB_TOKEN }}
88+
with:
89+
python-matrix: "['3.12', '3.13']"
90+
postgres-major-version: "17"
91+
```
92+
93+
When `postgres-major-version` is set, the workflow starts a PostgreSQL service and exposes a `DATABASE_URL` environment variable (`postgres://test_user:test_password@localhost/test_db`) to the test runner.

.github/workflows/ci-python-library.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ on:
1717
required: false
1818
type: boolean
1919
default: true
20+
postgres-major-version:
21+
description: "PostgreSQL major version to use for testing (supported: 13, 14, 17). Optional - omit if no database is needed."
22+
required: false
23+
type: string
24+
default: ''
25+
use-postgis:
26+
description: "Set to true to use the PostGIS extension. Requires postgres-major-version to be set."
27+
required: false
28+
type: boolean
29+
default: false
2030
secrets:
2131
sonar-token:
2232
description: "Token for SonarQube Cloud Scan. Required if enable-sonar is set to true."
@@ -59,7 +69,38 @@ jobs:
5969
matrix:
6070
python_version: ${{ fromJSON(inputs.python-matrix) }}
6171

72+
services:
73+
postgres:
74+
# The workflow supports only the PostgreSQL and PostGIS versions used in Platta
75+
image: ${{ inputs.postgres-major-version != '' &&
76+
(inputs.use-postgis &&
77+
(inputs.postgres-major-version == '17' && 'postgis/postgis:17-3.5-alpine' ||
78+
inputs.postgres-major-version == '14' && 'postgis/postgis:14-3.2-alpine' ||
79+
inputs.postgres-major-version == '13' && 'postgis/postgis:13-3.2-alpine') ||
80+
(inputs.postgres-major-version == '17' && 'postgres:17-alpine' ||
81+
inputs.postgres-major-version == '14' && 'postgres:14-alpine' ||
82+
inputs.postgres-major-version == '13' && 'postgres:13-alpine')) ||
83+
'postgres:alpine' }} # This fallback is used only so that we don't error out before the validation step
84+
env:
85+
POSTGRES_USER: test_user
86+
POSTGRES_PASSWORD: test_password
87+
POSTGRES_DB: test_db
88+
ports:
89+
- 5432:5432
90+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
91+
6292
steps:
93+
- name: Validate provided PostgreSQL major version
94+
if: ${{ inputs.postgres-major-version != '' }}
95+
env:
96+
POSTGRES_MAJOR_VERSION: ${{ inputs.postgres-major-version }}
97+
run: |
98+
supported_versions=("13" "14" "17")
99+
if [[ ! " ${supported_versions[*]} " == *" ${POSTGRES_MAJOR_VERSION} "* ]]; then
100+
echo "Error: Unsupported PostgreSQL major version provided. Supported versions are 13, 14 and 17."
101+
exit 1
102+
fi
103+
63104
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
64105

65106
- name: Set up Python ${{ matrix.python_version }}
@@ -79,6 +120,22 @@ jobs:
79120
${{ runner.os }}-build-
80121
${{ runner.os }}-
81122
123+
- name: Install system packages
124+
if: ${{ inputs.postgres-major-version != '' }}
125+
env:
126+
USE_POSTGIS: ${{ inputs.use-postgis }}
127+
run: |
128+
packages=(libpq-dev)
129+
if [[ "${USE_POSTGIS}" == "true" ]]; then
130+
packages+=(gdal-bin)
131+
fi
132+
sudo apt-get update
133+
sudo apt-get install -y "${packages[@]}"
134+
135+
- name: Set database URL
136+
if: ${{ inputs.postgres-major-version != '' }}
137+
run: echo "DATABASE_URL=${{ inputs.use-postgis == true && 'postgis' || 'postgres' }}://test_user:test_password@localhost/test_db" >> $GITHUB_ENV
138+
82139
- name: Install hatch
83140
run: python -m pip install hatch
84141

0 commit comments

Comments
 (0)