Skip to content

Commit df742c9

Browse files
committed
Add 'postgres-version' input parameter
One of the foundational principles of this action was to use preinstalled PostgreSQL binaries. The rationale was simple: * Make sure the action is fast and no installation is required. * Make sure it's safe and doesn't reach out to external servers. * Make sure it's auditable and everyone can verify it contains no malicious code. It's quite common, however, for applications to follow upstream PostgreSQL releases and switch to them as soon as possible. This applications look to install the latest stable version instead. This patch adds a new `postgres-version` input parameter that controls what major version of PostgreSQL to install. Please note, the parameter receives only major part of the version, e.g. "14'. It's impossible to request any minor release. Co-Authored-By: Roman Podoliaka <[email protected]> Co-Authored-By: Ruslan Kiyanchuk <[email protected]> Resolves: #14
1 parent 814fad8 commit df742c9

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ jobs:
1818
- windows-latest
1919
- macos-latest
2020
steps:
21-
- uses: actions/checkout@v2
21+
- uses: actions/checkout@v3
22+
23+
- uses: mxschmitt/action-tmate@v3
24+
if: ${{ matrix.os == 'windows-latest' }}
2225

2326
- name: Run setup-postgres
2427
uses: ./
@@ -33,6 +36,7 @@ jobs:
3336
SERVICE_NAME: ${{ steps.postgres.outputs.service-name }}
3437
EXPECTED_CONNECTION_URI: postgresql://postgres:postgres@localhost:5432/postgres
3538
EXPECTED_SERVICE_NAME: postgres
39+
EXPECTED_SERVER_VERSION: "15"
3640
shell: bash
3741

3842
parametrized:
@@ -43,8 +47,11 @@ jobs:
4347
- ubuntu-latest
4448
- windows-latest
4549
- macos-latest
50+
postgres-version:
51+
- "13"
52+
- "14"
4653
steps:
47-
- uses: actions/checkout@v2
54+
- uses: actions/checkout@v3
4855

4956
- name: Run setup-postgres
5057
uses: ./
@@ -53,6 +60,7 @@ jobs:
5360
password: GrandMaster
5461
database: jedi_order
5562
port: 34837
63+
postgres-version: ${{ matrix.postgres-version }}
5664
id: postgres
5765

5866
- name: Run tests
@@ -64,4 +72,5 @@ jobs:
6472
SERVICE_NAME: ${{ steps.postgres.outputs.service-name }}
6573
EXPECTED_CONNECTION_URI: postgresql://yoda:GrandMaster@localhost:34837/jedi_order
6674
EXPECTED_SERVICE_NAME: yoda
75+
EXPECTED_SERVER_VERSION: ${{ matrix.postgres-version }}
6776
shell: bash

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ key features:
55

66
* Runs on Linux, macOS and Windows action runners.
77
* Adds PostgreSQL [client applications][1] to `PATH`.
8-
* Uses PostgreSQL binaries baked into [GitHub Actions Runner Images][2].
9-
* Easy [to prove][3] that it DOES NOT contain malicious code.
8+
* Supports PostgreSQL version parametrization.
9+
* Easy [to prove][2] that it DOES NOT contain malicious code.
10+
11+
By default PostgreSQL 15 is used.
1012

1113
[1]: https://www.postgresql.org/docs/current/reference-client.html
12-
[2]: https://github.com/actions/runner-images
13-
[3]: action.yml
14+
[2]: action.yml
1415

1516
## Usage
1617

@@ -37,19 +38,20 @@ key features:
3738

3839
```yaml
3940
steps:
40-
- uses: ikalnytskyi/action-setup-postgres@v4
41+
- uses: ikalnytskyi/action-setup-postgres@v5
4142
```
4243
4344
#### Advanced
4445
4546
```yaml
4647
steps:
47-
- uses: ikalnytskyi/action-setup-postgres@v4
48+
- uses: ikalnytskyi/action-setup-postgres@v5
4849
with:
4950
username: ci
5051
password: sw0rdfish
5152
database: test
5253
port: 34837
54+
postgres-version: "13"
5355
id: postgres
5456

5557
- run: pytest -vv tests/
@@ -67,7 +69,7 @@ steps:
6769
6870
```yaml
6971
steps:
70-
- uses: ikalnytskyi/action-setup-postgres@v4
72+
- uses: ikalnytskyi/action-setup-postgres@v5
7173

