Skip to content

: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.

chore(ci): hand backend tests off to depot ci (#96913)

Raúl Negrón-Oterobe6132a6ce12
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index 1309d94cb085..1153c26f741f 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -8,6 +8,8 @@
# changes without a matching depot bump.
#
# Intentional deltas vs canonical (everything else is kept apples-to-apples):
+# - The `sample` job waits for GitHub Actions to hand the event off and exposes `sampled`
+# and `side_effects`, both true only when GitHub Actions handed the tests to this engine.
# - runs-on: depot-* labels instead of ubuntu-latest
# - action paths: ./.depot/actions/... instead of ./.github/actions/... (pnpm-install,
# setup-sqlx-cli, setup-python-cached, setup-uv, setup-turbo, and paths-filter mirror
@@ -126,8 +128,8 @@
# depot-exempt. Login is best-effort because these jobs only pull public images and can
# fall back to anonymous pulls, which canonical relies on too.
# - Path-filter persons-SQL is folded into migrations
-# - Snapshot mode matches canonical, but snapshot patch upload/commit is stripped;
-# canonical's per-schema-leg patch scoping (legacy legs exclude *.new_events_schema.ambr,
+# - Snapshot mode is pinned to check, because the snapshot patch upload/commit is stripped.
+# Canonical's per-schema-leg patch scoping (legacy legs exclude *.new_events_schema.ambr,
# events_json legs ship only those) is therefore depot-exempt too
# - Test durations restored from Depot Cache (posthog-test-durations- prefix), refreshed by
# .depot/workflows/ci-backend-update-test-timing.yml, so shard counts match GitHub Actions.
@@ -148,19 +150,14 @@
# Shards derive from .test_durations (Amdahl), restored from Depot Cache (else committed);
# .depot/workflows/ci-backend-update-test-timing.yml keeps that cache fresh.
name: '[optional, does not block merge] Backend CI'
-# OPTIONAL, non-blocking shadow of canonical Backend CI — its statuses must never gate
-# merging (hence the "[optional, does not block merge]" name prefix). The
-# CI_DEPOT_SHADOW_PERCENT Depot CI variable (0-100; 0 or unset = fully off) is both the
-# master switch and the eligible pull_request sample rate: when >0, source PR runs are sampled
-# at that percent. Trunk merge-queue PRs and draft PRs labeled `no-ci` are excluded before a
-# runner is allocated. Master (push) runs only check-migrations to prime the schema cache (test
-# matrices are skipped there); manual triggers always run. The `sample` job rolls deterministic
-# per-run dice; `changes` (root of the needs graph) only runs when sampled in, so the whole
-# workflow cascades off on a miss. This is a Depot variable, NOT a GitHub one —
-# Depot CI reads its own `vars` store. Set it with:
-# depot ci vars add CI_DEPOT_SHADOW_PERCENT=<n> --repo PostHog/posthog (n=0 disables)
+# OPTIONAL, non-blocking shadow of canonical Backend CI: its statuses must never gate
+# merging (hence the "[optional, does not block merge]" name prefix). Each event runs on
+# exactly one engine, and GitHub Actions picks it. The `sample` job waits for the
+# `Hand off backend tests to Depot CI` check from .github/workflows/ci-backend.yml and
+# `changes` (root of the needs graph) only runs when that check succeeded, so the whole
+# workflow cascades off otherwise. Manual triggers always run here.
# The drift check (.github/workflows/ci-backend-shadow-drift.yml) keeps this file in sync
-# with canonical; it runs whenever either file changes, independent of this variable.
+# with canonical; it runs whenever either file changes, independent of the percent.
on:
push:
branches:
@@ -177,8 +174,8 @@ on:
# Add the `run-ci-backend` label to force the full matrices on a PR; the
# `no-ci` label silences the workflow on a draft entirely. No
# labeled/unlabeled triggers, so a label takes effect from the next push
- # or from ready_for_review. Trunk merge-queue PRs are filtered by the
- # sampling gate because their source PR already supplied a trial run.
+ # or from ready_for_review. Every event, merge-queue PRs included, runs here
+ # only when GitHub Actions hands it off (see the `sample` job).
types: [opened, synchronize, reopened, ready_for_review]
concurrency:
# PRs: one active run per branch, cancel stale. Push: per-SHA so master
@@ -238,50 +235,78 @@ permissions:
actions: read
pull-requests: read
jobs:
- # A manual trigger bypasses sampling so an explicit full run cannot silently no-op.
- # Automatic runs still require a nonzero sampling percentage. Exclude automatic
- # runs that only duplicate work before allocating even this small runner.
+ # GitHub Actions alone decides which engine runs an event. Every other job here
+ # needs this one, so Depot CI runs nothing GitHub Actions did not hand off, and the
+ # tests and their side effects never run on both engines.
sample:
if: >-
- github.event_name == 'workflow_dispatch'
- || github.event_name == 'api'
- || (vars.CI_DEPOT_SHADOW_PERCENT != ''
- && vars.CI_DEPOT_SHADOW_PERCENT != '0'
- && (github.event_name != 'pull_request'
- || (!startsWith(github.head_ref, 'trunk-merge/')
- && (github.event.pull_request.draft != true
- || !contains(github.event.pull_request.labels.*.name, 'no-ci')))))
+ github.event_name != 'pull_request'
+ || github.event.pull_request.draft != true
+ || !contains(github.event.pull_request.labels.*.name, 'no-ci')
runs-on: depot-ubuntu-24.04
- timeout-minutes: 5
- name: '[optional, does not block merge] Sample run'
+ timeout-minutes: 15
+ name: '[optional, does not block merge] Wait for GitHub Actions to hand off backend tests'
+ # Depot scopes the ambient token by this block, and the workflow-level block has no
+ # `checks`, so without it the check-runs read below answers 403.
+ permissions:
+ checks: read
outputs:
- sampled: ${{ steps.roll.outputs.sampled }}
+ sampled: ${{ steps.handoff.outputs.handed_off }}
+ side_effects: ${{ steps.handoff.outputs.handed_off }}
steps:
- - name: Roll sampling dice
- id: roll
+ - name: Wait for the hand-off check from GitHub Actions
+ id: handoff
env:
- PERCENT: ${{ vars.CI_DEPOT_SHADOW_PERCENT }}
- RUN_ID: ${{ github.run_id }}
+ GH_TOKEN: ${{ github.token }}
+ REPO: ${{ github.repository }}
EVENT: ${{ github.event_name }}
+ IS_FORK: ${{ github.event.pull_request.head.repo.fork == true }}
+ SHA: ${{ github.event.pull_request.head.sha || github.sha }}
+ # A no-ci draft marked ready keeps its SHA, so its earlier skipped hand-off
+ # check is still on the commit. Only a check that started after this event counts.
+ EVENT_AT: ${{ github.event.pull_request.updated_at }}
+ # Must match the job name in .github/workflows/ci-backend.yml.
+ HANDOFF_CHECK: Hand off backend tests to Depot CI
+ GITHUB_ACTIONS_APP_ID: '15368'
run: |
- set -euo pipefail
- # Only pull_request is sampled; any other event that reaches this job always
- # runs (sampling it would skip cache priming or no-op a manual trigger).
- if [ "$EVENT" != "pull_request" ]; then
- echo "sampled=true" >> "$GITHUB_OUTPUT"
- echo "::notice::Optional Backend CI: $EVENT always runs when enabled."
- exit 0
- fi
- # Deterministic per-run dice. Non-integer percent → 0 (off).
- case "$PERCENT" in ''|*[!0-9]*) PERCENT=0 ;; esac
- bucket=$(( RUN_ID % 100 ))
- if [ "$bucket" -lt "$PERCENT" ]; then
- echo "sampled=true" >> "$GITHUB_OUTPUT"
- echo "::notice::Optional Backend CI: PR sampled IN (bucket $bucket < ${PERCENT}%)."
+ set -uo pipefail
+ handed_off=false
+ state=absent
+ if [ "$EVENT" = workflow_dispatch ] || [ "$EVENT" = api ]; then
+ echo "::notice::A person started this run on Depot CI, so it runs here."
+ handed_off=true
+ elif [ "$IS_FORK" = true ]; then
+ # The fork controls its own copy of the GitHub Actions workflow, so its hand-off is not trusted.
+ echo "::notice::Fork pull requests never run on Depot CI."
else
- echo "sampled=false" >> "$GITHUB_OUTPUT"
- echo "::notice::Optional Backend CI: PR sampled OUT (bucket $bucket >= ${PERCENT}%) — skipped."
+ url="https://api.github.com/repos/$REPO/commits/$SHA/check-runs?check_name=$(jq -rn --arg n "$HANDOFF_CHECK" '$n | @uri')&app_id=$GITHUB_ACTIONS_APP_ID"
+ for attempt in $(seq 1 60); do
+ # curl rewrites the saved ETag empty on a 304, so promote it only after a 200.
+ code=$(curl -s -o /tmp/handoff-body -w '%{http_code}' --max-time 15 --etag-compare /tmp/handoff-etag --etag-save /tmp/handoff-etag.new \
+ -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" "$url")
+ case "$code" in
+ 200)
+ mv /tmp/handoff-etag.new /tmp/handoff-etag
+ state=$(jq -r --arg since "$EVENT_AT" '[.check_runs[] | select((.started_at // "") >= $since)] | max_by(.id) | if . == null then "absent" elif .status == "completed" then .conclusion else .status end' /tmp/handoff-body)
+ ;;
+ 304) ;;
+ 401|403) echo "::warning::check-runs API returned $code; cannot read the hand-off"; break ;;
+ *) echo "::warning::check-runs API returned $code" ;;
+ esac
+ case "$state" in
+ success) handed_off=true; break ;;
+ absent|queued|in_progress|pending|waiting|requested) ;;
+ *) break ;;
+ esac
+ [ "$attempt" -lt 60 ] && sleep 10
+ done
+ if [ "$handed_off" = true ]; then
+ echo "::notice::GitHub Actions handed the backend tests to Depot CI."
+ else
+ echo "::notice::GitHub Actions kept the backend tests (hand-off check: $state). Depot CI runs nothing."
+ fi
fi
+ echo "handed_off=$handed_off" >> "$GITHUB_OUTPUT"
# Job to decide if we should run backend ci
# See .github/actions/paths-filter/README.md for filter semantics
changes:
@@ -594,31 +619,14 @@ jobs:
outputs:
mode: ${{ steps.detect.outputs.mode }}
steps:
+ # Always check, never update. The steps that upload the snapshot patch and
+ # commit it back are not ported here, so an update-mode run would rewrite the
+ # snapshots on the runner, throw them away, and pass. A routed PR with stale
+ # snapshots goes red here instead of merging stale. Restore canonical's mode
+ # detection once handle-snapshots is ported.
- name: Detect mode
id: detect
- env:
- PR_REPO: ${{ github.event.pull_request.head.repo.full_name }}
- REPO: ${{ github.repository }}
- HAS_NO_SNAPSHOT_LABEL: ${{ contains(github.event.pull_request.labels.*.name, 'no-snapshot-update') }}
- AUTHOR: ${{ github.actor }}
- run: |
- if [ "$PR_REPO" != "$REPO" ] && [ -n "$PR_REPO" ]; then
- echo "mode=check" >> "$GITHUB_OUTPUT"
- echo "Fork detected - running in check mode"
- elif [ "$HAS_NO_SNAPSHOT_LABEL" == "true" ]; then
- echo "mode=check" >> "$GITHUB_OUTPUT"
- echo "No-snapshot label detected - running in check mode"
- else
- echo "Workflow triggered by: $AUTHOR"
- if [[ "$AUTHOR" != "dependabot[bot]" ]] && \
- { [[ "$AUTHOR" == *"github-actions"* ]] || [[ "$AUTHOR" == *"[bot]"* ]] || [[ "$AUTHOR" == "posthog-bot" ]]; }; then
- echo "mode=check" >> "$GITHUB_OUTPUT"
- echo "Bot actor detected - running in check mode"
- else
- echo "mode=update" >> "$GITHUB_OUTPUT"
- echo "Running in update mode"
- fi
- fi
+ run: echo "mode=check" >> "$GITHUB_OUTPUT"
# Fast pre-job: determines which products need testing and if Django should run
# Only needs pnpm + node — no Python, Docker, or services
@@ -2862,6 +2870,7 @@ jobs:
check_required_result "Django test matrix" "${{ needs.django.result }}"
check_required_result "Migration checks" "${{ needs.check-migrations.result }}"
check_required_result "OpenAPI type checks" "${{ needs.check-openapi-types.result }}"
+ check_required_result "Hand-off wait" "${{ needs.sample.result }}"
check_required_result "Repo checks" "${{ needs.repo-checks.result }}"
check_required_result "Turbo discover" "${{ needs.turbo-discover.result }}"
check_required_result "Product test matrix build" "${{ needs.build-product-test-matrix.result }}"
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index be76f50d58a9..16a85e1d8bbc 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -162,6 +162,9 @@ jobs:
repo_invariants: ${{ steps.filter.outputs.repo_invariants || 'false' }}
product_yamls_files: ${{ steps.filter.outputs.product_yamls_files }}
timing_scripts: ${{ steps.filter.outputs.timing_scripts }}
+ # Which engine runs this event's backend tests. Depot CI never decides on its
+ # own: it waits for the hand-off job below and runs only when that succeeded.
+ engine: github
steps:
# For pull requests it's not necessary to checkout the code, but we
# also want this to run on master so we need to checkout
@@ -439,6 +442,20 @@ jobs:
- 'services/mcp/src/templates/sections/**/*'
- 'services/mcp/schema/exec-command-reference.md'
+ # The only signal Depot CI acts on. Its wait job in .depot/workflows/ci-backend.yml
+ # polls this check by name and runs the backend tests only when it concluded
+ # `success`, so a skipped hand-off keeps the event on GitHub Actions. Renaming
+ # this job breaks that lookup.
+ hand-off-to-depot:
+ name: Hand off backend tests to Depot CI
+ runs-on: ubuntu-latest
+ timeout-minutes: 2
+ needs: [changes]
+ if: needs.changes.outputs.engine == 'depot'
+ permissions: {}
+ steps:
+ - run: echo "::notice::Depot CI runs the backend tests for this event. GitHub Actions runs none."
+
detect-snapshot-mode:
name: Detect snapshot mode
runs-on: depot-ubuntu-24.04
diff --git a/tools/workflow-plan/tests/workflows.test.ts b/tools/workflow-plan/tests/workflows.test.ts
index d3e94edbc4a1..045fbd2c6d6a 100644
--- a/tools/workflow-plan/tests/workflows.test.ts
+++ b/tools/workflow-plan/tests/workflows.test.ts
@@ -133,7 +133,7 @@ const EXPECTATIONS: Expectation[] = [
'test-selection-verdict',
'capture-test-selection',
],
- skipped: ['handle-snapshots', 'cancel-backend-on-openapi-check-failure'],
+ skipped: ['handle-snapshots', 'cancel-backend-on-openapi-check-failure', 'hand-off-to-depot'],
}
),
backend(