Skip to content

Commit a5a3181

Browse files
authored
feat!: load config file / auto-update (#10)
- look for and `source` `.env.sh` if present - `git pull` to auto-update if clone is in a clean working state - adjust option names for consistency (*breaking change!*)
1 parent 12eab63 commit a5a3181

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

.env.example.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
export POSTGRES_VERSION="17"
4+
export POSTGRES_USER="postgres"
5+
export POSTGRES_PASSWORD="postgres"
6+
export POSTGRES_HOST_AUTH_METHOD="trust"
7+
export POSTGRES_ROLE_ATTRIBUTES="LOGIN CREATEDB"
8+
export POSTGRES_EXTENSIONS="ltree postgis"
9+
10+
export EPHEMERAL_POSTGRES_AUTO_UPDATE="1"
11+
export EPHEMERAL_POSTGRES_FORCE_BUILD="0"

.github/workflows/cd.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ on:
1212
jobs:
1313
ci:
1414
runs-on: ubuntu-latest
15+
16+
env:
17+
EPHEMERAL_POSTGRES_AUTO_UPDATE: 0
18+
1519
strategy:
1620
matrix:
1721
postgres-version: [
@@ -20,6 +24,7 @@ jobs:
2024
16,
2125
17
2226
]
27+
2328
steps:
2429
- uses: actions/checkout@v4
2530
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
.env.sh

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ start-ephemeral-postgres.sh
3232
```
3333

3434
**Options**
35-
3635
- `POSTGRES_VERSION=17`
3736
- `POSTGRES_USER=postgres`
3837
- `POSTGRES_PASSWORD=postgres`
3938
- `POSTGRES_HOST_AUTH_METHOD=trust` - could be `scram-sha-256` / `md5` / etc
40-
- `ROLE_ATTRIBUTES='LOGIN CREATEDB'` - could be `SUPERUSER` / `CREATEROLE BYPASSRLS` / etc
39+
- `POSTGRES_ROLE_ATTRIBUTES='LOGIN CREATEDB'` - could be `SUPERUSER` / `CREATEROLE BYPASSRLS` / etc
4140
- `POSTGRES_EXTENSIONS=` - could be `postgis ltree` / etc
42-
- `FORCE_BUILD=0` - force building the docker image locally instead of pulling a prebuilt image
41+
- `EPHEMERAL_POSTGRES_FORCE_BUILD=0` - force building the docker image locally instead of pulling a prebuilt image
42+
- `EPHEMERAL_POSTGRES_AUTO_UPDATE=1` - whether to automatically check for updates to `ephemeral-postgres`
43+
44+
You can also create a `.env.sh` file and this will be automatically loaded by `start-postgres.sh
4345

4446
Connect using `psql`:
4547

bin/test-image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fi
1515
export POSTGRES_VERSION="${1}"
1616
export POSTGRES_EXTENSIONS="ltree postgis"
1717
# reuse the image we just built
18-
export FORCE_BUILD=1
18+
export EPHEMERAL_POSTGRES_FORCE_BUILD=1
1919

2020
../start-ephemeral-postgres.sh
2121

ensure_role_and_database_exists.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void ensure_role_and_database_exists(Port *port, int status) {
2323
return;
2424
}
2525

26-
role_attributes = getenv("ROLE_ATTRIBUTES");
26+
role_attributes = getenv("POSTGRES_ROLE_ATTRIBUTES");
2727

2828
fprintf(stderr, "ensuring user_name '%s' exists with attributes '%s'\n", port->user_name, role_attributes);
2929
asprintf(&cmd,

start-ephemeral-postgres.sh

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,36 @@ trap popd EXIT
1111
: "${POSTGRES_USER:=postgres}"
1212
: "${POSTGRES_PASSWORD:=postgres}"
1313
: "${POSTGRES_HOST_AUTH_METHOD:=trust}"
14-
: "${ROLE_ATTRIBUTES:=LOGIN CREATEDB}"
14+
: "${POSTGRES_ROLE_ATTRIBUTES:=LOGIN CREATEDB}"
1515
: "${POSTGRES_EXTENSIONS:=}"
16-
: "${FORCE_BUILD:=0}"
16+
17+
: "${EPHEMERAL_POSTGRES_AUTO_UPDATE:=1}"
18+
: "${EPHEMERAL_POSTGRES_FORCE_BUILD:=0}"
19+
20+
if [ -f .env.sh ]; then
21+
echo "loading config from '.env.sh'"
22+
source .env.sh
23+
fi
24+
25+
if [[ "${EPHEMERAL_POSTGRES_AUTO_UPDATE}" -eq 1 ]]; then
26+
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
27+
if [ -z "$(git status --porcelain)" ]; then
28+
echo "Repository is clean. Pulling latest changes..."
29+
git pull
30+
else
31+
echo "Repository has uncommitted changes. Skipping pull."
32+
fi
33+
else
34+
echo "Current directory is not a Git repository."
35+
fi
36+
fi
37+
1738
IMAGE=mnahkies/ephemeral-postgres:$POSTGRES_VERSION
1839

1940
docker stop postgres || echo 'already stopped'
2041
docker rm postgres || echo 'already removed'
2142

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

0 commit comments

Comments
 (0)