Skip to content

Commit 91d91a6

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 91d91a6

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- windows-latest
1919
- macos-latest
2020
steps:
21-
- uses: actions/checkout@v2
21+
- uses: actions/checkout@v3
2222

2323
- name: Run setup-postgres
2424
uses: ./
@@ -33,6 +33,7 @@ jobs:
3333
SERVICE_NAME: ${{ steps.postgres.outputs.service-name }}
3434
EXPECTED_CONNECTION_URI: postgresql://postgres:postgres@localhost:5432/postgres
3535
EXPECTED_SERVICE_NAME: postgres
36+
EXPECTED_SERVER_VERSION: "15"
3637
shell: bash
3738

3839
parametrized:
@@ -43,8 +44,11 @@ jobs:
4344
- ubuntu-latest
4445
- windows-latest
4546
- macos-latest
47+
postgres-version:
48+
- "13"
49+
- "14"
4650
steps:
47-
- uses: actions/checkout@v2
51+
- uses: actions/checkout@v3
4852

4953
- name: Run setup-postgres
5054
uses: ./
@@ -53,6 +57,7 @@ jobs:
5357
password: GrandMaster
5458
database: jedi_order
5559
port: 34837
60+
postgres-version: ${{ matrix.postgres-version }}
5661
id: postgres
5762

5863
- name: Run tests
@@ -64,4 +69,5 @@ jobs:
6469
SERVICE_NAME: ${{ steps.postgres.outputs.service-name }}
6570
EXPECTED_CONNECTION_URI: postgresql://yoda:GrandMaster@localhost:34837/jedi_order
6671
EXPECTED_SERVICE_NAME: yoda
72+
EXPECTED_SERVER_VERSION: ${{ matrix.postgres-version }}
6773
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: 41 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,45 @@ 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+
# FIXME: Aargh! For reasons unknown the '--servicename' option is
60+
# ignored when installing a PostgreSQL version that is already
61+
# preinstalled on GitHub runners. In order to bypass the issue I'm
62+
# using default naming convention (i.e. with arch in the name).
63+
choco install postgresql${{ inputs.postgres-version }} \
64+
--ia "--servicename postgresql-${{ runner.arch }}-${{ inputs.postgres-version }}"
65+
66+
# Stop PostgreSQL that has been auto started after installation. This
67+
# action prepares new configuration and brings its own instance, and
68+
# we need a network port to be free.
69+
net stop postgresql-${{ runner.arch }}-${{ inputs.postgres-version }}
70+
71+
# Add PostgreSQL binaries to PATH, so they become globally available
72+
# and set path to LIBPQ to link against. On Windows it comes together
73+
# with PostgreSQL distribution.
74+
export PGROOT="$PROGRAMFILES/PostgreSQL/${{ inputs.postgres-version }}"
75+
"$PGROOT"/bin/pg_config.exe --bindir >> $GITHUB_PATH
76+
echo "PQ_LIB_DIR=$("$PGROOT"/bin/pg_config.exe --libdir)" >> $GITHUB_ENV
4177
fi
4278
shell: bash
4379

@@ -65,8 +101,7 @@ runs:
65101
--pwfile="$PWFILE" \
66102
--auth="scram-sha-256" \
67103
--encoding="UTF-8" \
68-
--locale="en_US.UTF-8" \
69-
--no-instructions
104+
--locale="en_US.UTF-8"
70105
71106
# Do not create unix sockets since they are created by default in the
72107
# 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)