:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(hogbox-preview): serve static assets from caddy (#113092)
Frank Hamand1f5a384e9c9a
diff --git a/tools/hogbox-preview/README.md b/tools/hogbox-preview/README.mdindex b6ac48bdea25..5af5531f5b2e 100644--- a/tools/hogbox-preview/README.md+++ b/tools/hogbox-preview/README.md@@ -51,6 +51,9 @@ print(url) # https://pen-….boxes.hogland.prod-us.posthog.dev/ (stable acrosstemplates into the restored database before the preview becomes available.6. **Sync feature flags** - run `sync_feature_flags` so each flag in the PR's `constants.tsx` that the golden lacks exists and is on.7. **Serve + report** — the box is HTTP-exposed; the URL is posted to the PR.+ A Caddy container (`static-proxy`) takes the exposed port.+ It serves `/static/*` from `staticfiles/` and sends all other requests to web, so asset requests do not use Django threads.+ A non-frontend PR copies `staticfiles/` out of the image. A file that is not there falls through to Django.Driven entirely by the **`posthog-hogland` Python SDK** over hogplane's HTTP API— **keyless** (GitHub OIDC → hogplane token over the tailnet), no `hogland` CLIdiff --git a/tools/hogbox-preview/hogbox_preview/stack.py b/tools/hogbox-preview/hogbox_preview/stack.pyindex f16b35c8ddf3..820ddf2b37c3 100644--- a/tools/hogbox-preview/hogbox_preview/stack.py+++ b/tools/hogbox-preview/hogbox_preview/stack.py@@ -53,6 +53,26 @@ _CSRF_TRUSTED_ORIGINS = ",".join(f"https://*.boxes.hogland.{env}.posthog.dev" fo# runs unchanged. Not importable here — the tool runs in an SDK-only env with no# posthog Django on the path — so they're duplicated with that source named. If# the seed defaults ever change, change these too.+_STATIC_PROXY_CADDYFILE = """\+:8000 {+ @static {+ path /static/*+ file {+ root /srv+ }+ }+ handle @static {+ root * /srv+ header Cache-Control "public, max-age=3600"+ file_server {+ precompressed br gzip+ }+ }+ handle {+ reverse_proxy web:8000+ }+}+"""_DEMO_EMAIL = "[email protected]"_DEMO_PASSWORD = "12345678"@@ -74,6 +94,8 @@ class PostHogPreviewStack:# Pass image=None to fall back to the build-from-checkout escape hatch.IMAGE = "ghcr.io/posthog/posthog:master"CDP_IMAGE = "ghcr.io/posthog/posthog-node:master"+ STATIC_PROXY_IMAGE = "caddy:2-alpine"+ STATIC_PROXY_CADDYFILE = "preview-static.Caddyfile"REPO_DIR = "/home/hog/posthog"COMPOSE = "docker-compose.dev-full.yml"OVERRIDE = "docker-compose.preview.yml"@@ -196,6 +218,8 @@ class PostHogPreviewStack:# run before up_web so the fresh web container reads the new index# template + collected statics.self.swap_frontend()+ elif self.image:+ self.export_image_static()self.up_web()self.wait_for_health()self.deep_health()@@ -330,9 +354,11 @@ class PostHogPreviewStack:# (JS_URL=""), not removing it. Keeping the env constant means web only# ever recreates for the mount, never for config drift.timing.stage("write compose override (backend mount)")+ self.backend.write_file(f"{self.repo_dir}/{self.STATIC_PROXY_CADDYFILE}", _STATIC_PROXY_CADDYFILE)lines = ["# Generated by tools/hogbox-preview.", "services:", " web:"]if self.image:lines.append(f" image: {self.image}")+ lines.append(" ports: !reset []")if self.image and self.mount:# Bind-mount PR backend source over the image's /code — no rebuild.# Compose concatenates volume lists across files, so dev-full's@@ -395,6 +421,18 @@ class PostHogPreviewStack:# as the rest of the preview and no more." - ADMIN_PORTAL_ENABLED=1",]+ lines += [+ " static-proxy:",+ f" image: {self.STATIC_PROXY_IMAGE}",+ " restart: always",+ " ports:",+ f" - {self.backend.web_port}:8000",+ " volumes:",+ f" - ./{self.STATIC_PROXY_CADDYFILE}:/etc/caddy/Caddyfile:ro",+ " - ./staticfiles:/srv/static:ro",+ " depends_on:",+ " - web",+ ]lines += [" plugins:",f" image: {self.CDP_IMAGE}",@@ -605,7 +643,9 @@ class PostHogPreviewStack:# mounts — the frontend mounts are web-only), so it just costs one more# container recreate on the deferred-swap path.timing.stage("start web + temporal worker containers")- self.backend.run_long(self._compose("up -d --no-build web temporal-django-worker"), name="up-web", timeout=900)+ self.backend.run_long(+ self._compose("up -d --no-build web temporal-django-worker static-proxy"), name="up-web", timeout=900+ )def migrate(self) -> None:# PostHog needs both: Postgres (schema) and ClickHouse (events DB +@@ -717,6 +757,18 @@ class PostHogPreviewStack:self.backend.run_long(script, name="frontend", timeout=900)timing.stage("frontend swap done (collectstatic done)")+ def export_image_static(self) -> None:+ assert self.image is not None+ timing.stage("export image static files for the static proxy")+ self.backend.run_long(+ f"cd {self.repo_dir} && rm -rf staticfiles && "+ f"cid=$(docker create {self.image}) && "+ 'docker cp "$cid":/code/staticfiles ./staticfiles; status=$?; '+ 'docker rm "$cid" >/dev/null; exit $status',+ name="export-static",+ timeout=600,+ )+def wait_for_health(self) -> None:# up_services already brought web up cleanly — just wait for it to serve.# Django is a heavy import; first health can take ~7 min.diff --git a/tools/hogbox-preview/tests/test_stack_admin_portal.py b/tools/hogbox-preview/tests/test_stack_admin_portal.pyindex f7907cb4b517..b91cfbd37bb9 100644--- a/tools/hogbox-preview/tests/test_stack_admin_portal.py+++ b/tools/hogbox-preview/tests/test_stack_admin_portal.py@@ -29,6 +29,7 @@ class _RecordingBackend:def __init__(self):self.web_url = "https://pen-test.boxes.example.dev"+ self.web_port = 8000self.files: dict[str, str] = {}def write_file(self, remote_path, content) -> None:diff --git a/tools/hogbox-preview/tests/test_stack_secret_key.py b/tools/hogbox-preview/tests/test_stack_secret_key.pyindex b7da663dffdc..5960b9ed85f5 100644--- a/tools/hogbox-preview/tests/test_stack_secret_key.py+++ b/tools/hogbox-preview/tests/test_stack_secret_key.py@@ -52,6 +52,7 @@ class _RecordingBackend:def __init__(self, stored_key: str = "", mints: bool = True):self.web_url = "https://pen-test.boxes.example.dev"+ self.web_port = 8000self.files: dict[str, str] = {}self.stored_key = stored_keyself.mints = mintsdiff --git a/tools/hogbox-preview/tests/test_swap_frontend.py b/tools/hogbox-preview/tests/test_swap_frontend.pyindex 80bd26181a15..2c21956f8f50 100644--- a/tools/hogbox-preview/tests/test_swap_frontend.py+++ b/tools/hogbox-preview/tests/test_swap_frontend.py@@ -41,6 +41,7 @@ class _RecordingBackend:without a real box."""def __init__(self):+ self.web_port = 8000self.files: dict[str, bytes | str] = {}self.long_runs: dict[str, str] = {}self.attached = False