Skip to content

Commit d6592a6

Browse files
ikalnytskyimalorrkiyanchuk
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 d6592a6

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
@@ -53,6 +54,7 @@ jobs:
5354
password: GrandMaster
5455
database: jedi_order
5556
port: 34837
57+
postgres-version: "13"
5658
id: postgres
5759

5860
- name: Run tests
@@ -64,4 +66,5 @@ jobs:
6466
SERVICE_NAME: ${{ steps.postgres.outputs.service-name }}
6567
EXPECTED_CONNECTION_URI: postgresql://yoda:GrandMaster@localhost:34837/jedi_order
6668
EXPECTED_SERVICE_NAME: yoda
69+
EXPECTED_SERVER_VERSION: "13"
6770
shell: bash

action.yml

Lines changed: 30 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 (major) version to install.
26+
default: "15"
27+
required: false
2428
outputs:
2529
connection-uri:
2630
description: The connection URI to connect to PostgreSQL.
@@ -31,13 +35,34 @@ 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+
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
42+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
43+
sudo apt-get update
44+
sudo apt-get -y install postgresql-${{ inputs.postgres-version }}
45+
46+
# Add PostgreSQL binaries to PATH, so they become globally available.
47+
/usr/lib/postgresql/${{ inputs.postgres-version }}/bin/pg_config --bindir >> $GITHUB_PATH
48+
49+
elif [ "$RUNNER_OS" == "macOS" ]; then
50+
brew install postgresql@${{ inputs.postgres-version }}
51+
52+
# Ling PostgreSQL binaries to /usr/local/bin so they become globally
53+
# available. The overwrite option is required because they might be a
54+
# preinstalled linked bottle.
55+
brew link --overwrite postgresql@${{ inputs.postgres-version }}
56+
3857
elif [ "$RUNNER_OS" == "Windows" ]; then
39-
echo "$PGBIN" >> $GITHUB_PATH
40-
echo "PQ_LIB_DIR=$PGROOT\lib" >> $GITHUB_ENV
58+
choco install postgresql${{ inputs.postgres-version }}
59+
60+
# Add PostgreSQL binaries to PATH, so they become globally available.
61+
"C:/Program Files/PostgreSQL/${{ inputs.postgres-version }}/bin/pg_config.exe" --bindir >> $GITHUB_PATH
62+
63+
# Set the path to LIBPQ to link against. On Windows it comes together
64+
# with PostgreSQL distribution.
65+
echo "PQ_LIB_DIR=$('C:/Program Files/PostgreSQL/${{ inputs.postgres-version }}/bin/pg_config.exe' --libdir)" >> $GITHUB_ENV
4166
fi
4267
shell: bash
4368

@@ -65,8 +90,7 @@ runs:
6590
--pwfile="$PWFILE" \
6691
--auth="scram-sha-256" \
6792
--encoding="UTF-8" \
68-
--locale="en_US.UTF-8" \
69-
--no-instructions
93+
--locale="en_US.UTF-8"
7094
7195
# Do not create unix sockets since they are created by default in the
7296
# 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)