Skip to content
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
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.claude
.git
.github
.worktrees
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: [dargmuesli] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
5 changes: 5 additions & 0 deletions .github/semantic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Always validate the PR title AND all the commits
titleAndCommits: true
# Allows use of Merge commits (eg on github: "Merge branch 'master' into feature/ride-unicorns")
# this is only relevant when using commitsOnly: true (or titleAndCommits: true)
allowMergeCommits: true
44 changes: 44 additions & 0 deletions .github/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -euo pipefail

IMAGE="${SMOKE_TEST_IMAGE:?SMOKE_TEST_IMAGE not set}"
CONTAINER="smoke-postgres-${GITHUB_RUN_ID:-$$}-${GITHUB_RUN_ATTEMPT:-1}"

trap 'docker rm --force "$CONTAINER" >/dev/null 2>&1 || true' EXIT

docker run --detach --name "$CONTAINER" \
-e POSTGRES_DB=smoke \
-e POSTGRES_PASSWORD=postgres \
"$IMAGE"

query() {
docker exec "$CONTAINER" psql -U postgres -d smoke -v ON_ERROR_STOP=1 -tAc "$1"
}

# The entrypoint serves init scripts from a temporary server first, so only a query on the final server proves readiness.
for i in $(seq 1 60); do
if docker logs "$CONTAINER" 2>&1 | grep -q 'PostgreSQL init process complete' && query 'SELECT 1' >/dev/null 2>&1; then
echo "PostgreSQL is ready after ${i}s"
break
fi
sleep 1
done

fail() {
echo "$1"
docker logs "$CONTAINER"
exit 1
}

query 'CREATE EXTENSION postgis' >/dev/null || fail 'PostGIS is not installed'
query 'CREATE EXTENSION vector' >/dev/null || fail 'pgvector is not installed'

DISTANCE="$(query "SELECT round(ST_Distance(ST_Point(9.48, 51.31, 4326)::geography, ST_Point(11.58, 48.14, 4326)::geography) / 1000)")"
echo "Distance from Kassel to Munich: ${DISTANCE} km"
[ "$DISTANCE" = '384' ] || fail 'Unexpected geography distance'

VECTOR_DISTANCE="$(query "SELECT '[1,2]'::vector <-> '[2,2]'::vector")"
echo "Vector distance: $VECTOR_DISTANCE"
[ "$VECTOR_DISTANCE" = '1' ] || fail 'Unexpected vector distance'

echo "Smoke test passed."
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: CI

permissions: {}

