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.

feat(error-tracking): pass the release id into the django image (#104420)

Aleksander Błaszkiewicza5f865b695be
diff --git a/.depot/actions/resolve-error-tracking-release/action.yml b/.depot/actions/resolve-error-tracking-release/action.yml
new file mode 100644
index 000000000000..2bd826baeb01
--- /dev/null
+++ b/.depot/actions/resolve-error-tracking-release/action.yml
@@ -0,0 +1,78 @@
+name: Resolve error tracking release
+description: Create the error tracking release for this build, or find it when it exists, and output its id for the image build to bake in as POSTHOG_RELEASE_ID.
+
+inputs:
+ api-key:
+ required: true
+ description: 'Personal API key with the error_tracking:write scope. When empty, nothing runs and the output stays empty.'
+ project-id:
+ required: false
+ default: '2'
+ description: 'Id of the project that owns the release.'
+ release-name:
+ required: false
+ default: 'posthog'
+ description: 'Name of the release. Must match the --release-name the sourcemap stage of the Dockerfile passes, so both find one release.'
+ release-version:
+ required: true
+ description: 'Version of the release, normally the commit sha. Must match the --release-version the sourcemap stage of the Dockerfile passes.'
+
+outputs:
+ release-id:
+ description: 'Id of the release, or empty when none was resolved.'
+ value: ${{ steps.resolve.outputs.release-id }}
+
+runs:
+ using: 'composite'
+ steps:
+ # Never fails on its own: without a key, or on a download, checksum, or API error, the id
+ # stays empty and a warning says so. A caller that must not block on this step adds
+ # continue-on-error and a timeout on its `uses:` step, because a composite step cannot
+ # carry a timeout itself.
+ - name: Resolve release
+ id: resolve
+ shell: bash
+ env:
+ # The CLI installer is pinned to an immutable release tag and checksum-verified before
+ # execution. The Dockerfile pins its own copy for the sourcemap stage, and the two do
+ # not have to match: both sides find the release by its name and version. To upgrade,
+ # change the version and recompute the hash:
+ # curl -LsSf "https://github.com/PostHog/posthog/releases/download/posthog-cli%2Fv<X.Y.Z>/posthog-cli-installer.sh" | sha256sum
+ POSTHOG_CLI_VERSION: 0.18.2
+ POSTHOG_CLI_INSTALLER_SHA256: 1ed5ff785ca33f38458efb1677ffd35ed99d935ac59d5e28bfd0d07a4f697f7a
+ POSTHOG_CLI_TOKEN: ${{ inputs.api-key }}
+ POSTHOG_CLI_ENV_ID: ${{ inputs.project-id }}
+ POSTHOG_CLI_INSTALL_DIR: ${{ runner.temp }}/posthog-cli
+ POSTHOG_CLI_NO_MODIFY_PATH: '1'
+ # The CLI stamps the release with git metadata (branch, commit, repo) read from these.
+ # GitHub Actions exports them already. Depot CI, which runs the outage image build,
+ # may not, so they come from the context here.
+ GITHUB_ACTIONS: 'true'
+ GITHUB_SHA: ${{ github.sha }}
+ GITHUB_REF_NAME: ${{ github.ref_name }}
+ GITHUB_REPOSITORY: ${{ github.repository }}
+ GITHUB_SERVER_URL: ${{ github.server_url }}
+ RELEASE_NAME: ${{ inputs.release-name }}
+ RELEASE_VERSION: ${{ inputs.release-version }}
+ run: |
+ release_id=""
+ if [ -n "$POSTHOG_CLI_TOKEN" ]; then
+ # Explicit && chaining, not set -e: bash ignores errexit inside a command
+ # substitution that || guards, so a failed download or checksum would
+ # otherwise fall through to running the installer.
+ release_id="$(
+ installer="$RUNNER_TEMP/posthog-cli-installer.sh" &&
+ curl --proto '=https' --tlsv1.2 -fsSL --retry 5 --retry-all-errors --retry-max-time 60 --connect-timeout 10 \
+ -o "$installer" \
+ "https://github.com/PostHog/posthog/releases/download/posthog-cli%2Fv${POSTHOG_CLI_VERSION}/posthog-cli-installer.sh" &&
+ echo "${POSTHOG_CLI_INSTALLER_SHA256} ${installer}" | sha256sum -c - >&2 &&
+ sh "$installer" >&2 &&
+ "$POSTHOG_CLI_INSTALL_DIR/posthog-cli" release resolve \
+ --release-name "$RELEASE_NAME" \
+ --release-version "$RELEASE_VERSION"
+ )" || release_id=""
+ fi
+ if [ -z "$release_id" ]; then
+ echo "::warning::No error tracking release resolved; the image ships without POSTHOG_RELEASE_ID"
+ fi
+ echo "release-id=$release_id" >> "$GITHUB_OUTPUT"
diff --git a/.depot/workflows/actions-outage-image-build.yml b/.depot/workflows/actions-outage-image-build.yml
index 51c1ecae7fa1..c3666a97a024 100644
--- a/.depot/workflows/actions-outage-image-build.yml
+++ b/.depot/workflows/actions-outage-image-build.yml
@@ -53,6 +53,20 @@ jobs:
username: ${{ vars.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
+ # Same step as in container-images-cd.yml, so an image built during an outage links
+ # backend exceptions to its release like a normal build does. Never blocks the build:
+ # the action turns the expected failures into an empty id with a warning, and
+ # continue-on-error plus the timeout cover the rest. An empty id is fine: the SDK
+ # treats a blank value as unset.
+ - name: Resolve error tracking release
+ id: release
+ continue-on-error: true
+ timeout-minutes: 5
+ uses: ./.depot/actions/resolve-error-tracking-release
+ with:
+ api-key: ${{ secrets.POSTHOG_UPLOAD_SOURCEMAPS_CLI_API_KEY }}
+ release-version: ${{ github.sha }}
+
- name: Build and push
id: build
uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
@@ -61,10 +75,12 @@ jobs:
tags: ${{ steps.aws-ecr.outputs.registry }}/posthog-cloud-prs:${{ github.sha }}
platforms: linux/amd64,linux/arm64
# The GITHUB_* args become the git metadata on the error tracking release the
- # sourcemap upload stage creates. That stage builds in a container with no .git
- # directory, so the workflow has to hand them in (see Dockerfile).
+ # sourcemap upload stage resolves, and creates when the release step above could
+ # not. That stage builds in a container with no .git directory, so the workflow
+ # has to hand them in (see Dockerfile).
build-args: |
COMMIT_HASH=${{ github.sha }}
+ POSTHOG_RELEASE_ID=${{ steps.release.outputs.release-id }}
GITHUB_ACTIONS=true
GITHUB_SHA=${{ github.sha }}
GITHUB_REF_NAME=${{ github.ref_name }}
diff --git a/.github/actions/resolve-error-tracking-release/action.yml b/.github/actions/resolve-error-tracking-release/action.yml
new file mode 100644
index 000000000000..2bd826baeb01
--- /dev/null
+++ b/.github/actions/resolve-error-tracking-release/action.yml
@@ -0,0 +1,78 @@
+name: Resolve error tracking release
+description: Create the error tracking release for this build, or find it when it exists, and output its id for the image build to bake in as POSTHOG_RELEASE_ID.
+
+inputs:
+ api-key:
+ required: true
+ description: 'Personal API key with the error_tracking:write scope. When empty, nothing runs and the output stays empty.'
+ project-id:
+ required: false
+ default: '2'
+ description: 'Id of the project that owns the release.'
+ release-name:
+ required: false
+ default: 'posthog'
+ description: 'Name of the release. Must match the --release-name the sourcemap stage of the Dockerfile passes, so both find one release.'
+ release-version:
+ required: true
+ description: 'Version of the release, normally the commit sha. Must match the --release-version the sourcemap stage of the Dockerfile passes.'
+
+outputs:
+ release-id:
+ description: 'Id of the release, or empty when none was resolved.'
+ value: ${{ steps.resolve.outputs.release-id }}
+
+runs:
+ using: 'composite'
+ steps:
+ # Never fails on its own: without a key, or on a download, checksum, or API error, the id
+ # stays empty and a warning says so. A caller that must not block on this step adds
+ # continue-on-error and a timeout on its `uses:` step, because a composite step cannot
+ # carry a timeout itself.
+ - name: Resolve release
+ id: resolve
+ shell: bash
+ env:
+ # The CLI installer is pinned to an immutable release tag and checksum-verified before
+ # execution. The Dockerfile pins its own copy for the sourcemap stage, and the two do
+ # not have to match: both sides find the release by its name and version. To upgrade,
+ # change the version and recompute the hash:
+ # curl -LsSf "https://github.com/PostHog/posthog/releases/download/posthog-cli%2Fv<X.Y.Z>/posthog-cli-installer.sh" | sha256sum
+ POSTHOG_CLI_VERSION: 0.18.2
+ POSTHOG_CLI_INSTALLER_SHA256: 1ed5ff785ca33f38458efb1677ffd35ed99d935ac59d5e28bfd0d07a4f697f7a
+ POSTHOG_CLI_TOKEN: ${{ inputs.api-key }}
+ POSTHOG_CLI_ENV_ID: ${{ inputs.project-id }}
+ POSTHOG_CLI_INSTALL_DIR: ${{ runner.temp }}/posthog-cli
+ POSTHOG_CLI_NO_MODIFY_PATH: '1'
+ # The CLI stamps the release with git metadata (branch, commit, repo) read from these.
+ # GitHub Actions exports them already. Depot CI, which runs the outage image build,
+ # may not, so they come from the context here.
+ GITHUB_ACTIONS: 'true'
+ GITHUB_SHA: ${{ github.sha }}
+ GITHUB_REF_NAME: ${{ github.ref_name }}
+ GITHUB_REPOSITORY: ${{ github.repository }}
+ GITHUB_SERVER_URL: ${{ github.server_url }}
+ RELEASE_NAME: ${{ inputs.release-name }}
+ RELEASE_VERSION: ${{ inputs.release-version }}
+ run: |
+ release_id=""
+ if [ -n "$POSTHOG_CLI_TOKEN" ]; then
+ # Explicit && chaining, not set -e: bash ignores errexit inside a command
+ # substitution that || guards, so a failed download or checksum would
+ # otherwise fall through to running the installer.
+ release_id="$(
+ installer="$RUNNER_TEMP/posthog-cli-installer.sh" &&
+ curl --proto '=https' --tlsv1.2 -fsSL --retry 5 --retry-all-errors --retry-max-time 60 --connect-timeout 10 \
+ -o "$installer" \
+ "https://github.com/PostHog/posthog/releases/download/posthog-cli%2Fv${POSTHOG_CLI_VERSION}/posthog-cli-installer.sh" &&
+ echo "${POSTHOG_CLI_INSTALLER_SHA256} ${installer}" | sha256sum -c - >&2 &&
+ sh "$installer" >&2 &&
+ "$POSTHOG_CLI_INSTALL_DIR/posthog-cli" release resolve \
+ --release-name "$RELEASE_NAME" \
+ --release-version "$RELEASE_VERSION"
+ )" || release_id=""
+ fi
+ if [ -z "$release_id" ]; then
+ echo "::warning::No error tracking release resolved; the image ships without POSTHOG_RELEASE_ID"
+ fi
+ echo "release-id=$release_id" >> "$GITHUB_OUTPUT"
diff --git a/.github/workflows/container-images-cd.yml b/.github/workflows/container-images-cd.yml
index 7b70658171f8..f55de1e374ce 100644
--- a/.github/workflows/container-images-cd.yml
+++ b/.github/workflows/container-images-cd.yml
@@ -175,6 +175,25 @@ jobs:
echo "Image for $SHA already present in ECR ($digest); skipping build." >&2
fi
+ # Create the error tracking release for this commit before the build, so the build can
+ # bake its id into the image as POSTHOG_RELEASE_ID (see the final stage of the Dockerfile).
+ # The Python SDK sends that id as $release_id on every event, which links a backend
+ # exception to the same release the frontend bundles carry: the sourcemap stage of the
+ # Dockerfile resolves the release by the same name and version, so it finds this one.
+ # This step must never block a deploy. The action turns the expected failures (no secret,
+ # a download, checksum, or API error) into an empty id with a warning. continue-on-error
+ # and the step timeout cover what the action does not catch, such as a hung API call.
+ # An empty id is fine: the SDK treats a blank value as unset.
+ - name: Resolve error tracking release
+ id: release
+ if: steps.preflight.outputs.digest == ''
+ continue-on-error: true
+ timeout-minutes: 5
+ uses: ./.github/actions/resolve-error-tracking-release
+ with:
+ api-key: ${{ secrets.POSTHOG_UPLOAD_SOURCEMAPS_CLI_API_KEY }}
+ release-version: ${{ github.sha }}
+
- name: Build and push container image
id: build
if: steps.preflight.outputs.digest == ''
@@ -192,10 +211,12 @@ jobs:
annotations: ${{ steps.docker-meta.outputs.annotations }}
platforms: linux/arm64,linux/amd64
# The GITHUB_* args become the git metadata on the error tracking release the
- # sourcemap upload stage creates. That stage builds in a container with no .git
- # directory, so the workflow has to hand them in (see Dockerfile).
+ # sourcemap upload stage resolves, and creates when the release step above could
+ # not. That stage builds in a container with no .git directory, so the workflow
+ # has to hand them in (see Dockerfile).
build-args: |
COMMIT_HASH=${{ github.sha }}
+ POSTHOG_RELEASE_ID=${{ steps.release.outputs.release-id }}
GITHUB_ACTIONS=true
GITHUB_SHA=${{ github.sha }}
GITHUB_REF_NAME=${{ github.ref_name }}
@@ -270,10 +291,12 @@ jobs:
annotations: ${{ steps.docker-meta.outputs.annotations }}
platforms: linux/arm64,linux/amd64
# The GITHUB_* args become the git metadata on the error tracking release the
- # sourcemap upload stage creates. That stage builds in a container with no .git
- # directory, so the workflow has to hand them in (see Dockerfile).
+ # sourcemap upload stage resolves, and creates when the release step above could
+ # not. That stage builds in a container with no .git directory, so the workflow
+ # has to hand them in (see Dockerfile).
build-args: |
COMMIT_HASH=${{ github.sha }}
+ POSTHOG_RELEASE_ID=${{ steps.release.outputs.release-id }}
GITHUB_ACTIONS=true
GITHUB_SHA=${{ github.sha }}
GITHUB_REF_NAME=${{ github.ref_name }}
diff --git a/Dockerfile b/Dockerfile
index f273a346cbd7..932526eea7fc 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -94,7 +94,8 @@ ARG POSTHOG_CLI_INSTALLER_SHA256=1ed5ff785ca33f38458efb1677ffd35ed99d935ac59d5e2
# directory to fall back on: without these the release is created with no link back to the code it
# was built from, and the CLI skips the metadata silently because --release-name/--release-version
# already let it create the release. The CLI treats empty values as absent, so local builds that
-# pass none of these behave as before.
+# pass none of these behave as before. In the CD workflow the release usually exists already, created
+# with the same metadata before the build, and this stage only looks it up by name and version.
ARG GITHUB_ACTIONS
ARG GITHUB_SHA
ARG GITHUB_REF_NAME
@@ -360,6 +361,14 @@ USER posthog
ARG COMMIT_HASH
RUN echo $COMMIT_HASH > /code/commit.txt
+# The error tracking release this build belongs to. The CD workflow creates the release before the
+# build and passes its id in (see "Resolve error tracking release" in
+# .github/workflows/container-images-cd.yml). The Python SDK reads POSTHOG_RELEASE_ID and sends it as
+# $release_id on every event, so a backend exception resolves to the same release as the frontend
+# bundles. Empty in every other build, which the SDK treats as unset.
+ARG POSTHOG_RELEASE_ID
+ENV POSTHOG_RELEASE_ID=$POSTHOG_RELEASE_ID
+
# Copy the Python dependencies and Django staticfiles from the posthog-build stage.
COPY --from=posthog-build --chown=posthog:posthog /code/staticfiles /code/staticfiles
COPY --from=posthog-build --chown=posthog:posthog /python-runtime /python-runtime