: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
chore(devex): guard CI against image build-context drops (#63905)
Julian Bez52d5ae90174d
diff --git a/.github/dockerignore-drop-allowlist.txt b/.github/dockerignore-drop-allowlist.txtnew file mode 100644index 000000000000..2ef4b028f061--- /dev/null+++ b/.github/dockerignore-drop-allowlist.txt@@ -0,0 +1,10 @@+# Patterns (extended regex, matched against repo-relative paths) for files that are safe to drop+# from the production image build context, beyond the test/story/spec defaults baked into+# bin/dockerignore-drop-check.+#+# Adding a line here is the reviewed escape hatch: if a .dockerignore change intentionally excludes+# a non-test file and the guard fails, add the pattern here so reviewers see the acknowledgement in+# the diff. Keep entries narrow — a broad pattern re-opens the silent-drop hole the guard closes.+#+# Example:+# ^docs/internal/.*\.md$diff --git a/.github/workflows/container-images-cd.yml b/.github/workflows/container-images-cd.ymlindex 231a9fd14f98..042561e874aa 100644--- a/.github/workflows/container-images-cd.yml+++ b/.github/workflows/container-images-cd.yml@@ -138,6 +138,27 @@ jobs:event: 'posthog-image-build'properties: '{"status": "failure", "commit_hash": "${{ github.sha }}"}'+ # Boot the image we're about to ship before dispatching the deploy. A file missing from the+ # build context (e.g. dropped by an over-broad .dockerignore) surfaces here as an ImportError+ # and blocks the deploy, instead of crash-looping in production. These are the same vars the+ # Dockerfile's collectstatic uses; STATIC_COLLECTION=1 in particular lets Django boot without+ # live services or a real SECRET_KEY (settings/access.py otherwise exits on the default key).+ # Runs before the backend wait so a broken image fails fast; skipped on manual+ # workflow_dispatch (break-glass), like the backend gate below.+ - name: Boot Django from the built image+ if: github.event_name == 'push'+ env:+ IMAGE: ghcr.io/posthog/posthog:${{ github.sha }}+ run: |+ set -euo pipefail+ docker pull "$IMAGE"+ docker run --rm \+ -e SKIP_SERVICE_VERSION_REQUIREMENTS=1 \+ -e STATIC_COLLECTION=1 \+ -e DATABASE_URL=postgres:/// \+ -e REDIS_URL=redis:/// \+ "$IMAGE" sh -c 'python -c "import posthog.asgi" && python manage.py check'+# Gate the deploy on backend CI. The image is already built and pushed above# (ci-backend.yml runs in parallel on the same commit) — we only hold the# deploy *dispatch* to Charts until this commit's backend suite is green.diff --git a/.github/workflows/container-images-ci.yml b/.github/workflows/container-images-ci.ymlindex 72996729eb35..263b8be85495 100644--- a/.github/workflows/container-images-ci.yml+++ b/.github/workflows/container-images-ci.yml@@ -32,6 +32,7 @@ jobs:|| steps.filter.outputs.build_inputs == 'true'|| contains(github.event.pull_request.labels.*.name, 'hobby-preview')}}+ dockerignore_changed: ${{ steps.filter.outputs.dockerignore }}steps:- uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1id: app-token@@ -78,6 +79,28 @@ jobs:- .github/actions/build-n-cache-image/**- .dockerignore- pnpm-lock.yaml+ # Drives the fast PR-time guard below (models `COPY . .`, so it only reacts+ # to .dockerignore — Dockerfile COPY changes are covered by the image boot check).+ dockerignore:+ - .dockerignore++ dockerignore_guard:+ needs: changes+ name: Guard against dropped image files+ runs-on: ubuntu-24.04+ timeout-minutes: 10+ # PR-time only (event == pull_request), so it's skipped on merge_group. Needs no secrets or+ # docker login — unlike the image build below, it runs on fork PRs too.+ if: github.event_name == 'pull_request' && needs.changes.outputs.dockerignore_changed == 'true'+ permissions:+ contents: read+ steps:+ - name: Check out+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2+ - name: Fetch base ref for comparison+ run: git fetch --no-tags --depth=1 origin "${{ github.event.pull_request.base.sha }}"+ - name: Check for non-test files dropped from the image+ run: bin/dockerignore-drop-check "${{ github.event.pull_request.base.sha }}"posthog_build:needs: changesdiff --git a/bin/dockerignore-drop-check b/bin/dockerignore-drop-checknew file mode 100755index 000000000000..be941cf5a5c0--- /dev/null+++ b/bin/dockerignore-drop-check@@ -0,0 +1,120 @@+#!/usr/bin/env bash+#+# Catch .dockerignore changes that silently drop non-test files from the image build context.+#+# The production Dockerfile copies whole directories (e.g. `COPY products/ products/`), so any file+# the .dockerignore excludes never reaches the image — the build still succeeds, then prod breaks at+# runtime. This builds the post-.dockerignore build context two ways (current vs a base ref's+# .dockerignore, same working tree) and lists files the change removes.+#+# Routine exclusions (tests, stories, specs) drop hundreds of files and are expected, so they're+# filtered out — only "anomalies" (files that don't look like test/dev artifacts) fail the check.+# A test-named file that's actually imported at runtime won't be caught here; the image boot check+# (manage.py check on the built image) is the net for that.+#+# Usage: bin/dockerignore-drop-check <base-ref>+# <base-ref> git ref to compare against (e.g. origin/master, or a merge-base SHA)+#+# Extra safe-to-drop patterns (ERE, one per line, # comments ok) can live in+# .github/dockerignore-drop-allowlist.txt — adding to it is the reviewed escape hatch for an+# intentional non-test exclusion.++set -euo pipefail++# Manifests are sorted with `LC_ALL=C sort` inside the container; pin the host side to the same+# collation so `comm` agrees and doesn't choke on "input is not sorted".+export LC_ALL=C++base_ref="${1:?usage: bin/dockerignore-drop-check <base-ref>}"++# Files that are always fine to drop from the image — never loaded at runtime.+SAFE_PATTERN='(\.test\.|\.spec\.|\.stories\.|(^|/)test_[^/]*\.py$|_test\.py$|/tests?/|/__tests__/|/__mocks__/|/e2e/|/cypress/|/\.storybook/|\.mock\.|/fixtures?/|/__snapshots__/|/snapshots?/)'+allowlist_file=".github/dockerignore-drop-allowlist.txt"++workdir="$(mktemp -d)"+dockerfile="$workdir/Dockerfile.context-lister"+base_ignore="$workdir/base.dockerignore"+backup_ignore="$workdir/head.dockerignore"+head_manifest="$workdir/head.txt"+base_manifest="$workdir/base.txt"+img="dockerignore-context-lister:$$"++# shellcheck disable=SC2329 # invoked indirectly via `trap ... EXIT`+cleanup() {+ # Always restore the real .dockerignore, even if the build is interrupted.+ [ -f "$backup_ignore" ] && cp "$backup_ignore" .dockerignore+ docker image rm -f "$img" >/dev/null 2>&1 || true+ rm -rf "$workdir"+}+trap cleanup EXIT++# Lists every file the build context contains after .dockerignore filtering, mirroring `COPY . .`.+cat > "$dockerfile" <<'DOCKERFILE'+FROM busybox+COPY . /ctx+RUN find /ctx -type f | sed 's#^/ctx/##' | LC_ALL=C sort > /manifest.txt+CMD ["cat", "/manifest.txt"]+DOCKERFILE++build_manifest() {+ local out="$1"+ DOCKER_BUILDKIT=1 docker build --quiet -f "$dockerfile" -t "$img" . >/dev/null+ docker run --rm "$img" > "$out"+}++# Filter stdin through grep, keeping lines that do NOT match (-v). grep exit 1 ("no matches") means+# everything got filtered out — a valid result, not a failure. But grep exit >=2 is a real error+# (e.g. a malformed regex in the allowlist); fail loud instead of letting a blanket `|| true` swallow+# it and wave dropped runtime files through as a false "OK".+filter_drops() {+ local status=0+ grep "$@" || status=$?+ if [ "$status" -ge 2 ]; then+ echo "error: grep exited $status while filtering dropped files (check $allowlist_file for an invalid regex)" >&2+ return "$status"+ fi+ return 0+}++# Base .dockerignore from the ref we compare against. If the ref never had one, there's nothing to+# regress against — every file was already included, so any current exclusion is intentional.+if ! git show "$base_ref:.dockerignore" > "$base_ignore" 2>/dev/null; then+ echo "No .dockerignore at $base_ref — nothing to compare, skipping."+ exit 0+fi++cp .dockerignore "$backup_ignore"++# Same working tree, two .dockerignore files: the delta is purely the .dockerignore change.+build_manifest "$head_manifest"+cp "$base_ignore" .dockerignore+build_manifest "$base_manifest"+cp "$backup_ignore" .dockerignore++# Files in the base context but not the head context = dropped by this .dockerignore change.+dropped="$(comm -23 "$base_manifest" "$head_manifest" || true)"++if [ -z "$dropped" ]; then+ echo "OK — .dockerignore change drops no files from the image build context."+ exit 0+fi++# Filter out files that are never needed at runtime (tests, stories, fixtures, plus team allowlist).+anomalies="$(printf '%s\n' "$dropped" | filter_drops -vE "$SAFE_PATTERN")"+if [ -f "$allowlist_file" ]; then+ anomalies="$(printf '%s\n' "$anomalies" | filter_drops -vEf <(grep -vE '^\s*(#|$)' "$allowlist_file"))"+fi++if [ -z "$anomalies" ]; then+ echo "OK — drops $(printf '%s\n' "$dropped" | grep -c .) file(s), all match test/dev patterns (not shipped at runtime)."+ exit 0+fi++echo "This .dockerignore change removes $(printf '%s\n' "$anomalies" | grep -c .) non-test file(s) from the production image:"+echo+printf '%s\n' "$anomalies" | sed 's/^/ - /'+echo+echo "These are in the repo but won't ship in the image — the build will pass, then prod may break"+echo "at runtime. Narrow the .dockerignore pattern, or, if a drop is intentional, add a pattern to"+echo "$allowlist_file (reviewed) to acknowledge it."+exit 1diff --git a/hogli.yaml b/hogli.yamlindex dcffe750dbae..4dcb3b8fdf2d 100644--- a/hogli.yaml+++ b/hogli.yaml@@ -1040,6 +1040,10 @@ environment:bin_script: build-dashboard-widget-types.pydescription: Widget enum preflight, date options, and form-field manifest codegenhidden: true+ dockerignore:drop:check:+ bin_script: dockerignore-drop-check+ description: List files an .dockerignore change drops from the image build context+ hidden: trueverify:metrics:pipe:bin_script: verify-metrics-pipedescription: 'Verify the local metrics pipe reached ClickHouse metrics1'