|
| 1 | +name: Setup PostgreSQL for Linux/macOS/Windows |
| 2 | +author: Ihor Kalnytskyi |
| 3 | +description: Setup a preinstalled PostgreSQL server. |
| 4 | +branding: |
| 5 | + icon: database |
| 6 | + color: purple |
| 7 | +inputs: |
| 8 | + username: |
| 9 | + description: The username of the user to setup. |
| 10 | + default: postgres |
| 11 | + required: false |
| 12 | + password: |
| 13 | + description: The password of the user to setup. |
| 14 | + default: postgres |
| 15 | + required: false |
| 16 | + database: |
| 17 | + description: The database name to setup and grant permissions to created user. |
| 18 | + default: postgres |
| 19 | + required: false |
| 20 | + port: |
| 21 | + description: The server port to listen on. |
| 22 | + default: "5432" |
| 23 | + required: false |
| 24 | +outputs: |
| 25 | + connection-uri: |
| 26 | + description: The connection URI to connect to PostgreSQL. |
| 27 | + value: ${{ steps.set-outputs.outputs.connection-uri }} |
| 28 | + service-name: |
| 29 | + description: The service name with connection parameters. |
| 30 | + value: ${{ steps.set-outputs.outputs.service-name }} |
| 31 | +runs: |
| 32 | + using: composite |
| 33 | + steps: |
| 34 | + - name: Prerequisites |
| 35 | + run: | |
| 36 | + if [ "$RUNNER_OS" == "Linux" ]; then |
| 37 | + echo "$(pg_config --bindir)" >> $GITHUB_PATH |
| 38 | + elif [ "$RUNNER_OS" == "Windows" ]; then |
| 39 | + echo "$PGBIN" >> $GITHUB_PATH |
| 40 | + echo "PQ_LIB_DIR=$PGROOT\lib" >> $GITHUB_ENV |
| 41 | +
|
| 42 | + # The Windows runner has some PostgreSQL environment variables set |
| 43 | + # that may confuse users since they may be irrelevant to the |
| 44 | + # PostgreSQL server we're using. |
| 45 | + for name in "PGROOT" "PGDATA" "PGBIN" "PGUSER" "PGPASSWORD"; do |
| 46 | + echo "$name=" >> $GITHUB_ENV |
| 47 | + done |
| 48 | + elif [ "$RUNNER_OS" == "macOS" ]; then |
| 49 | + case "$(sw_vers -productVersion)" in |
| 50 | + 12.*|13.*) |
| 51 | + brew install postgresql@14 |
| 52 | + echo "/usr/local/opt/postgresql@14/bin" >> $GITHUB_PATH |
| 53 | + ;; |
| 54 | + 14.*) |
| 55 | + brew install postgresql@16 |
| 56 | + echo "/opt/homebrew/opt/postgresql@16/bin" >> $GITHUB_PATH |
| 57 | + ;; |
| 58 | + esac |
| 59 | + fi |
| 60 | + shell: bash |
| 61 | + |
| 62 | + - name: Setup and start PostgreSQL |
| 63 | + run: | |
| 64 | + export PGDATA="$RUNNER_TEMP/pgdata" |
| 65 | + export PWFILE="$RUNNER_TEMP/pwfile" |
| 66 | +
|
| 67 | + # Unfortunately 'initdb' could only receive a password via file on disk |
| 68 | + # or prompt to enter on. Prompting is not an option since we're running |
| 69 | + # in non-interactive mode. |
| 70 | + echo '${{ inputs.password }}' > $PWFILE |
| 71 | +
|
| 72 | + # There are couple of reasons why we need to create a new PostgreSQL |
| 73 | + # database cluster. First and foremost, we have to create a superuser |
| 74 | + # with provided credentials. Second, we want the PostgreSQL client |
| 75 | + # applications [1] to be available for execution without |
| 76 | + # run-from-another-user dances. Third, we want to make sure that |
| 77 | + # settings are the same between operating systems and aren't changed by |
| 78 | + # package vendors. |
| 79 | + # |
| 80 | + # [1] https://www.postgresql.org/docs/15/reference-client.html |
| 81 | + initdb \ |
| 82 | + --username="${{ inputs.username }}" \ |
| 83 | + --pwfile="$PWFILE" \ |
| 84 | + --auth="scram-sha-256" \ |
| 85 | + --encoding="UTF-8" \ |
| 86 | + --locale="en_US.UTF-8" \ |
| 87 | + --no-instructions |
| 88 | +
|
| 89 | + # Do not create unix sockets since they are created by default in the |
| 90 | + # directory we have no permissions to (owned by system postgres user). |
| 91 | + echo "unix_socket_directories = ''" >> "$PGDATA/postgresql.conf" |
| 92 | + echo "port = ${{ inputs.port }}" >> "$PGDATA/postgresql.conf" |
| 93 | + pg_ctl start |
| 94 | +
|
| 95 | + # Save required connection parameters for created superuser to the |
| 96 | + # connection service file [1]. This allows using these connection |
| 97 | + # parameters by setting 'PGSERVICE' environment variable or by |
| 98 | + # requesting them via connection string. |
| 99 | + # |
| 100 | + # HOST is required for Linux/macOS because these OS-es default to unix |
| 101 | + # sockets but we turned them off. |
| 102 | + # |
| 103 | + # PORT, USER, PASSWORD and DBNAME are required because they could be |
| 104 | + # parametrized via action input parameters. |
| 105 | + # |
| 106 | + # [1] https://www.postgresql.org/docs/15/libpq-pgservice.html |
| 107 | + cat <<EOF > "$PGDATA/pg_service.conf" |
| 108 | + [${{ inputs.username }}] |
| 109 | + host=localhost |
| 110 | + port=${{ inputs.port }} |
| 111 | + user=${{ inputs.username }} |
| 112 | + password=${{ inputs.password }} |
| 113 | + dbname=${{ inputs.database }} |
| 114 | + EOF |
| 115 | + echo "PGSERVICEFILE=$PGDATA/pg_service.conf" >> $GITHUB_ENV |
| 116 | + shell: bash |
| 117 | + |
| 118 | + - name: Setup PostgreSQL database |
| 119 | + run: | |
| 120 | + # The 'postgres' database is a pre-created database meant for use by |
| 121 | + # users, utilities and third party applications. There's no way to |
| 122 | + # parametrize the name, so all we can do is to avoid creating a |
| 123 | + # database if provided name is 'postgres'. |
| 124 | + if [ "${{ inputs.database }}" != "postgres" ]; then |
| 125 | + createdb -O "${{ inputs.username }}" "${{ inputs.database }}" |
| 126 | + fi |
| 127 | + env: |
| 128 | + PGSERVICE: ${{ inputs.username }} |
| 129 | + shell: bash |
| 130 | + |
| 131 | + - name: Set action outputs |
| 132 | + run: | |
| 133 | + CONNECTION_URI="postgresql://${{ inputs.username }}:${{ inputs.password }}@localhost:${{ inputs.port }}/${{ inputs.database }}" |
| 134 | +
|
| 135 | + echo "connection-uri=$CONNECTION_URI" >> $GITHUB_OUTPUT |
| 136 | + echo "service-name=${{ inputs.username }}" >> $GITHUB_OUTPUT |
| 137 | + shell: bash |
| 138 | + id: set-outputs |
0 commit comments