on:
pull_request:
push:
branches:
- alpha
- beta
- main
- renovate/**

# Release branches queue instead of being cancelled, so a release never stops halfway.
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: ${{ !contains(fromJSON('["refs/heads/alpha", "refs/heads/beta", "refs/heads/main"]'), github.ref) }}

jobs:
ci-optimization:
name: CI optimization
uses: dargmuesli/github-actions/.github/workflows/ci-optimization.yml@66040b4567381bad6e0b9a68caa17643e1d365bd # 5.10.0
permissions:
pull-requests: read
release_semantic_dry:
needs: ci-optimization
if: needs.ci-optimization.outputs.continue == 'true'
name: Release (semantic, dry)
uses: dargmuesli/github-actions/.github/workflows/release-semantic.yml@66040b4567381bad6e0b9a68caa17643e1d365bd # 5.10.0
permissions:
contents: write
id-token: write
secrets:
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
with:
DRY_RUN: true
PACKAGE_MANAGER: npm
build:
needs: release_semantic_dry
name: Build
uses: dargmuesli/github-actions/.github/workflows/docker.yml@66040b4567381bad6e0b9a68caa17643e1d365bd # 5.10.0
permissions:
attestations: write
id-token: write
packages: write
with:
PLATFORMS: linux/amd64,linux/arm64
SMOKE_TEST_SCRIPT: .github/smoke-test.sh
TAG: ${{ needs.release_semantic_dry.outputs.new_release_version }}
release_semantic:
needs: build
name: Release (semantic)
uses: dargmuesli/github-actions/.github/workflows/release-semantic.yml@66040b4567381bad6e0b9a68caa17643e1d365bd # 5.10.0
permissions:
contents: write
id-token: write
secrets:
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
with:
PACKAGE_MANAGER: npm
21 changes: 21 additions & 0 deletions .github/workflows/ghcr-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: GHCR Cleanup

permissions: {}

on:
schedule:
- cron: "0 1 * * 0" # Every Sunday at 01:00 AM UTC
workflow_dispatch:
inputs:
DRY_RUN:
default: true
type: boolean

jobs:
ghcr-cleanup:
name: GHCR cleanup
uses: dargmuesli/github-actions/.github/workflows/ghcr-cleanup.yml@66040b4567381bad6e0b9a68caa17643e1d365bd # 5.10.0
permissions:
packages: write
with:
DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && inputs.DRY_RUN }}
18 changes: 18 additions & 0 deletions .github/workflows/image-scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Image scan

permissions: {}

on:
schedule:
- cron: "0 4 * * *" # Every day at 04:00 AM UTC on the default branch
workflow_dispatch:

jobs:
image-scan:
name: Image scan
uses: dargmuesli/github-actions/.github/workflows/image-scan.yml@ab8e226263168a9aab8c98d6db89ce707f77497e # 5.11.0
permissions:
contents: read
packages: read
secrets:
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.claude
.worktrees
node_modules
22 changes: 22 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"presetConfig": {}
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"presetConfig": {}
}
],
"@semantic-release/changelog",
"@semantic-release/github",
"@semantic-release/git"
],
"tagFormat": "${version}"
}
36 changes: 36 additions & 0 deletions .renovaterc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"customManagers": [
{
"customType": "regex",
"datasourceTemplate": "github-tags",
"managerFilePatterns": ["/^Dockerfile$/"],
"matchStrings": [
"#\\s+renovate:\\s+datasource=github-tags\\s+depName=(?<depName>\\S+)\\s+ENV\\s+\\S+_VERSION=(?<currentValue>\\S+)"
]
}
],
"extends": ["github>dargmuesli/renovate-config"],
"packageRules": [
{
"automerge": false,
"description": "A new PostgreSQL major needs a `pg_upgrade` of every deployed database.",
"matchDatasources": ["docker"],
"matchPackageNames": ["postgres"],
"matchUpdateTypes": ["major"]
},
{
"automerge": false,
"description": "A new PostGIS major renames its library, so existing databases fail until they are upgraded.",
"matchPackageNames": ["postgis/postgis"],
"matchUpdateTypes": ["major"]
},
{
"automerge": false,
"description": "pgvector is still on 0.x, where a minor may break as much as a major.",
"matchPackageNames": ["pgvector/pgvector"],
"matchUpdateTypes": ["major", "minor"]
}
],
"semanticCommitType": "fix"
}
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["editorconfig.editorconfig"]
}
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
29 changes: 29 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
applyTo: '**'
---

# Project Instructions

This project builds the PostgreSQL container image of `vibetype`, an event community platform, for the `postgres` and `reccoom-postgres` services of the `stack` repository.

## Image

- PostGIS and pgvector are compiled from source in the `build` stage, as `postgis/postgis` publishes no arm64 image and Alpine's own packages lag behind the PostGIS version in use
- The PostGIS build follows the upstream Alpine Dockerfile at https://github.com/postgis/docker-postgis, so compare against it when PostGIS updates
- pgvector is built with an empty `OPTFLAGS`, as the default `-march=native` would tie the image to the build machine's CPU
- The arm64 build compiles under emulation and takes about 25 minutes in CI
- The base stays on Alpine, as a switch of the C library changes the collation of existing databases, which invalidates their text indexes
- Renovate tracks PostGIS and pgvector through the `# renovate:` comments in the `Dockerfile`, and does not merge updates automatically that need care:
- a new PostgreSQL major needs a `pg_upgrade` of every deployed database
- a new PostGIS major renames its library, so existing databases fail until they are upgraded; a minor only needs `SELECT postgis_extensions_upgrade()` at some point
- pgvector is still on 0.x, where a minor may break as much as a major
- Both extensions are only installed, unlike in `postgis/postgis`, whose init script creates PostGIS
- The services that use them create them in their own migrations, `sqitch` for PostGIS and `reccoom` for pgvector, which also reach existing databases, while an init script only runs for a new data directory

## Release

- semantic-release versions the image from git tags alone, so the repository has no `package.json`

## Verification

- `.github/smoke-test.sh` runs in CI against the built image and checks the extensions; extend it when the image gains a feature
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @dargmuesli
82 changes: 82 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# syntax=docker/dockerfile:1

# PostGIS is compiled here instead of using `postgis/postgis`, which publishes no arm64 image.
# The build follows https://github.com/postgis/docker-postgis/blob/master/18-3.7/alpine/Dockerfile, without its regression tests.

########################
# Provide the base.

FROM postgres:18.7-alpine3.24@sha256:ff64c9e3bdf8e2120107d3559de8db314e8305f7e38c5105903c6295876738ff AS base


########################
# Compile the extensions.

FROM base AS build

# renovate: datasource=github-tags depName=postgis/postgis
ENV POSTGIS_VERSION=3.7.0
# renovate: datasource=github-tags depName=pgvector/pgvector
ENV PGVECTOR_VERSION=v0.8.7

ADD https://github.com/postgis/postgis.git#${POSTGIS_VERSION} /usr/src/postgis
ADD https://github.com/pgvector/pgvector.git#${PGVECTOR_VERSION} /usr/src/pgvector

# `DOCKER_PG_LLVM_DEPS` names the clang and llvm versions that the base image's PostgreSQL was built with, which its JIT bitcode needs.
# A git checkout gives all files arbitrary modification times, so make may regenerate the committed parsers, which needs bison and flex.
RUN apk add --no-cache \
$DOCKER_PG_LLVM_DEPS \
autoconf \
automake \
bison \
file \
flex \
g++ \
gcc \
gdal-dev \
geos-dev \
gettext-dev \
gmp-dev \
json-c-dev \
libtool \
libxml2-dev \
make \
pcre2-dev \
perl \
proj-dev \
protobuf-c-dev \
sfcgal-dev

WORKDIR /usr/src/postgis

RUN gettextize \
&& ./autogen.sh \
&& ./configure --enable-lto \
&& make -j"$(nproc)" \
&& make install DESTDIR=/srv/extensions

WORKDIR /usr/src/pgvector

# An empty `OPTFLAGS` drops `-march=native`, which would tie the build to the CPU of the machine building it.
RUN make OPTFLAGS="" \
&& make install DESTDIR=/srv/extensions


########################
# Serve the database.

FROM base AS production

RUN apk add --no-cache \
ca-certificates \
gdal \
geos \
gmp \
json-c \
libstdc++ \
pcre2 \
proj \
protobuf-c \
sfcgal

COPY --from=build /srv/extensions/usr/local /usr/local
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# postgres

Vibetype's PostgreSQL container image, which adds [PostGIS](https://postgis.net/) and [pgvector](https://github.com/pgvector/pgvector) to the official [`postgres`](https://hub.docker.com/_/postgres) Alpine image.
It is published for `linux/amd64` and `linux/arm64` as `ghcr.io/maevsi/postgres`.

## Usage

The image is configured like the official `postgres` image.
Both extensions are installed but not created, so a database that needs one runs `CREATE EXTENSION postgis` or `CREATE EXTENSION vector`, ideally in its migrations.

## Development

Build the image with `docker build -t postgres .` and test it with `SMOKE_TEST_IMAGE=postgres .github/smoke-test.sh`.
Loading