Skip to content

feat!: load config file / auto-update #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .env.example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

export POSTGRES_VERSION="17"
export POSTGRES_USER="postgres"
export POSTGRES_PASSWORD="postgres"
export POSTGRES_HOST_AUTH_METHOD="trust"
export POSTGRES_ROLE_ATTRIBUTES="LOGIN CREATEDB"
export POSTGRES_EXTENSIONS="ltree postgis"

export EPHEMERAL_POSTGRES_AUTO_UPDATE="1"
export EPHEMERAL_POSTGRES_FORCE_BUILD="0"
5 changes: 5 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ on:
jobs:
ci:
runs-on: ubuntu-latest

env:
EPHEMERAL_POSTGRES_AUTO_UPDATE: 0

strategy:
matrix:
postgres-version: [
Expand All @@ -20,6 +24,7 @@ jobs:
16,
17
]

steps:
- uses: actions/checkout@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.env.sh
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ start-ephemeral-postgres.sh
```

**Options**

- `POSTGRES_VERSION=17`
- `POSTGRES_USER=postgres`
- `POSTGRES_PASSWORD=postgres`
- `POSTGRES_HOST_AUTH_METHOD=trust` - could be `scram-sha-256` / `md5` / etc
- `ROLE_ATTRIBUTES='LOGIN CREATEDB'` - could be `SUPERUSER` / `CREATEROLE BYPASSRLS` / etc
- `POSTGRES_ROLE_ATTRIBUTES='LOGIN CREATEDB'` - could be `SUPERUSER` / `CREATEROLE BYPASSRLS` / etc
- `POSTGRES_EXTENSIONS=` - could be `postgis ltree` / etc
- `FORCE_BUILD=0` - force building the docker image locally instead of pulling a prebuilt image
- `EPHEMERAL_POSTGRES_FORCE_BUILD=0` - force building the docker image locally instead of pulling a prebuilt image
- `EPHEMERAL_POSTGRES_AUTO_UPDATE=1` - whether to automatically check for updates to `ephemeral-postgres`

You can also create a `.env.sh` file and this will be automatically loaded by `start-postgres.sh

Connect using `psql`:

Expand Down
2 changes: 1 addition & 1 deletion bin/test-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fi
export POSTGRES_VERSION="${1}"
export POSTGRES_EXTENSIONS="ltree postgis"
# reuse the image we just built
export FORCE_BUILD=1
export EPHEMERAL_POSTGRES_FORCE_BUILD=1

../start-ephemeral-postgres.sh

Expand Down
2 changes: 1 addition & 1 deletion ensure_role_and_database_exists.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void ensure_role_and_database_exists(Port *port, int status) {
return;
}

role_attributes = getenv("ROLE_ATTRIBUTES");
role_attributes = getenv("POSTGRES_ROLE_ATTRIBUTES");

fprintf(stderr, "ensuring user_name '%s' exists with attributes '%s'\n", port->user_name, role_attributes);
asprintf(&cmd,
Expand Down
29 changes: 25 additions & 4 deletions start-ephemeral-postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,36 @@ trap popd EXIT
: "${POSTGRES_USER:=postgres}"
: "${POSTGRES_PASSWORD:=postgres}"
: "${POSTGRES_HOST_AUTH_METHOD:=trust}"
: "${ROLE_ATTRIBUTES:=LOGIN CREATEDB}"
: "${POSTGRES_ROLE_ATTRIBUTES:=LOGIN CREATEDB}"
: "${POSTGRES_EXTENSIONS:=}"
: "${FORCE_BUILD:=0}"

: "${EPHEMERAL_POSTGRES_AUTO_UPDATE:=1}"
: "${EPHEMERAL_POSTGRES_FORCE_BUILD:=0}"

if [ -f .env.sh ]; then
echo "loading config from '.env.sh'"
source .env.sh
fi

if [[ "${EPHEMERAL_POSTGRES_AUTO_UPDATE}" -eq 1 ]]; then
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
if [ -z "$(git status --porcelain)" ]; then
echo "Repository is clean. Pulling latest changes..."
git pull
else
echo "Repository has uncommitted changes. Skipping pull."
fi
else
echo "Current directory is not a Git repository."
fi
fi

IMAGE=mnahkies/ephemeral-postgres:$POSTGRES_VERSION

docker stop postgres || echo 'already stopped'
docker rm postgres || echo 'already removed'

if [[ "${FORCE_BUILD}" -ne 0 ]]; then
if [[ "${EPHEMERAL_POSTGRES_FORCE_BUILD}" -ne 0 ]]; then
echo "Force build enabled. Skipping pull and building Docker image with POSTGRES_VERSION=$POSTGRES_VERSION"
docker build --build-arg POSTGRES_VERSION="${POSTGRES_VERSION}" . -t "${IMAGE}"
else
Expand All @@ -40,7 +61,7 @@ docker run -d --rm --name postgres $MNT \
-e POSTGRES_USER="${POSTGRES_USER}" \
-e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \
-e POSTGRES_HOST_AUTH_METHOD="${POSTGRES_HOST_AUTH_METHOD}" \
-e ROLE_ATTRIBUTES="${ROLE_ATTRIBUTES}" \
-e POSTGRES_ROLE_ATTRIBUTES="${POSTGRES_ROLE_ATTRIBUTES}" \
-p 5432:5432 "${IMAGE}" \
-c shared_buffers=256MB \
-c 'shared_preload_libraries=$libdir/ensure_role_and_database_exists'
Expand Down