Skip to content

Commit 5183a5e

Browse files
Allow set PostGIS version(windows only) and postgres version (#14)
- [x] Allow set postgres version - [x] Allow set postgis version, for windows only - [x] Setup postgres first before setup postgis - [x] Skip exist files when copying postgis files on windows - [x] Not restart postgres after postgis installation --------- Co-authored-by: Yuri Astrakhan <YuriAstrakhan@gmail.com>
1 parent bf0ce8e commit 5183a5e

File tree

3 files changed

+104
-46
lines changed

3 files changed

+104
-46
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ jobs:
6666
password: GrandMaster
6767
database: jedi_order
6868
port: 34837
69+
postgres-version: 17
70+
postgis_version: 3.5.0
6971
id: postgres
7072

7173
- name: Test PostGIS with connection URI
@@ -75,3 +77,8 @@ jobs:
7577
run: psql -v ON_ERROR_STOP=1 -c 'SELECT PostGIS_Full_Version();'
7678
env:
7779
PGSERVICE: ${{ steps.postgres.outputs.service-name }}
80+
81+
- name: Verify PostGIS 3.5.0 installation on Windows
82+
if: matrix.os == 'windows-latest'
83+
run: |
84+
psql -v ON_ERROR_STOP=1 -c "SELECT PostGIS_Full_Version();" "${{ steps.postgres.outputs.connection-uri }}" | findstr /C:"POSTGIS=3.5.0 3.5.0"

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ steps:
3131
3232
#### Input parameters
3333
34-
| Key | Value | Default |
35-
|------------|------------------------------------------------------------------------------------------------------|-------------|
36-
| username | The username of the user to setup. | `postgres` |
37-
| password | The password of the user to setup. | `postgres` |
38-
| database | The database name to setup and grant permissions to created user. | `postgres` |
39-
| port | The server port to listen on. | `5432` |
40-
| cached-dir | Where should the temporary downloads be placed. Used to download and cache PostGIS binary. | `downloads` |
34+
| Key | Value | Default |
35+
|------------------|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
36+
| username | The username of the user to setup. | `postgres` |
37+
| password | The password of the user to setup. | `postgres` |
38+
| database | The database name to setup and grant permissions to created user. | `postgres` |
39+
| port | The server port to listen on. | `5432` |
40+
| postgres-version | The PostgreSQL version to install. | `17` |
41+
| postgis_version | **(Windows only)** The PostGIS version to installed. | By default (empty), will use the latest. See available versions [here](https://download.osgeo.org/postgis/windows/). If set, must use the entire version string like `3.3.3` |
42+
| cached-dir | Where should the temporary downloads be placed. Used to download and cache PostGIS binary. | `downloads` |
4143

4244
#### Outputs
4345

action.yml

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ inputs:
2121
description: The server port to listen on.
2222
default: "5432"
2323
required: false
24+
postgres-version:
25+
description: The version of PostgreSQL to install.
26+
default: "17"
27+
required: false
28+
postgis_version:
29+
description: The version of PostGIS to install.
30+
default: ""
31+
required: false
2432
cached-dir:
2533
# TODO: move to runner's temp dir, help wanted
2634
description: Where should the temporary downloads be placed. Used to download and cache PostGIS binary.
@@ -36,50 +44,95 @@ outputs:
3644
runs:
3745
using: composite
3846
steps:
47+
- name: Setup PostgreSQL for Linux/macOS/Windows
48+
id: pg
49+
uses: ikalnytskyi/action-setup-postgres@v7
50+
with:
51+
username: "${{ inputs.username }}"
52+
password: "${{ inputs.password }}"
53+
database: "${{ inputs.database }}"
54+
port: "${{ inputs.port }}"
55+
postgres-version: "${{ inputs.postgres-version }}"
56+
3957
- name: Install PostGIS (Linux)
4058
if: runner.os == 'Linux'
4159
run: |
42-
# Detect installed PostgreSQL version
43-
PG_VERSION=$(apt list --installed | grep -E 'postgresql-[0-9.]+' | sed -rn 's/.*postgresql-([0-9.]+).*/\1/p')
44-
[[ "$PG_VERSION" =~ ^[0-9.]+$ ]] || (echo "Failed to detect Postgres version" && exit 1)
45-
echo "Detected PostgreSQL version: $PG_VERSION"
60+
# Detect installed PostgreSQL version (major only)
61+
PG_VERSION=$(postgres --version | awk '{print $3}' | cut -d. -f1)
62+
[[ "$PG_VERSION" =~ ^[0-9]+$ ]] || (echo "Failed to detect Postgres major version" && exit 1)
63+
echo "Detected PostgreSQL major version: $PG_VERSION"
4664
4765
# Install PostGIS
4866
REPO_URL="https://apt.postgresql.org/pub/repos/apt/"
4967
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql.gpg >/dev/null
5068
echo "deb [signed-by=/usr/share/keyrings/postgresql.gpg] $REPO_URL $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list >/dev/null
5169
sudo apt update
5270
sudo apt-get install postgresql-$PG_VERSION-postgis-3
53-
54-
# sudo apt install curl ca-certificates gnupg
55-
# curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null
56-
# sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
57-
# sudo apt update
58-
# sudo apt-get install postgis
5971
shell: bash
6072

61-
- uses: tecolicom/actions-use-homebrew-tools@v1
73+
- name: Install PostGIS on macOS
6274
if: runner.os == 'macOS'
63-
with:
64-
tools: 'postgis'
75+
shell: bash
76+
run: brew install postgis
6577

78+
- name: Detect PostgreSQL Version (Windows)
79+
if: runner.os == 'Windows'
80+
id: pg-version
81+
shell: pwsh
82+
run: |
83+
echo "PowerShell version: ${PSVersionTable.PSVersion}"
84+
# Get major version number (first 2 digits) from PostgreSQL server version number (format: XXYYZZ)
85+
$PG_VERSION = psql -t -A -c "SHOW server_version_num" '${{ steps.pg.outputs.connection-uri }}' | ForEach-Object { $_.Substring(0,2) }
86+
echo "Using PostgreSQL version: $PG_VERSION"
87+
Add-Content $env:GITHUB_OUTPUT "version=$PG_VERSION"
88+
89+
- name: Detect PGDATA directory (Windows)
90+
if: runner.os == 'Windows'
91+
id: pgdata
92+
shell: pwsh
93+
run: |
94+
$PGDATA = psql -t -A -c "SHOW data_directory;" '${{ steps.pg.outputs.connection-uri }}'
95+
echo "PGDATA directory: $PGDATA"
96+
echo "PGDATA=$PGDATA" >> $env:GITHUB_ENV
97+
6698
- name: Decide Postgis version (Windows)
67-
# Download the list of available Postgis versions, and decide which one to use
6899
if: runner.os == 'Windows'
69100
id: postgis-ver
70101
shell: pwsh
71102
run: |
72-
echo "PowerShell version: ${PSVersionTable.PSVersion}"
73-
$PG_VERSION = Split-Path $env:PGROOT -Leaf
74-
$postgis_page = "https://download.osgeo.org/postgis/windows/pg$PG_VERSION"
75-
echo "Detecting PostGIS version from $postgis_page for PostgreSQL $PG_VERSION"
76-
$pgis_bundle = (Invoke-WebRequest -Uri $postgis_page -ErrorAction Stop).Links.Where({$_.href -match "^postgis.*zip$"}).href
77-
if (!$pgis_bundle) {
78-
Write-Error "Could not find latest PostGIS version in $postgis_page that would match ^postgis.*zip$ pattern"
79-
exit 1
103+
$PG_VERSION = '${{ steps.pg-version.outputs.version }}'
104+
105+
if ("${{ inputs.postgis_version }}" -ne "") {
106+
$pgis_bundle = "postgis-bundle-pg$PG_VERSION-${{ inputs.postgis_version }}x64"
107+
$pgis_bundle_url_main = "https://download.osgeo.org/postgis/windows/pg$PG_VERSION/$pgis_bundle.zip"
108+
$pgis_bundle_url_archive = "https://download.osgeo.org/postgis/windows/pg$PG_VERSION/archive/$pgis_bundle.zip"
109+
$pgis_bundle_url = $null
110+
$urls = @($pgis_bundle_url_main, $pgis_bundle_url_archive)
111+
foreach ($url in $urls) {
112+
try {
113+
Invoke-WebRequest -Uri $url -Method Head -ErrorAction Stop
114+
$pgis_bundle_url = $url
115+
break
116+
} catch {
117+
continue
118+
}
119+
}
120+
if ($pgis_bundle_url -eq $null) {
121+
Write-Error "Could not find PostGIS bundle at either $pgis_bundle_url_main or $pgis_bundle_url_archive"
122+
exit 1
123+
}
124+
} else {
125+
$postgis_page = "https://download.osgeo.org/postgis/windows/pg$PG_VERSION"
126+
echo "Detecting PostGIS version from $postgis_page for PostgreSQL $PG_VERSION"
127+
$pgis_bundle = (Invoke-WebRequest -Uri $postgis_page -ErrorAction Stop).Links.Where({$_.href -match "^postgis.*zip$"}).href
128+
if (!$pgis_bundle) {
129+
Write-Error "Could not find latest PostGIS version in $postgis_page that would match ^postgis.*zip$ pattern"
130+
exit 1
131+
}
132+
$pgis_bundle = [IO.Path]::ChangeExtension($pgis_bundle, [NullString]::Value)
133+
$pgis_bundle_url = "$postgis_page/$pgis_bundle.zip"
80134
}
81-
$pgis_bundle = [IO.Path]::ChangeExtension($pgis_bundle, [NullString]::Value)
82-
$pgis_bundle_url = "$postgis_page/$pgis_bundle.zip"
135+
83136
Add-Content $env:GITHUB_OUTPUT "pgis_bundle=$pgis_bundle"
84137
Add-Content $env:GITHUB_OUTPUT "pgis_bundle_url=$pgis_bundle_url"
85138
@@ -116,26 +169,22 @@ runs:
116169
- name: Install Postgis (Windows)
117170
if: runner.os == 'Windows'
118171
shell: pwsh
172+
env:
173+
PGDATA: ${{ env.PGDATA }}
119174
run: |
120175
if (!(Test-Path "${{ inputs.cached-dir }}\*")) {
121176
Write-Error "Could not find PostGIS files in ${{ inputs.cached-dir }}"
122177
exit 1
123178
}
124-
echo "Copied $((Copy-Item -Path "${{ inputs.cached-dir }}\*" -Destination $env:PGROOT -Force -Recurse -PassThru).count) PostGIS files to $env:PGROOT"
125-
echo "Starting PostgreSQL and adding postgis extension"
126-
$env:Path = "$env:PGBIN;" + $env:Path
127-
& pg_ctl restart -D "$env:PGDATA"
128-
& pg_isready
129-
130-
- name: Setup PostgreSQL for Linux/macOS/Windows
131-
id: pg
132-
uses: ikalnytskyi/action-setup-postgres@v6
133-
with:
134-
username: "${{ inputs.username }}"
135-
password: "${{ inputs.password }}"
136-
database: "${{ inputs.database }}"
137-
port: "${{ inputs.port }}"
138-
179+
180+
# Set PG_ROOTDIR environment variable
181+
echo "PG_ROOTDIR=$env:PROGRAMFILES\PostgreSQL\${{ steps.pg-version.outputs.version }}" >> $env:GITHUB_ENV
182+
$env:PGROOT = "$env:PROGRAMFILES\PostgreSQL\${{ steps.pg-version.outputs.version }}"
183+
184+
# Copy PostGIS files to PostgreSQL directory
185+
echo "Copying PostGIS files to $env:PGROOT"
186+
Copy-Item -Path "${{ inputs.cached-dir }}\*" -Destination $env:PGROOT -Force -Recurse -ErrorAction Continue
187+
139188
- name: Enable PostGIS extension
140189
run: psql -v ON_ERROR_STOP=1 -c 'CREATE EXTENSION IF NOT EXISTS postgis;' '${{ steps.pg.outputs.connection-uri }}'
141190
shell: bash

0 commit comments

Comments
 (0)