Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Self-Hosting

MergeSpan has three runtime shapes:

  1. Cloudflare Worker (default) with SQLite-backed Durable Objects.
  2. Ingress-only Worker (optional split) that receives webhooks in front of the main Worker.
  3. Native Linux process via systemd or Docker, with one SQLite WAL database per repository.

All shapes expose only GET /health and POST /webhook publicly. Put a TLS proxy or Cloudflare Tunnel in front of the native listener; it never terminates TLS itself.

Cloudflare Workers

Prerequisites

  • A Cloudflare account on the Free plan. Use only Workers Free with SQLite-backed Durable Objects (new_sqlite_classes, one DO per repository). Do not add paid resources.

  • worker-build 0.9.0, pinned:

    cargo install worker-build --version 0.9.0 --locked
    
  • Wrangler: Wrangler 4.169.1 was checked locally and wrangler deploy --help documents --tag and --message as Worker Version metadata. The repository does not pin a global Wrangler version; record wrangler --version before deploying and do not upgrade during a release.

  • Rust toolchain pinned by rust-toolchain.toml.

  • The web/ directory (static console). wrangler deploy requires it; worker-build does not.

Deploy

cd runtimes/cloudflare
wrangler login
wrangler secret put GITHUB_APP_PRIVATE_KEY   # full PEM at the prompt
wrangler secret put GITHUB_WEBHOOK_SECRET    # same value as the App's webhook settings
wrangler deploy                              # [build] runs worker-build --release
curl -fsS "https://<worker-host>/health"

For a tagged production release, deploy the verified Worker package instead of building from main; see Deploy a tagged release (Worker). That package has no build step and embeds the tag commit.

The top-level Wrangler configuration is the default deployment: mergespan and mergespan-ingress, which follow main. Production is the separate mergespan-prod Worker and its mergespan-prod-ingress ingress, selected with -e production; deploy them only from a vX.Y.Z release tag, never from main. Production has its own Durable Object namespace and SQLite storage, and it needs its own vars and secrets — do not reuse the dev App’s values. The first-time production setup and commands are in Deploy a tagged release (Worker).

GITHUB_APP_ID is a non-secret identifier. Set it as a plain [vars] entry in runtimes/cloudflare/wrangler.toml.

