Skip to content

Commit bb1a08c

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

2 files changed

Lines changed: 86 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: 54 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,36 @@ 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+
run: |
96+
supported_versions=("13" "14" "17")
97+
if [[ ! " ${supported_versions[*]} " == *" ${{ inputs.postgres-major-version }} "* ]]; then
98+
echo "Error: Unsupported PostgreSQL major version provided. Supported versions are 13, 14 and 17."
99+
exit 1
100+
fi
101+
63102
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
64103

65104
- name: Set up Python ${{ matrix.python_version }}
@@ -79,6 +118,21 @@ jobs:
79118
${{ runner.os }}-build-
80119
${{ runner.os }}-
81120
121+
- name: Install database system packages
122+
if: ${{ inputs.postgres-major-version != '' }}
123+
run: |
124+
sudo apt-get update
125+
sudo apt-get install libpq-dev
126+
127+
- name: Install PostGIS system packages
128+
if: ${{ inputs.use-postgis }}
129+
run: |
130+
sudo apt-get install gdal-bin
131+
132+
- name: Set database URL
133+
if: ${{ inputs.postgres-major-version != '' }}
134+
run: echo "DATABASE_URL=${{ inputs.use-postgis == true && 'postgis' || 'postgres' }}://test_user:test_password@localhost/test_db" >> $GITHUB_ENV
135+
82136
- name: Install hatch
83137
run: python -m pip install hatch
84138

0 commit comments

Comments
 (0)