7274
- run: |
7375
createuser myuser
@@ -90,7 +92,7 @@ steps:
9092
9193
```yaml
9294
steps:
93-
- uses: ikalnytskyi/action-setup-postgres@v4
95+
- uses: ikalnytskyi/action-setup-postgres@v5
9496
```
9597
9698
```python

action.yml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inputs:
2121
description: The server port to listen on.
2222
default: "5432"
2323
required: false
24+
postgres-version:
25+
description: The PostgreSQL version (major) to install. E.g. "13" or "14".
26+
default: "15"
27+
required: false
2428
outputs:
2529
connection-uri:
2630
description: The connection URI to connect to PostgreSQL.
@@ -31,13 +35,41 @@ outputs:
3135
runs:
3236
using: composite
3337
steps:
34-
- name: Prerequisites
38+
- name: Install PostgreSQL
3539
run: |
3640
if [ "$RUNNER_OS" == "Linux" ]; then
37-
echo "$(pg_config --bindir)" >> $GITHUB_PATH
41+
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
42+
| sudo tee /etc/apt/sources.list.d/pgdg.list
43+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
44+
sudo apt-get update
45+
sudo apt-get -y install postgresql-${{ inputs.postgres-version }}
46+
47+
# Add PostgreSQL binaries to PATH, so they become globally available.
48+
/usr/lib/postgresql/${{ inputs.postgres-version }}/bin/pg_config --bindir >> $GITHUB_PATH
49+
50+
elif [ "$RUNNER_OS" == "macOS" ]; then
51+
brew install postgresql@${{ inputs.postgres-version }}
52+
53+
# Link PostgreSQL binaries to /usr/local/bin so they become globally
54+
# available. The overwrite option is required because they might be a
55+
# preinstalled linked bottle.
56+
brew link --overwrite postgresql@${{ inputs.postgres-version }}
57+
3858
elif [ "$RUNNER_OS" == "Windows" ]; then
39-
echo "$PGBIN" >> $GITHUB_PATH
40-
echo "PQ_LIB_DIR=$PGROOT\lib" >> $GITHUB_ENV
59+
choco install postgresql${{ inputs.postgres-version }} \
60+
--ia "--servicename postgresql-${{ inputs.postgres-version }}"
61+
62+
# Stop PostgreSQL that has been auto started after installation. This
63+
# action prepares new configuration and brings its own instance, and
64+
# we need a network port to be free.
65+
net stop postgresql-${{ inputs.postgres-version }}
66+
67+
# Add PostgreSQL binaries to PATH, so they become globally available
68+
# and set path to LIBPQ to link against. On Windows it comes together
69+
# with PostgreSQL distribution.
70+
export PGROOT="$PROGRAMFILES/PostgreSQL/${{ inputs.postgres-version }}"
71+
"$PGROOT"/bin/pg_config.exe --bindir >> $GITHUB_PATH
72+
echo "PQ_LIB_DIR=$("$PGROOT"/bin/pg_config.exe --libdir)" >> $GITHUB_ENV
4173
fi
4274
shell: bash
4375

@@ -65,8 +97,7 @@ runs:
6597
--pwfile="$PWFILE" \
6698
--auth="scram-sha-256" \
6799
--encoding="UTF-8" \
68-
--locale="en_US.UTF-8" \
69-
--no-instructions
100+
--locale="en_US.UTF-8"
70101
71102
# Do not create unix sockets since they are created by default in the
72103
# directory we have no permissions to (owned by system postgres user).

test_action.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def test_locale(connection: psycopg.Connection):
8484
assert locale.normalize(lc_ctype) == "en_US.UTF-8"
8585

8686

87+
def test_server_version(connection: psycopg.Connection):
88+
"""Test that PostgreSQL's version is expected."""
89+
90+
server_version = connection.execute("SHOW SERVER_VERSION").fetchone()[0]
91+
assert server_version.split(".")[0] == os.getenv("EXPECTED_SERVER_VERSION")
92+
93+
8794
def test_user_permissions(connection: psycopg.Connection):
8895
"""Test that a user has super/createdb permissions."""
8996

0 commit comments

Comments
 (0)