diff --git a/.github/workflows/cla.yml b/.github/workflows/cla.yml index 24eb4f46..ce782cec 100644 --- a/.github/workflows/cla.yml +++ b/.github/workflows/cla.yml @@ -36,4 +36,4 @@ jobs: # a bot commit into the release history and trigger a CI run every # time somebody signs. branch: cla-signatures - allowlist: vishr,dependabot[bot] + allowlist: vishr,dependabot[bot],github-actions[bot] diff --git a/.github/workflows/discovery-image.yml b/.github/workflows/discovery-image.yml index ffe71e48..3a33c785 100644 --- a/.github/workflows/discovery-image.yml +++ b/.github/workflows/discovery-image.yml @@ -27,8 +27,8 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3 - + # No QEMU: the Go stage cross-compiles on the build platform and the + # scratch stage runs no commands, so no step executes foreign binaries. - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3 @@ -43,6 +43,8 @@ jobs: context: . file: build/package/onebox-discovery.Dockerfile platforms: linux/amd64,linux/arm64 + cache-from: type=gha,scope=onebox-discovery + cache-to: type=gha,mode=max,scope=onebox-discovery push: true tags: ghcr.io/labstack/onebox-discovery:edge provenance: mode=max diff --git a/.github/workflows/postgres-image-bump.yml b/.github/workflows/postgres-image-bump.yml new file mode 100644 index 00000000..fad2035d --- /dev/null +++ b/.github/workflows/postgres-image-bump.yml @@ -0,0 +1,192 @@ +name: PostgreSQL image pin bump + +# The Dockerfile pins its base images by digest and its pgdg extension +# packages by exact build, so security fixes and PostgreSQL minor releases +# arrive only through a pin bump. This opens that bump as a pull request each +# week, after both platforms build and pass the same smoke test the publish +# workflow runs. Merging it republishes onebox-postgres:18. +# +# Without POSTGRES_BUMP_TOKEN, opening the pull request relies on the repo +# setting "Allow GitHub Actions to create and approve pull requests". +on: + schedule: + - cron: "0 5 * * 1" + workflow_dispatch: + +permissions: {} + +concurrency: + group: postgres-image-bump + cancel-in-progress: true + +env: + BRANCH: bot/postgres-image-bump + +jobs: + bump: + name: Refresh pins + runs-on: ubuntu-24.04 + permissions: + contents: write + outputs: + changed: ${{ steps.bump.outputs.changed }} + sha: ${{ steps.commit.outputs.sha }} + candidate: ${{ steps.commit.outputs.candidate }} + steps: + # A pull request opened or pushed with GITHUB_TOKEN starts no workflows, + # so the required checks never run on it. With POSTGRES_BUMP_TOKEN (a + # fine-grained token with contents and pull-requests write on this repo) + # they run as usual; without it, close and reopen the pull request. + # Always bump main, whatever ref dispatched the run: the pull request + # targets main and must carry nothing but the bump. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: main + token: ${{ secrets.POSTGRES_BUMP_TOKEN || secrets.GITHUB_TOKEN }} + + # The script asks an arm64 container which pgdg builds it sees. + - uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3 + + - name: Bump pins + id: bump + run: | + set -euo pipefail + build/package/postgres-bump.sh | tee "$RUNNER_TEMP/changes.txt" + if git diff --quiet; then + echo "changed=false" >> "$GITHUB_OUTPUT" + echo "All pins are current." >> "$GITHUB_STEP_SUMMARY" + else + echo "changed=true" >> "$GITHUB_OUTPUT" + { echo '```'; cat "$RUNNER_TEMP/changes.txt"; echo '```'; } >> "$GITHUB_STEP_SUMMARY" + fi + + # The candidate lives on a per-run branch. The pull request branch moves + # to it only after both platforms pass, so an open pull request never + # carries unverified pins. + - name: Push candidate branch + id: commit + if: steps.bump.outputs.changed == 'true' + run: | + set -euo pipefail + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + candidate="$BRANCH-candidate-$GITHUB_RUN_ID" + git switch -c "$candidate" + { + echo "build(postgres): bump image pins" + echo + cat "$RUNNER_TEMP/changes.txt" + } > "$RUNNER_TEMP/message.txt" + git commit -q -a -F "$RUNNER_TEMP/message.txt" + git push origin "$candidate" + echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + echo "candidate=$candidate" >> "$GITHUB_OUTPUT" + + verify: + name: Verify bump (${{ matrix.arch }}) + needs: bump + if: needs.bump.outputs.changed == 'true' + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: true + matrix: + include: + - arch: amd64 + runner: ubuntu-24.04 + - arch: arm64 + runner: ubuntu-24.04-arm + permissions: + contents: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ needs.bump.outputs.sha }} + + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 + + - name: Build bumped image + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6 + with: + context: . + file: build/package/postgres.Dockerfile + platforms: linux/${{ matrix.arch }} + load: true + tags: onebox-postgres:bump + # Read main's cache but never write it: an unmerged bump must not + # replace the layers the published pins depend on. + cache-from: type=gha,scope=onebox-postgres-${{ matrix.arch }} + + - name: Verify bumped image + run: build/package/postgres-verify.sh onebox-postgres:bump + + pull-request: + name: Open or update pull request + needs: [bump, verify] + runs-on: ubuntu-24.04 + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ needs.bump.outputs.sha }} + token: ${{ secrets.POSTGRES_BUMP_TOKEN || secrets.GITHUB_TOKEN }} + + - name: Move pull request branch to the verified commit + env: + CANDIDATE: ${{ needs.bump.outputs.candidate }} + run: | + set -euo pipefail + git push -f origin "HEAD:refs/heads/$BRANCH" + git push origin --delete "$CANDIDATE" + + - name: Open or update pull request + env: + GH_TOKEN: ${{ secrets.POSTGRES_BUMP_TOKEN || secrets.GITHUB_TOKEN }} + HAS_BUMP_TOKEN: ${{ secrets.POSTGRES_BUMP_TOKEN != '' }} + run: | + set -euo pipefail + { + echo "## What this changes" + echo + echo "The weekly pin refresh found newer builds:" + echo + echo '```' + git log -1 --format=%b + echo '```' + echo + echo "## Why this is correct" + echo + echo "Both platforms built from this commit and passed \`build/package/postgres-verify.sh\` on native runners." + echo + echo "## Effect on the safety envelope" + echo + echo "Merging republishes \`ghcr.io/labstack/onebox-postgres:18\`. Each host picks it up on its next pull, which restarts Postgres. Existing databases keep their extension versions until \`ALTER EXTENSION ... UPDATE\` runs. Merge at a quiet time and check that Postgres starts and WAL archiving works on each host afterward." + if [ "$HAS_BUMP_TOKEN" != true ]; then + echo + echo "> [!NOTE]" + echo "> This pull request was opened with \`GITHUB_TOKEN\`, which starts no workflows. Close and reopen it to run the required checks." + fi + } > "$RUNNER_TEMP/body.md" + number=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number // empty') + if [ -n "$number" ]; then + gh pr edit "$number" --body-file "$RUNNER_TEMP/body.md" + else + gh pr create --base main --head "$BRANCH" \ + --title "build(postgres): bump image pins" \ + --body-file "$RUNNER_TEMP/body.md" + fi + + cleanup: + name: Delete unverified candidate + needs: [bump, verify] + if: always() && needs.bump.outputs.candidate != '' && needs.verify.result != 'success' + runs-on: ubuntu-24.04 + permissions: + contents: write + steps: + - name: Delete candidate branch + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CANDIDATE: ${{ needs.bump.outputs.candidate }} + run: gh api -X DELETE "repos/$GITHUB_REPOSITORY/git/refs/heads/$CANDIDATE" diff --git a/.github/workflows/postgres-image.yml b/.github/workflows/postgres-image.yml index c672cd96..e5640323 100644 --- a/.github/workflows/postgres-image.yml +++ b/.github/workflows/postgres-image.yml @@ -5,6 +5,7 @@ on: branches: [main] paths: - build/package/postgres.Dockerfile + - build/package/postgres-verify.sh - .github/workflows/postgres-image.yml workflow_dispatch: @@ -15,80 +16,61 @@ concurrency: cancel-in-progress: true jobs: - verify: - name: Verify PostgreSQL 18 image - runs-on: ubuntu-24.04 + platform: + name: Build and verify PostgreSQL 18 (${{ matrix.arch }}) + # Each platform builds on a native runner. Compiling pgvector under QEMU + # was the slowest step of the old single-runner build. + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: true + matrix: + include: + - arch: amd64 + runner: ubuntu-24.04 + - arch: arm64 + runner: ubuntu-24.04-arm + permissions: + contents: read + packages: write + env: + CANDIDATE: ghcr.io/labstack/onebox-postgres:18-${{ github.sha }}-${{ matrix.arch }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 - - name: Build test image + - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # The per-platform tag is a candidate only. Hosts pull + # onebox-postgres:18, which the merge job moves after both platforms pass. + - name: Build and publish platform candidate uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6 with: context: . file: build/package/postgres.Dockerfile - load: true - tags: onebox-postgres:test + platforms: linux/${{ matrix.arch }} + push: true + tags: ${{ env.CANDIDATE }} + provenance: mode=max + sbom: true + cache-from: type=gha,scope=onebox-postgres-${{ matrix.arch }} + cache-to: type=gha,mode=max,scope=onebox-postgres-${{ matrix.arch }} - - name: Verify bundled extensions - shell: bash - run: | - set -euo pipefail - container=onebox-postgres-test - trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT - docker run -d --name "$container" -e POSTGRES_PASSWORD=test onebox-postgres:test \ - -c shared_preload_libraries=pg_cron,pgaudit,pg_stat_statements \ - -c cron.database_name=postgres \ - -c cron.use_background_workers=on - for attempt in $(seq 1 30); do - if docker exec "$container" pg_isready -U postgres -d postgres >/dev/null 2>&1; then - break - fi - if [ "$attempt" -eq 30 ]; then - docker logs "$container" - exit 1 - fi - sleep 1 - done - docker run --rm onebox-postgres:test wal-g --version - docker exec "$container" psql -U postgres -d postgres -v ON_ERROR_STOP=1 \ - -c 'CREATE EXTENSION vector' \ - -c 'CREATE EXTENSION vectorscale' \ - -c 'CREATE EXTENSION postgis' \ - -c 'CREATE EXTENSION pg_cron' \ - -c 'CREATE EXTENSION pgaudit' \ - -c 'CREATE EXTENSION pg_repack' \ - -c 'CREATE EXTENSION pg_partman' \ - -c 'CREATE EXTENSION hypopg' \ - -c 'CREATE EXTENSION pg_stat_statements' \ - -c "SELECT '[1,2,3]'::vector <-> '[1,2,4]'::vector" \ - -c 'CREATE TABLE onebox_vectorscale(id bigint, embedding vector(3))' \ - -c "INSERT INTO onebox_vectorscale VALUES (1, '[1,2,3]'), (2, '[4,5,6]'), (3, '[7,8,9]')" \ - -c 'CREATE INDEX onebox_vectorscale_diskann ON onebox_vectorscale USING diskann (embedding vector_l2_ops)' \ - -c "SELECT indexdef FROM pg_indexes WHERE indexname = 'onebox_vectorscale_diskann'" \ - -c 'SET enable_seqscan = off' \ - -c "SELECT id FROM onebox_vectorscale ORDER BY embedding <-> '[1,2,3]' LIMIT 1" \ - -c "SELECT ST_AsText(ST_Point(1, 2))" \ - -c "SELECT cron.schedule('onebox-smoke', '0 0 * * *', 'SELECT 1')" \ - -c "SELECT cron.unschedule('onebox-smoke')" \ - -c 'CREATE TABLE onebox_hypopg(id bigint)' \ - -c "SELECT indexrelid IS NOT NULL FROM hypopg_create_index('CREATE INDEX ON onebox_hypopg(id)')" \ - -c "SELECT extname, extversion FROM pg_extension WHERE extname IN ('vector', 'vectorscale', 'postgis', 'pg_cron', 'pgaudit', 'pg_repack', 'pg_partman', 'hypopg', 'pg_stat_statements') ORDER BY extname" - docker exec "$container" pg_repack --version + - name: Verify exact published platform image + run: build/package/postgres-verify.sh "$CANDIDATE" publish: name: Publish PostgreSQL 18 image - needs: verify + needs: platform runs-on: ubuntu-24.04 permissions: contents: read packages: write steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - - uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3 - - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3 @@ -97,66 +79,27 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and publish commit-addressed candidate - uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6 - with: - context: . - file: build/package/postgres.Dockerfile - platforms: linux/amd64,linux/arm64 - push: true - tags: ghcr.io/labstack/onebox-postgres:18-${{ github.sha }} - provenance: mode=max - sbom: true - - - name: Verify exact published platform images - shell: bash + - name: Merge verified platforms into commit-addressed candidate run: | - set -euo pipefail candidate="ghcr.io/labstack/onebox-postgres:18-${GITHUB_SHA}" - for platform in linux/amd64 linux/arm64; do - architecture="${platform#linux/}" - container="onebox-postgres-${architecture}" - trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT - docker run -d --platform "$platform" --name "$container" -e POSTGRES_PASSWORD=test "$candidate" \ - -c shared_preload_libraries=pg_cron,pgaudit,pg_stat_statements \ - -c cron.database_name=postgres \ - -c cron.use_background_workers=on - for attempt in $(seq 1 60); do - if docker exec "$container" pg_isready -U postgres -d postgres >/dev/null 2>&1; then - break - fi - if [ "$attempt" -eq 60 ]; then - docker logs "$container" - exit 1 - fi - sleep 1 - done - docker exec "$container" psql -U postgres -d postgres -v ON_ERROR_STOP=1 \ - -c 'CREATE EXTENSION vector' \ - -c 'CREATE EXTENSION vectorscale' \ - -c 'CREATE EXTENSION postgis' \ - -c 'CREATE EXTENSION pg_cron' \ - -c 'CREATE EXTENSION pgaudit' \ - -c 'CREATE EXTENSION pg_repack' \ - -c 'CREATE EXTENSION pg_partman' \ - -c 'CREATE EXTENSION hypopg' \ - -c 'CREATE EXTENSION pg_stat_statements' \ - -c "SELECT '[1,2,3]'::vector <-> '[1,2,4]'::vector" \ - -c 'CREATE TABLE onebox_vectorscale(id bigint, embedding vector(3))' \ - -c "INSERT INTO onebox_vectorscale VALUES (1, '[1,2,3]'), (2, '[4,5,6]'), (3, '[7,8,9]')" \ - -c 'CREATE INDEX onebox_vectorscale_diskann ON onebox_vectorscale USING diskann (embedding vector_l2_ops)' \ - -c "SELECT indexdef FROM pg_indexes WHERE indexname = 'onebox_vectorscale_diskann'" \ - -c 'SET enable_seqscan = off' \ - -c "SELECT id FROM onebox_vectorscale ORDER BY embedding <-> '[1,2,3]' LIMIT 1" \ - -c "SELECT ST_AsText(ST_Point(1, 2))" \ - -c "SELECT cron.schedule('onebox-smoke', '0 0 * * *', 'SELECT 1')" \ - -c "SELECT cron.unschedule('onebox-smoke')" \ - -c 'CREATE TABLE onebox_hypopg(id bigint)' \ - -c "SELECT indexrelid IS NOT NULL FROM hypopg_create_index('CREATE INDEX ON onebox_hypopg(id)')" - docker exec "$container" pg_repack --version - docker exec "$container" wal-g --version - docker rm -f "$container" - trap - EXIT + docker buildx imagetools create --tag "$candidate" "${candidate}-amd64" "${candidate}-arm64" + # Promote only an index holding exactly the two verified platforms, + # each at the digest its own runner tested. + platforms=$(docker buildx imagetools inspect "$candidate" --raw | + jq -r '[.manifests[] | select(.platform.os == "linux") | .platform.architecture] | sort | join(",")') + if [ "$platforms" != "amd64,arm64" ]; then + echo "::error::merged index has platforms '$platforms', want amd64,arm64" + exit 1 + fi + for arch in amd64 arm64; do + tested=$(docker buildx imagetools inspect "${candidate}-${arch}" --raw | + jq -r --arg arch "$arch" '.manifests[] | select(.platform.architecture == $arch) | .digest') + merged=$(docker buildx imagetools inspect "$candidate" --raw | + jq -r --arg arch "$arch" '.manifests[] | select(.platform.architecture == $arch) | .digest') + if [ -z "$tested" ] || [ "$tested" != "$merged" ]; then + echo "::error::$arch digest in merged index ($merged) differs from tested candidate ($tested)" + exit 1 + fi done - name: Promote tested manifest to PostgreSQL 18 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a7f9dce0..ac3c9af3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -116,8 +116,8 @@ jobs: fi docker manifest inspect ghcr.io/labstack/onebox-postgres:18 >/dev/null - - uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3 - + # No QEMU: the Go stage cross-compiles on the build platform and the + # scratch stage runs no commands, so no step executes foreign binaries. - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3 - uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3 @@ -134,6 +134,7 @@ jobs: context: . file: build/package/onebox-discovery.Dockerfile platforms: linux/amd64,linux/arm64 + cache-from: type=gha,scope=onebox-discovery push: true tags: ghcr.io/labstack/onebox-discovery:${{ github.ref_name }} provenance: mode=max diff --git a/build/package/onebox-discovery.Dockerfile b/build/package/onebox-discovery.Dockerfile index 2635892b..c357054e 100644 --- a/build/package/onebox-discovery.Dockerfile +++ b/build/package/onebox-discovery.Dockerfile @@ -8,7 +8,10 @@ COPY go.mod go.sum ./ RUN go mod download COPY cmd/onebox-discovery ./cmd/onebox-discovery COPY internal/discovery ./internal/discovery -RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \ +# Modules stay in the layer above so a CI layer-cache hit skips downloads; +# the build cache mount only speeds up local rebuilds. +RUN --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \ go build -trimpath -ldflags="-s -w" -o /out/onebox-discovery ./cmd/onebox-discovery FROM scratch diff --git a/build/package/postgres-bump.sh b/build/package/postgres-bump.sh new file mode 100755 index 00000000..618ec9ff --- /dev/null +++ b/build/package/postgres-bump.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +# Refresh the pins in the onebox-postgres Dockerfile in place: +# - POSTGRES_IMAGE and DEBIAN_IMAGE move to the digest their tag points at now. +# - Each pgdg extension package moves to the newest build apt.postgresql.org +# offers for the new base image. +# WAL-G, pgvector, and pgvectorscale are pinned by checksum or commit and stay +# as they are; bump those by hand. Prints one line per changed pin. +# Usage: build/package/postgres-bump.sh [DOCKERFILE] +set -euo pipefail + +dockerfile=${1:-build/package/postgres.Dockerfile} + +arg_value() { + sed -n "s/^ARG $1=//p" "$dockerfile" +} + +set_arg() { + local name=$1 new=$2 old + old=$(arg_value "$name") + if [ -z "$old" ]; then + echo "ARG $name not found in $dockerfile" >&2 + exit 1 + fi + if [ "$old" != "$new" ]; then + # '|' delimits: image references and Debian versions never contain it. + sed -i.bak "s|^ARG $name=.*|ARG $name=$new|" "$dockerfile" + rm -f "$dockerfile.bak" + echo "$name: $old -> $new" + fi +} + +bump_image() { + local name=$1 ref tag digest + ref=$(arg_value "$name") + tag=${ref%@*} + digest=$(docker buildx imagetools inspect "$tag" --format '{{json .Manifest}}' | jq -r .digest) + case "$digest" in + sha256:*) ;; + *) echo "could not resolve a digest for $tag" >&2; exit 1 ;; + esac + set_arg "$name" "$tag@$digest" +} + +bump_image POSTGRES_IMAGE +bump_image DEBIAN_IMAGE + +pg_major=$(arg_value PG_MAJOR) +postgres_image=$(arg_value POSTGRES_IMAGE) +case "$postgres_image" in + "postgres:$pg_major-"*) ;; + *) echo "PG_MAJOR=$pg_major does not match POSTGRES_IMAGE=$postgres_image" >&2; exit 1 ;; +esac +packages=( + "POSTGIS_PACKAGE_VERSION=postgresql-$pg_major-postgis-3" + "PG_CRON_PACKAGE_VERSION=postgresql-$pg_major-cron" + "PGAUDIT_PACKAGE_VERSION=postgresql-$pg_major-pgaudit" + "PG_REPACK_PACKAGE_VERSION=postgresql-$pg_major-repack" + "PG_PARTMAN_PACKAGE_VERSION=postgresql-$pg_major-partman" + "HYPOPG_PACKAGE_VERSION=postgresql-$pg_major-hypopg" +) +names=() +for entry in "${packages[@]}"; do + names+=("${entry#*=}") +done + +# Ask the new base image, once per published platform, which builds its pgdg +# source offers now. pgdg can publish a build for one architecture before the +# other, so a pin moves only to a version both offer. +candidates() { + docker run --rm --platform "$1" "$postgres_image" sh -c ' + apt-get update -qq >/dev/null || { echo "apt-get update failed" >&2; exit 1; } + for package in "$@"; do + printf "%s %s\n" "$package" "$(apt-cache policy "$package" | sed -n "s/^ *Candidate: //p")" + done' sh "${names[@]}" +} +amd64=$(candidates linux/amd64) +arm64=$(candidates linux/arm64) + +for entry in "${packages[@]}"; do + name=${entry%%=*} + package=${entry#*=} + version=$(printf '%s\n' "$amd64" | awk -v p="$package" '$1 == p { print $2 }') + other=$(printf '%s\n' "$arm64" | awk -v p="$package" '$1 == p { print $2 }') + if [ -z "$version" ] || [ "$version" = "(none)" ]; then + echo "no candidate for $package" >&2 + exit 1 + fi + if [ "$version" != "$other" ]; then + echo "$package: amd64 offers $version, arm64 offers ${other:-nothing}; keeping the current pin" >&2 + continue + fi + set_arg "$name" "$version" +done diff --git a/build/package/postgres-verify.sh b/build/package/postgres-verify.sh new file mode 100755 index 00000000..d1b8a373 --- /dev/null +++ b/build/package/postgres-verify.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# Smoke-test a built onebox-postgres image: start it, create every bundled +# extension, exercise each one, and check that build-only tools stayed out. +# Usage: build/package/postgres-verify.sh IMAGE +set -euo pipefail +if [ "$#" -ne 1 ]; then + echo "usage: $0 IMAGE" >&2 + exit 2 +fi +CANDIDATE=$1 +container=onebox-postgres-verify-$$ +trap 'docker rm -f "$container" >/dev/null 2>&1 || true' EXIT +docker run -d --name "$container" -e POSTGRES_PASSWORD=test "$CANDIDATE" \ + -c shared_preload_libraries=pg_cron,pgaudit,pg_stat_statements \ + -c cron.database_name=postgres \ + -c cron.use_background_workers=on +for attempt in $(seq 1 60); do + # The entrypoint init server listens only on the Unix socket; TCP + # answers only once the real server is up. + if docker exec "$container" pg_isready -h 127.0.0.1 -U postgres -d postgres >/dev/null 2>&1; then + break + fi + if [ "$attempt" -eq 60 ]; then + docker logs "$container" + exit 1 + fi + sleep 1 +done +docker exec "$container" psql -U postgres -d postgres -v ON_ERROR_STOP=1 \ + -c 'CREATE EXTENSION vector' \ + -c 'CREATE EXTENSION vectorscale' \ + -c 'CREATE EXTENSION postgis' \ + -c 'CREATE EXTENSION pg_cron' \ + -c 'CREATE EXTENSION pgaudit' \ + -c 'CREATE EXTENSION pg_repack' \ + -c 'CREATE EXTENSION pg_partman' \ + -c 'CREATE EXTENSION hypopg' \ + -c 'CREATE EXTENSION pg_stat_statements' \ + -c "SELECT '[1,2,3]'::vector <-> '[1,2,4]'::vector" \ + -c 'CREATE TABLE onebox_vectorscale(id bigint, embedding vector(3))' \ + -c "INSERT INTO onebox_vectorscale VALUES (1, '[1,2,3]'), (2, '[4,5,6]'), (3, '[7,8,9]')" \ + -c 'CREATE INDEX onebox_vectorscale_diskann ON onebox_vectorscale USING diskann (embedding vector_l2_ops)' \ + -c "SELECT indexdef FROM pg_indexes WHERE indexname = 'onebox_vectorscale_diskann'" \ + -c 'SET enable_seqscan = off' \ + -c "SELECT id FROM onebox_vectorscale ORDER BY embedding <-> '[1,2,3]' LIMIT 1" \ + -c "SELECT ST_AsText(ST_Point(1, 2))" \ + -c "SELECT cron.schedule('onebox-smoke', '0 0 * * *', 'SELECT 1')" \ + -c "SELECT cron.unschedule('onebox-smoke')" \ + -c 'CREATE TABLE onebox_hypopg(id bigint)' \ + -c "SELECT indexrelid IS NOT NULL FROM hypopg_create_index('CREATE INDEX ON onebox_hypopg(id)')" \ + -c "SELECT extname, extversion FROM pg_extension WHERE extname IN ('vector', 'vectorscale', 'postgis', 'pg_cron', 'pgaudit', 'pg_repack', 'pg_partman', 'hypopg', 'pg_stat_statements') ORDER BY extname" +docker exec "$container" pg_repack --version +docker exec "$container" wal-g --version +# WAL-G reaches object storage over TLS and needs the CA bundle. +docker exec "$container" test -s /etc/ssl/certs/ca-certificates.crt +# Build tools belong to the pgvector-build and fetch stages only. +if docker exec "$container" sh -c 'for tool in gcc cc clang make ld llvm-config curl unzip; do command -v "$tool" && exit 0; done; ls -d /usr/lib/llvm-*/bin/clang 2>/dev/null && exit 0; exit 1'; then + echo "::error::build tools leaked into the final image" + exit 1 +fi + diff --git a/build/package/postgres.Dockerfile b/build/package/postgres.Dockerfile index ba545ebe..126ed6ec 100644 --- a/build/package/postgres.Dockerfile +++ b/build/package/postgres.Dockerfile @@ -1,108 +1,127 @@ # syntax=docker/dockerfile:1 +# PG_MAJOR must match the major version in POSTGRES_IMAGE; change both together. ARG PG_MAJOR=18 -ARG DEBIAN_CODENAME=trixie +ARG PGVECTORSCALE_VERSION=0.9.0 +# Digest pins fix the base images, so PostgreSQL minor releases and Debian +# security updates arrive only by bumping these digests. The pgdg extension +# packages below are pinned too, but apt.postgresql.org keeps only recent +# builds: a rebuild without a warm layer cache fails once a pinned version +# leaves the index, and the fix is to bump that pin. +# .github/workflows/postgres-image-bump.yml refreshes these pins weekly in a +# pull request; merging it, or any change to this file or postgres-verify.sh, +# republishes the image. +ARG POSTGRES_IMAGE=postgres:18-trixie@sha256:64c8b0d31e4ab6ea665e8426ea406a94bf52de3c05b3a4a8bf713704eefa9328 +ARG DEBIAN_IMAGE=debian:trixie-slim@sha256:8aa5a7bf5a1955bddbe7d74fcdd0b91ab30e74362a8b73af672b0481b35b44a0 -FROM debian:trixie-slim AS walg-build +# Downloads and checksum checks run here so curl and unzip never enter the +# final image. +# The stage only downloads and checksums, so it runs on the build platform and +# never needs emulation. TARGETARCH still selects the assets. +FROM --platform=$BUILDPLATFORM ${DEBIAN_IMAGE} AS fetch -# The official release has separate Linux binaries. This mapping is build-time -# only: Buildx selects TARGETARCH and the resulting multi-arch image manifest -# selects the matching image at the host. ob neither detects an architecture -# nor transfers a WAL-G binary. +# The official releases have separate Linux binaries. This mapping is +# build-time only: Buildx selects TARGETARCH and the resulting multi-arch image +# manifest selects the matching image at the host. ob neither detects an +# architecture nor transfers a WAL-G binary. +ARG PG_MAJOR ARG TARGETARCH ARG WALG_VERSION=v3.0.8 ARG WALG_AMD64_SHA256=f30544c5ce93cf83b87578e3c4a2e9c0e0ffc3d160ef89ecddaf75f397d98deb ARG WALG_ARM64_SHA256=794d1a81f0c27825a1603bd39c0f2cf5dd8bed7cc36b598ca05d8d963c3d5fcf +ARG PGVECTORSCALE_COMMIT=c66cae4b621664b68546587da9fafd80b791e643 +ARG PGVECTORSCALE_VERSION +ARG PGVECTORSCALE_AMD64_SHA256=7a5450b81a7403ca20ff5e5a2f81aa13c81795ddd1fdfe9b986c42c48b12ed67 +ARG PGVECTORSCALE_ARM64_SHA256=8d0916df999f082ceb3d019bdfa72f5df395c31b152f7906de59e429ee11edc7 RUN apt-get update && \ - apt-get install -y --no-install-recommends ca-certificates curl && \ + apt-get install -y --no-install-recommends ca-certificates curl unzip && \ case "${TARGETARCH}" in \ - amd64) walg_asset="wal-g-pg-22.04-amd64"; walg_sha="${WALG_AMD64_SHA256}" ;; \ - arm64) walg_asset="wal-g-pg-22.04-aarch64"; walg_sha="${WALG_ARM64_SHA256}" ;; \ - *) echo "unsupported WAL-G architecture: ${TARGETARCH}" >&2; exit 1 ;; \ + amd64) walg_asset="wal-g-pg-22.04-amd64"; walg_sha="${WALG_AMD64_SHA256}"; pgvectorscale_sha="${PGVECTORSCALE_AMD64_SHA256}" ;; \ + arm64) walg_asset="wal-g-pg-22.04-aarch64"; walg_sha="${WALG_ARM64_SHA256}"; pgvectorscale_sha="${PGVECTORSCALE_ARM64_SHA256}" ;; \ + *) echo "unsupported architecture: ${TARGETARCH}" >&2; exit 1 ;; \ esac && \ - install -d /out && \ - curl -fsSL "https://github.com/wal-g/wal-g/releases/download/${WALG_VERSION}/${walg_asset}" -o /out/wal-g && \ - echo "${walg_sha} /out/wal-g" | sha256sum -c - && \ - chmod 0755 /out/wal-g + install -d /out/bin /out/deb /out/doc/pgvectorscale && \ + curl -fsSL "https://github.com/wal-g/wal-g/releases/download/${WALG_VERSION}/${walg_asset}" -o /out/bin/wal-g && \ + echo "${walg_sha} /out/bin/wal-g" | sha256sum -c - && \ + chmod 0755 /out/bin/wal-g && \ + pgvectorscale_archive="pgvectorscale-${PGVECTORSCALE_VERSION}-pg${PG_MAJOR}-${TARGETARCH}.zip" && \ + curl -fsSL \ + "https://github.com/timescale/pgvectorscale/releases/download/${PGVECTORSCALE_VERSION}/${pgvectorscale_archive}" \ + -o "/tmp/${pgvectorscale_archive}" && \ + echo "${pgvectorscale_sha} /tmp/${pgvectorscale_archive}" | sha256sum -c - && \ + unzip -j "/tmp/${pgvectorscale_archive}" \ + "pgvectorscale-postgresql-${PG_MAJOR}_${PGVECTORSCALE_VERSION}-Linux_${TARGETARCH}.deb" \ + -d /out/deb && \ + curl -fsSL \ + "https://raw.githubusercontent.com/timescale/pgvectorscale/${PGVECTORSCALE_COMMIT}/LICENSE" \ + -o /out/doc/pgvectorscale/LICENSE && \ + curl -fsSL \ + "https://raw.githubusercontent.com/timescale/pgvectorscale/${PGVECTORSCALE_COMMIT}/NOTICE" \ + -o /out/doc/pgvectorscale/NOTICE && \ + echo "df34f0384d53261f4dc47b3d834a13b570f177b8ab1c8a00266a98f30de2e117 /out/doc/pgvectorscale/LICENSE" | sha256sum -c - && \ + echo "4e204f7b0aa175af0a3b38c3bc56852954adf0110e25babe94eab6e35eeef114 /out/doc/pgvectorscale/NOTICE" | sha256sum -c - -FROM postgres:${PG_MAJOR}-${DEBIAN_CODENAME} +# pgvector compiles against the same server image it ships in. Only the files +# `make install` produces are copied out: the library, its LLVM bitcode, SQL +# scripts, and pgvector's own headers (which other extensions build against). +# The compiler, binutils, clang/LLVM, and postgresql-server-dev never reach the +# final image. +FROM ${POSTGRES_IMAGE} AS pgvector-build ARG PG_MAJOR -ARG TARGETARCH ARG PGVECTOR_COMMIT=8ee86c96f0fd72390f890aa8a336fda6d3ab4c6c ARG PGVECTOR_VERSION=0.8.6 -ARG PGVECTORSCALE_COMMIT=c66cae4b621664b68546587da9fafd80b791e643 -ARG PGVECTORSCALE_VERSION=0.9.0 -ARG PGVECTORSCALE_AMD64_SHA256=7a5450b81a7403ca20ff5e5a2f81aa13c81795ddd1fdfe9b986c42c48b12ed67 -ARG PGVECTORSCALE_ARM64_SHA256=8d0916df999f082ceb3d019bdfa72f5df395c31b152f7906de59e429ee11edc7 -ARG POSTGIS_PACKAGE_VERSION=3.6.4+dfsg-2.pgdg13+1 -ARG PG_CRON_PACKAGE_VERSION=1.6.7-3.pgdg13+1 -ARG PGAUDIT_PACKAGE_VERSION=18.0-3.pgdg13+1 -ARG PG_REPACK_PACKAGE_VERSION=1.5.3-1.pgdg13+1 -ARG PG_PARTMAN_PACKAGE_VERSION=5.5.0-1.pgdg13+1 -ARG HYPOPG_PACKAGE_VERSION=1.4.3-1.pgdg13+1 +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + build-essential \ + postgresql-server-dev-${PG_MAJOR} +# The full commit is immutable. BuildKit checks out that exact source instead +# of trusting a movable release tag. +ADD https://github.com/pgvector/pgvector.git#${PGVECTOR_COMMIT} /src/pgvector +WORKDIR /src/pgvector +RUN make OPTFLAGS="" && \ + make install DESTDIR=/out && \ + grep -Fqx "default_version = '${PGVECTOR_VERSION}'" vector.control && \ + install -d /out/usr/share/doc/pgvector && \ + install -m 0644 LICENSE README.md /out/usr/share/doc/pgvector/ && \ + printf '%s\n' "${PGVECTOR_VERSION}" > /out/usr/share/doc/pgvector/VERSION + +FROM ${POSTGRES_IMAGE} + +ARG PG_MAJOR +ARG TARGETARCH +ARG PGVECTORSCALE_VERSION +ARG POSTGIS_PACKAGE_VERSION=3.7.0+dfsg-2.pgdg13+1 +ARG PG_CRON_PACKAGE_VERSION=1.6.8-2.pgdg13+1 +ARG PGAUDIT_PACKAGE_VERSION=18.1-1.pgdg13+1 +ARG PG_REPACK_PACKAGE_VERSION=1.5.3-3.pgdg13+1 +ARG PG_PARTMAN_PACKAGE_VERSION=5.5.1-1.pgdg13+1 +ARG HYPOPG_PACKAGE_VERSION=1.4.5-2.pgdg13+1 LABEL org.opencontainers.image.source="https://github.com/labstack/onebox" \ org.opencontainers.image.description="PostgreSQL for Onebox-managed applications" \ org.opencontainers.image.licenses="PostgreSQL AND GPL-2.0-or-later" -COPY --from=walg-build /out/wal-g /usr/local/bin/wal-g +COPY --from=fetch /out/bin/wal-g /usr/local/bin/wal-g +COPY --from=fetch /out/doc/ /usr/share/doc/ +COPY --from=pgvector-build /out/ / -# The full commit is immutable. BuildKit checks out that exact source instead -# of trusting a movable release tag, and the final image contains no compiler. -ADD https://github.com/pgvector/pgvector.git#${PGVECTOR_COMMIT} /tmp/pgvector -RUN apt-get update && \ +# The pgvectorscale package is bind-mounted, not copied, so the .deb never +# lands in an image layer. ca-certificates is explicit: the base image does not +# ship a CA bundle, and WAL-G needs one to reach object storage over TLS. +RUN --mount=type=bind,from=fetch,source=/out/deb,target=/tmp/pgvectorscale \ + apt-get update && \ apt-mark hold locales && \ apt-get install -y --no-install-recommends \ - build-essential \ ca-certificates \ - curl \ - postgresql-server-dev-${PG_MAJOR} \ postgresql-${PG_MAJOR}-postgis-3=${POSTGIS_PACKAGE_VERSION} \ postgresql-${PG_MAJOR}-cron=${PG_CRON_PACKAGE_VERSION} \ postgresql-${PG_MAJOR}-pgaudit=${PGAUDIT_PACKAGE_VERSION} \ postgresql-${PG_MAJOR}-repack=${PG_REPACK_PACKAGE_VERSION} \ postgresql-${PG_MAJOR}-partman=${PG_PARTMAN_PACKAGE_VERSION} \ postgresql-${PG_MAJOR}-hypopg=${HYPOPG_PACKAGE_VERSION} \ - unzip && \ - cd /tmp/pgvector && \ - make clean && \ - make OPTFLAGS="" && \ - make install && \ - grep -Fqx "default_version = '${PGVECTOR_VERSION}'" vector.control && \ - install -d /usr/share/doc/pgvector && \ - install -m 0644 LICENSE README.md /usr/share/doc/pgvector/ && \ - printf '%s\n' "${PGVECTOR_VERSION}" > /usr/share/doc/pgvector/VERSION && \ - case "${TARGETARCH}" in \ - amd64) pgvectorscale_sha="${PGVECTORSCALE_AMD64_SHA256}" ;; \ - arm64) pgvectorscale_sha="${PGVECTORSCALE_ARM64_SHA256}" ;; \ - *) echo "unsupported pgvectorscale architecture: ${TARGETARCH}" >&2; exit 1 ;; \ - esac && \ - pgvectorscale_archive="pgvectorscale-${PGVECTORSCALE_VERSION}-pg${PG_MAJOR}-${TARGETARCH}.zip" && \ - curl -fsSL \ - "https://github.com/timescale/pgvectorscale/releases/download/${PGVECTORSCALE_VERSION}/${pgvectorscale_archive}" \ - -o "/tmp/${pgvectorscale_archive}" && \ - echo "${pgvectorscale_sha} /tmp/${pgvectorscale_archive}" | sha256sum -c - && \ - install -d /tmp/pgvectorscale-package && \ - unzip -j "/tmp/${pgvectorscale_archive}" \ - "pgvectorscale-postgresql-${PG_MAJOR}_${PGVECTORSCALE_VERSION}-Linux_${TARGETARCH}.deb" \ - -d /tmp/pgvectorscale-package && \ - apt-get install -y --no-install-recommends \ - "/tmp/pgvectorscale-package/pgvectorscale-postgresql-${PG_MAJOR}_${PGVECTORSCALE_VERSION}-Linux_${TARGETARCH}.deb" && \ + "/tmp/pgvectorscale/pgvectorscale-postgresql-${PG_MAJOR}_${PGVECTORSCALE_VERSION}-Linux_${TARGETARCH}.deb" && \ grep -Fqx "default_version = '${PGVECTORSCALE_VERSION}'" \ "/usr/share/postgresql/${PG_MAJOR}/extension/vectorscale.control" && \ - install -d /usr/share/doc/pgvectorscale && \ - curl -fsSL \ - "https://raw.githubusercontent.com/timescale/pgvectorscale/${PGVECTORSCALE_COMMIT}/LICENSE" \ - -o /usr/share/doc/pgvectorscale/LICENSE && \ - curl -fsSL \ - "https://raw.githubusercontent.com/timescale/pgvectorscale/${PGVECTORSCALE_COMMIT}/NOTICE" \ - -o /usr/share/doc/pgvectorscale/NOTICE && \ - echo "df34f0384d53261f4dc47b3d834a13b570f177b8ab1c8a00266a98f30de2e117 /usr/share/doc/pgvectorscale/LICENSE" | sha256sum -c - && \ - echo "4e204f7b0aa175af0a3b38c3bc56852954adf0110e25babe94eab6e35eeef114 /usr/share/doc/pgvectorscale/NOTICE" | sha256sum -c - && \ - cd / && \ - rm -rf /tmp/pgvector /tmp/pgvectorscale-package "/tmp/${pgvectorscale_archive}" && \ - apt-get remove -y build-essential curl postgresql-server-dev-${PG_MAJOR} unzip && \ - apt-get autoremove -y && \ apt-mark unhold locales && \ rm -rf /var/lib/apt/lists/*