diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fd16d73 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.claude +.git +.github +.worktrees diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..f78d206 --- /dev/null +++ b/.github/FUNDING.yml @@ -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'] diff --git a/.github/semantic.yml b/.github/semantic.yml new file mode 100644 index 0000000..40b6927 --- /dev/null +++ b/.github/semantic.yml @@ -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 diff --git a/.github/smoke-test.sh b/.github/smoke-test.sh new file mode 100755 index 0000000..72d0f73 --- /dev/null +++ b/.github/smoke-test.sh @@ -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." diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..81c3e42 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.github/workflows/ghcr-cleanup.yml b/.github/workflows/ghcr-cleanup.yml new file mode 100644 index 0000000..01876ef --- /dev/null +++ b/.github/workflows/ghcr-cleanup.yml @@ -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 }} diff --git a/.github/workflows/image-scan.yml b/.github/workflows/image-scan.yml new file mode 100644 index 0000000..0d7fa1a --- /dev/null +++ b/.github/workflows/image-scan.yml @@ -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 }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a34baa --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.claude +.worktrees +node_modules diff --git a/.releaserc.json b/.releaserc.json new file mode 100644 index 0000000..e7e56b9 --- /dev/null +++ b/.releaserc.json @@ -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}" +} diff --git a/.renovaterc.json b/.renovaterc.json new file mode 100644 index 0000000..e111d28 --- /dev/null +++ b/.renovaterc.json @@ -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=(?\\S+)\\s+ENV\\s+\\S+_VERSION=(?\\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" +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..9c4b1cc --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,3 @@ +{ + "recommendations": ["editorconfig.editorconfig"] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..629d9cb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "[json]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[jsonc]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } +} diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..6393e34 --- /dev/null +++ b/AGENTS.md @@ -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 diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..b5cafd7 --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1 @@ +* @dargmuesli diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c7bb0f8 --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..78fe428 --- /dev/null +++ b/README.md @@ -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`.