Skip to content

feat(publish): store the site snapshot in Workers KV from the API - #8

Merged
sakib412 merged 4 commits into
mainfrom
claude/snapshot-kv
Nov 19, 2026
Merged

sakib412 merged 4 commits into
mainfrom
claude/snapshot-kv

Conversation

@sakib412

Copy link
Copy Markdown
Member

What and why

The publish workflow no longer reads Supabase. The API builds the site snapshot, stores it in Cloudflare Workers KV, and fires the publish workflow with the snapshot's hash; CI downloads that snapshot and builds from it. This removes the database credential from GitHub Actions and Go from publish runs, and stays on free plans with no card. R2 was rejected because it needs one. See ADR 0002 and docs/research/publish-snapshot-source.md.

Closes #7

The API publishes the snapshot (internal/republish):

  • Publisher implements the existing Notifier, so the middleware and republish.Skip() are unchanged.
  • It runs on one background goroutine. Each run builds the snapshot, writes snapshot:<hash> (30-day TTL) and then latest, and dispatches with client_payload.hash.
  • Edits that arrive during a run merge into one next run. Nothing is written or dispatched when the hash is unchanged, and a failure is retried on the next trigger.
  • It runs on content writes, at API boot, at 00:05 Asia/Dhaka (replacing the workflow cron), and on the new admin-only POST /site/republish. That route needs permission site.publish and sends force so an unchanged snapshot still deploys.
  • KV is a small REST client for Workers KV.
  • With no SNAPSHOT_KV_* set (local dev), writes dispatch as before.

The workflow:

  • Go and the database step are removed.
  • It fetches snapshot:<hash>, or latest for push and manual runs. It retries for about two minutes because KV is eventually consistent.
  • Without KV secrets it builds the fixture and never deploys it.

Also in this PR: fix(web). The committee "Terms" nav linked to terms with no committee, such as /committee/2019-20, which made the prerender fail with a 404. It now links only to terms that have a committee.

How to check it

  • Run EKMAN_TEST_DATABASE_URL=postgres://binary:binary@localhost:5433/binarysust?sslmode=disable go test -race ./... in apps/api. cmd/server/publish_test.go covers write → store → dispatch, boot with KV in sync, bursts, unchanged content, KV failure and retry, the nightly run, republish-now, and the no-KV fallback.

  • Before merging, do the setup in docs/DEPLOY.md step 5, "Snapshot store":

    1. Create the KV namespace and the edit and read tokens.
    2. Put the API values in /etc/binarysust-api.env and add the SNAPSHOT_KV_NAMESPACE_ID / SNAPSHOT_KV_READ_TOKEN secrets.
    3. Restart the API and check its log for republish: published snapshot.

    Without the secrets, publish runs build the fixture and don't deploy.

  • After one successful publish from KV, delete the SNAPSHOT_DATABASE_URL secret and the site_reader role (docs/DEPLOY.md step 1).

Known gaps (from review, deliberately left for later):

  • Background publish failures aren't logged with the request ID.
  • The tests use their own setup in cmd/server, not internal/testapp.
  • Some named test cases are missing or weak: a Skip() route, a 403 for a non-admin on republish-now, and asserting the error log.
  • The nightly schedule has no injectable clock; only NextRollover is tested.

The workflow change has only been checked with actionlint.

Checklist

  • The PR title follows Conventional Commits (feat(events): add waitlist emails); it becomes the squash commit.
  • pnpm lint, pnpm typecheck and pnpm test pass locally.
  • API changes: ran pnpm gen and committed apps/api/openapi.json and packages/api-client/src/schema.d.ts.
  • New migrations are new files (NNNN_*.sql); no applied migration was edited. (No migrations.)
  • UI changes: checked light and dark theme and a 360px-wide screen. (Only the committee term nav's links changed; not checked visually.)
  • Docs updated where behaviour changed (docs/, READMEs, CONTRIBUTING.md).

The API builds the snapshot in a background publisher, stores it under
snapshot:<hash> and latest in Workers KV, then dispatches the publish
workflow with the hash. CI downloads it instead of reading Supabase, so
SNAPSHOT_DATABASE_URL and the site_reader role go away. The nightly
rollover moves into the API, and admins can force POST /site/republish.

Closes #7
A forced run now sends client_payload.force, and the publish workflow skips
its live-hash check for it. Before, the workflow saw the live site already
served the hash and skipped the build, so republish now did nothing.
@sakib412
sakib412 merged commit 5b63028 into main Nov 19, 2026
3 checks passed
@sakib412
sakib412 deleted the claude/snapshot-kv branch November 19, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Publish: API stores the site snapshot in Workers KV; CI stops reading the database

1 participant