The Worker also hosts the console: Workers Static Assets stores web/, while /auth/* and /api/v1/* run the runtime-neutral dashboard service. Dashboard bindings are optional; without them, pages still serve but auth/API answer 503:

BindingKindPurpose
GITHUB_APP_CLIENT_IDplain varOAuth client ID
GITHUB_APP_CLIENT_SECRETsecretOAuth client secret
SESSION_SECRETsecret, ≥ 32 bytesSession signing; also derives the internal dashboard relay key with purpose mergespan/dashboard-do/v1

The OAuth callback is https://<host>/auth/callback. The App uses only its mandatory Metadata repository permission (read) for the collaborator-permission check; no Administration permission.

Ingress-only Worker (optional split)

The split exists because the Rust/Wasm webhook path exceeded 10 ms CPU on fresh isolates. The JS entry has had no 1102 errors in live runs on Workers Free; measurements and acceptance rules are in runtimes/cloudflare-ingress/README.md.

Its Wrangler entry is a zero-npm JavaScript ES module at runtimes/cloudflare-ingress/src/index.mjs with only GET /health and POST /webhook. There is no build command, npm install, Durable Object class, SQLite store, GitHub orchestration, RSA signing, or dashboard route.

The REPOSITORY binding is a cross-script binding to the existing mergespan / RepositoryObject namespace. It adds no migration, route, or authentication bypass. The Durable Object still owns deduplication.

cd runtimes/cloudflare-ingress
wrangler secret put GITHUB_WEBHOOK_SECRET   # same value as the existing Worker
wrangler deploy
curl -fsS "https://<ingress-host>/health"

Only GITHUB_WEBHOOK_SECRET is needed; no App ID or private key. Deploy the existing mergespan Worker first, because the binding targets it. For production, use wrangler deploy -e production (and wrangler secret put GITHUB_WEBHOOK_SECRET -e production); its value must match mergespan-prod’s, not the default Worker’s. Rollback: delete the ingress Worker and point the App webhook back at the existing Worker’s /webhook; no data migration is involved.

The DO alarm transport emits one non-secret marker {"event":"installation_token","result":"ok"} only when an App-JWT installation-token response passes the GitHub client’s acceptance checks. Failures emit no marker. It is observable with wrangler tail, contains no token/header/body data, and does not enable paid features or change quotas/backstop.

Native Linux (systemd)

The same engine runs as one native Linux process outside Cloudflare: one locked data directory may hold any number of repositories, each with its own SQLite WAL database under repos/repo-<numeric id>.sqlite3, and a static musl binary for x86_64 or aarch64.

Deliveries are routed by the signed numeric repository ID; each queue re-arms independently, and an outbound await in one repository does not block ingress or work for another. It exposes only GET /health and POST /webhook; it is HTTP-only, so put a TLS proxy or Cloudflare Tunnel in front of 127.0.0.1:8080 and never expose the listener directly.

Build

rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl
cargo zigbuild --locked --release -p mergespan-runtime-native \
  --target x86_64-unknown-linux-musl

CI builds and verifies both targets. See Versions and Upgrades for build verification details.

Install

install -m 0755 target/x86_64-unknown-linux-musl/release/mergespan /usr/local/bin/mergespan
install -D -m 0644 deploy/mergespan.toml /etc/mergespan/mergespan.toml
install -m 0644 deploy/mergespan.service /etc/systemd/system/mergespan.service
# Put the PEM and webhook secret in the two LoadCredential= files, then:
systemctl daemon-reload && systemctl enable --now mergespan
curl -fsS http://127.0.0.1:8080/health

Credential values are read only from files: either the --app-private-key-file / --webhook-secret-file flags or, by default, github-app-private-key and github-webhook-secret inside $CREDENTIALS_DIRECTORY (systemd LoadCredential=). They are never accepted in the environment, config file, command line, or stdout. Diagnostics are one-line redacted JSON.

Files:

One deliberate difference from the Worker: when repository_owner/name are configured and a correctly signed delivery arrives for a different repository, the response is 403 instead of the Worker’s 502-for-a-DO-rejection, so GitHub does not retry a routing mismatch this process is configured to refuse. Without that optional binding, any repository the App delivers is served by its own queue.

Operational behavior matches the Worker where it matters: raw X-Hub-Signature-256 verification with a 1 MiB body cap, 202 only after the delivery and signal are committed in one SQLite transaction, deduplication by X-GitHub-Delivery, read-first recovery for an interrupted squash, and an advisory data-directory lock that makes a second live instance fail fast. SIGTERM stops accepting, drains bounded, and leaves a recoverable checkpoint; Restart=on-failure re-arms persisted outbox work.

Docker

CI builds the binary once per architecture and passes the same prebuilt binary to the Dockerfile; the image has no compiler or package manager.

On a Linux host with default Docker UID mapping, prepare the private bind mounts before the first run. The state directory must be writable by UID 65532. Install copies of the credential files outside the repository, readable only by that UID, and mount them read-only; do not make credentials world-readable. With rootless Docker or user namespace remapping, use the host UID/GID mapped to container 65532:65532 for ownership instead.

sudo install -d -m 0700 -o 65532 -g 65532 /var/lib/mergespan-docker /etc/mergespan-docker
sudo install -m 0400 -o 65532 -g 65532 /path/to/github-app-private-key.pem /etc/mergespan-docker/
sudo install -m 0400 -o 65532 -g 65532 /path/to/github-webhook-secret /etc/mergespan-docker/
docker build -f deploy/Dockerfile \
  --build-arg BINARY=mergespan-linux-amd64 \
  -t mergespan-native:local dist

docker run -d --name mergespan \
  --read-only --user 65532:65532 \
  -p 127.0.0.1:8080:8080 \
  --mount type=bind,src=/var/lib/mergespan-docker,dst=/var/lib/mergespan \
  --mount type=bind,src=/etc/mergespan-docker,dst=/etc/mergespan,readonly \
  mergespan-native:local \
  --app-id 123456 --data-dir /var/lib/mergespan \
  --listen 0.0.0.0:8080 \
  --app-private-key-file /etc/mergespan/github-app-private-key.pem \
  --webhook-secret-file /etc/mergespan/github-webhook-secret

Inside a container, bind 0.0.0.0:8080; the host-side publish above keeps the port on host loopback only (-p 127.0.0.1:8080:8080). Never publish it on 0.0.0.0. The image runs as nonroot and is HTTP only; put a TLS proxy or Cloudflare Tunnel in front.

State layout

One WAL database per repository under <data-dir>/repos/repo-<positive decimal id>.sqlite3 (no leading zeroes). Startup refuses unexpected files or orphaned SQLite sidecars in repos/. An unexpected legacy <data-dir>/mergespan.sqlite3 (or any other database-looking file outside repos/) makes the process refuse to start; it never adopts, overwrites, or migrates it.

Operational notes

  • One process owns one data directory. A second process fails the advisory flock and exits with status 3 instead of touching the database.
  • SIGTERM stops accepting new webhooks, drains in-flight work for a bounded time, and leaves a recoverable checkpoint. systemd Restart=on-failure handles crashes; the next process re-arms persisted outbox work.
  • SQLite runs in WAL mode with synchronous=FULL; keep the data directory on local disk, not a network filesystem.
  • /health is liveness only. It does not prove GitHub credentials or network reachability.
  • GET /health and POST /webhook are the only public routes. Everything else is a 404, matching the Cloudflare Worker.

Local validation (CI and release checks)

rustup target add wasm32-unknown-unknown
cargo fmt --all --check
cargo clippy --locked --workspace --all-targets -- -D warnings
cargo test --locked --workspace
node --test runtimes/cloudflare-ingress/test/webhook.test.mjs
cargo check --locked \
  -p mergespan-core \
  -p mergespan-engine \
  -p mergespan-github \
  -p mergespan-runtime-cloudflare \
  --target wasm32-unknown-unknown
cargo install worker-build --version 0.9.0 --locked
cd runtimes/cloudflare && worker-build --release

The release workflow’s validate job also runs bash .github/scripts/check-release-tag.test.sh, bash .github/scripts/fetch-origin.test.sh, bash .github/scripts/fetch-release-artifacts.test.sh, bash .github/scripts/fetch-previous-release.test.sh, bash .github/scripts/check-release-tools.test.sh, bash .github/scripts/release-evidence.test.sh and bash .github/scripts/reap-job-target.test.sh. Run those scripts after changing release logic, and run actionlint .github/workflows/*.yml and shellcheck .github/scripts/*.sh if installed.

Release-tag, release-evidence and restart/upgrade checks are separate and can be run locally (none creates a tag or calls GitHub):

bash .github/scripts/check-release-tag.test.sh
bash .github/scripts/fetch-release-artifacts.test.sh
bash .github/scripts/release-evidence.test.sh
cargo build --locked -p mergespan-runtime-native
NATIVE_BIN=target/debug/mergespan .github/scripts/checkpoint-smoke.sh
NATIVE_BIN=target/debug/mergespan .github/scripts/upgrade-smoke.sh historical
node .github/scripts/worker-checkpoint-smoke.mjs --tarball dist/mergespan-worker-X.Y.Z.tar.gz

The Worker harness needs a built release tarball and a local Miniflare install; if either is unavailable it reports BLOCKED rather than replacing the check with a /health probe. See Versions and Upgrades.

The native runtime deliberately has no wasm target; the wasm check selects the portable packages explicitly so the Worker validation is not weakened.

Native static validation additionally runs in CI: cargo zigbuild --release -p mergespan-runtime-native for x86_64-unknown-linux-musl and aarch64-unknown-linux-musl, then rejects any binary with PT_INTERP or DT_NEEDED; CI smoke-tests the prebuilt amd64 binary and verifies the distroless Dockerfile definition (CI does not build the image).

CI runs one job named ci on pull_request to main, with contents: read, on a fixed GitHub-hosted ubuntu-24.04 runner. Same-repository pull requests always run; a fork pull request runs only when the repository is public. The workflow references no secrets.

Every MergeSpan workflow runs on GitHub-hosted runners only; no self-hosted runner is attached to the repository. Public repositories skip the ci-build Actions caches, because a fork executes untrusted workspace code on the same hosted runner class and a cache key is not a secrecy boundary. Operators should also expire or purge pre-existing Actions caches if they may have been written from untrusted workspace or tool input; this repository does not perform that cleanup automatically.

The cache/toolchain/check/build steps live in .github/actions/ci-build and are shared with the release workflow, so the commands cannot drift; the ci job name, trigger, runner, permissions and required-check status are unchanged. The job still classifies docs/web/full changes first. It records the compressed Worker size in the job summary and fails above the 1 MB budget — a target, not a measurement.

The shared ci-build action’s cargo test --locked --workspace step also runs a short seeded merge-queue invariant sample (Full CI A: real coordinator transitions, randomized SHA/checks/base/parent/tree inputs) inside the normal PR budget; there is no separate short-test CI step. The longer sample is #[ignore]d so it never runs in the default cargo test --workspace; the explicit long command, reused by the scheduled full-ci-nightly workflow and by downstream CI workstreams, is:

cargo test --locked -p mergespan-engine --lib full_ci -- --ignored

Set MERGESPAN_FULL_CI_SEEDS / MERGESPAN_FULL_CI_STEPS to scale the long sample. full-ci-nightly runs on a clean standard GitHub-hosted runner with contents: read and no secrets, and has a 20 minute job budget.