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(ci): build the dagster ml image with the inbox-ranking group (#116613)

posthog[bot]5f74ffa6e1fd
diff --git a/.github/workflows/container-images-cd.yml b/.github/workflows/container-images-cd.yml
index a266c679bd7d..a87c03e6d877 100644
--- a/.github/workflows/container-images-cd.yml
+++ b/.github/workflows/container-images-cd.yml
@@ -1452,6 +1452,102 @@ jobs:
"timestamp": "${{ github.event.head_commit.timestamp }}"
}
+ # Main image plus the inbox-ranking dependency group, for the inbox ranking training code
+ # location. Nothing depends on this job, and continue-on-error keeps the run green, so a
+ # failure here never blocks the main image publish or its deploy dispatches.
+ # The `ml-image-digest` output is the digest of posthog-dagster-ml:<sha>. A code location
+ # dispatch that runs this image reads it as needs.dagster_ml_build.outputs.ml-image-digest.
+ dagster_ml_build:
+ name: Build and push Dagster ML image
+ needs: posthog_build
+ if: ${{ needs.posthog_build.result == 'success' && needs.posthog_build.outputs.image-digest != '' && github.repository_owner == 'PostHog' && vars.CD_DEPLOY_ENABLED == 'true' && github.ref == 'refs/heads/master' }}
+ runs-on: depot-ubuntu-24.04
+ timeout-minutes: 30
+ continue-on-error: true
+ permissions:
+ id-token: write
+ contents: read
+ outputs:
+ ml-image-digest: ${{ steps.image.outputs.digest }}
+ env:
+ ML_ECR_REPOSITORY: posthog-dagster-ml
+
+ steps:
+ - name: Assert default-branch publish role is configured
+ env:
+ DEFAULT_BRANCH_ROLE: ${{ vars.AWS_ECR_POSTHOG_MASTER_PUBLISH_IAM_ROLE }}
+ run: |
+ if [[ -z "$DEFAULT_BRANCH_ROLE" ]]; then
+ echo "::error::AWS_ECR_POSTHOG_MASTER_PUBLISH_IAM_ROLE is not set."
+ exit 1
+ fi
+
+ - name: Check out
+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+
+ - name: Set up Depot CLI
+ uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1
+
+ - name: Configure AWS credentials
+ uses: aws-actions/configure-aws-credentials@ec61189d14ec14c8efccab744f656cffd0e33f37 # v6.1.0
+ with:
+ role-to-assume: ${{ vars.AWS_ECR_POSTHOG_MASTER_PUBLISH_IAM_ROLE }}
+ aws-region: us-east-1
+
+ - name: Login to Amazon ECR
+ id: aws-ecr
+ uses: aws-actions/amazon-ecr-login@f2e9fc6c2b355c1890b65e6f6f0e2ac3e6e22f78 # v2.1.2
+
+ # Same idempotency rule as posthog_build: a re-run must not push a new digest over
+ # an existing full-SHA tag. A genuine miss returns `None`, and any API error fails the step.
+ - name: Check for existing image in ECR
+ id: preflight
+ env:
+ SHA: ${{ github.sha }}
+ run: |
+ set -euo pipefail
+ digest="$(aws ecr batch-get-image \
+ --repository-name "$ML_ECR_REPOSITORY" \
+ --image-ids imageTag="$SHA" \
+ --accepted-media-types \
+ "application/vnd.oci.image.index.v1+json" \
+ "application/vnd.docker.distribution.manifest.list.v2+json" \
+ "application/vnd.oci.image.manifest.v1+json" \
+ "application/vnd.docker.distribution.manifest.v2+json" \
+ --query 'images[0].imageId.imageDigest' \
+ --output text)"
+ [ "$digest" = "None" ] && digest=""
+ echo "digest=$digest" >> "$GITHUB_OUTPUT"
+ if [ -n "$digest" ]; then
+ echo "ML image for $SHA already present in ECR ($digest); skipping build." >&2
+ fi
+
+ - name: Build and push Dagster ML image
+ id: build
+ if: steps.preflight.outputs.digest == ''
+ uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
+ with:
+ context: .
+ file: ./Dockerfile.dagster-ml
+ buildx-fallback: false
+ push: true
+ tags: ${{ steps.aws-ecr.outputs.registry }}/${{ env.ML_ECR_REPOSITORY }}:master,${{ steps.aws-ecr.outputs.registry }}/${{ env.ML_ECR_REPOSITORY }}:${{ github.sha }}
+ platforms: linux/arm64,linux/amd64
+ build-args: |
+ BASE_IMAGE=${{ steps.aws-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}@${{ needs.posthog_build.outputs.image-digest }}
+
+ - name: Resolve ML image digest
+ id: image
+ env:
+ DIGEST: ${{ steps.preflight.outputs.digest || steps.build.outputs.digest }}
+ run: |
+ echo "digest=$DIGEST" >> "$GITHUB_OUTPUT"
+ echo "ML image digest: $DIGEST" >> "$GITHUB_STEP_SUMMARY"
+
+ - name: Warn when the ML image failed
+ if: failure()
+ run: echo "::warning title=Dagster ML image not published::The Dagster ML image build failed. The main image publish and deploys are not affected."
+
ordered_image_alias:
name: Publish ordered image alias
needs: posthog_build
diff --git a/.github/workflows/container-images-ci.yml b/.github/workflows/container-images-ci.yml
index 8d5cd7a7f462..ad4b3b65d556 100644
--- a/.github/workflows/container-images-ci.yml
+++ b/.github/workflows/container-images-ci.yml
@@ -27,7 +27,9 @@ jobs:
steps.filter.outputs.hobby == 'true'
|| (startsWith(github.head_ref, 'trunk-merge/') && steps.filter.outputs.build_inputs == 'true')
|| contains(github.event.pull_request.labels.*.name, 'hobby-preview')
+ || steps.filter.outputs.dagster_ml == 'true'
}}
+ dagster_ml_changed: ${{ steps.filter.outputs.dagster_ml }}
dockerignore_changed: ${{ steps.filter.outputs.dockerignore }}
# Drives the Dockerfile lint job: a boolean plus the shell-escaped, space-separated
# list of changed Dockerfiles to hand straight to hadolint.
@@ -113,6 +115,12 @@ jobs:
- '!frontend/src/**/*.test.*'
- '!frontend/src/**/*.spec.*'
- '!frontend/src/**/*.stories.*'
+ # The Dagster ML image is the main image plus the inbox-ranking group, so
+ # validating it needs the main image build of this PR too.
+ dagster_ml:
+ - Dockerfile
+ - Dockerfile.dagster-ml
+ - uv.lock
# 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:
@@ -192,6 +200,46 @@ jobs:
echo "- Commit: \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
echo "- Digest: \`$IMAGE_DIGEST\`" >> $GITHUB_STEP_SUMMARY
+ # Validates Dockerfile.dagster-ml on the image this PR built. It builds but never pushes, and
+ # skips in the merge queue, where posthog_build pushes no image to build from.
+ dagster_ml_build:
+ needs: [changes, posthog_build]
+ name: Build Dagster ML image
+ runs-on: depot-ubuntu-24.04
+ timeout-minutes: 30
+ permissions:
+ id-token: write
+ contents: read
+ if: needs.changes.outputs.dagster_ml_changed == 'true' && needs.posthog_build.outputs.digest != '' && !startsWith(github.head_ref, 'trunk-merge/')
+ steps:
+ - name: Check out
+ uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+
+ - name: Set up Depot CLI
+ uses: depot/setup-action@15c09a5f77a0840ad4bce955686522a257853461 # v1.7.1
+
+ # Pull-only use of the PR role: the base image lives in posthog-cloud-prs.
+ - name: Configure AWS credentials
+ uses: aws-actions/configure-aws-credentials@ec61189d14ec14c8efccab744f656cffd0e33f37 # v6.1.0
+ with:
+ role-to-assume: ${{ vars.AWS_ECR_POSTHOG_PRS_PUBLISH_IAM_ROLE }}
+ aws-region: us-east-1
+
+ - name: Login to Amazon ECR
+ id: aws-ecr
+ uses: aws-actions/amazon-ecr-login@f2e9fc6c2b355c1890b65e6f6f0e2ac3e6e22f78 # v2.1.2
+
+ - name: Build Dagster ML image
+ uses: depot/build-push-action@5f3b3c2e5a00f0093de47f657aeaefcedff27d18 # v1.17.0
+ with:
+ context: .
+ file: ./Dockerfile.dagster-ml
+ buildx-fallback: false
+ push: false
+ platforms: linux/amd64,linux/arm64
+ build-args: |
+ BASE_IMAGE=${{ steps.aws-ecr.outputs.registry }}/posthog-cloud-prs@${{ needs.posthog_build.outputs.digest }}
+
lint:
needs: changes
name: Lint changed Dockerfiles
diff --git a/Dockerfile.dagster-ml b/Dockerfile.dagster-ml
new file mode 100644
index 000000000000..6a78a7cd3bbf
--- /dev/null
+++ b/Dockerfile.dagster-ml
@@ -0,0 +1,66 @@
+#
+# Dagster ML image: the main PostHog image plus the `inbox-ranking` dependency group (CPU-only
+# torch, onnx, onnxruntime). The inbox ranking training code location runs it, so it must carry
+# the same application code as the main locations. CI builds it FROM the main image digest of the
+# same commit, see the dagster_ml_build jobs in .github/workflows/container-images-cd.yml and
+# .github/workflows/container-images-ci.yml.
+#
+# Build with: docker build -f Dockerfile.dagster-ml --build-arg BASE_IMAGE=<registry>/posthog-cloud@<digest> .
+#
+# No default: a floating base tag would let the image drift from the main image of its commit.
+ARG BASE_IMAGE
+
+# Keep the uv version equal to the one in Dockerfile.
+FROM ghcr.io/astral-sh/uv:0.12.13 AS uv
+
+FROM ${BASE_IMAGE}
+SHELL ["/bin/bash", "-e", "-o", "pipefail", "-c"]
+
+# Install as the user that owns /python-runtime, so the new files keep the same owner.
+USER posthog
+ENV UV_COMPILE_BYTECODE=1 \
+ UV_LINK_MODE=copy \
+ UV_PROJECT_ENVIRONMENT=/python-runtime \
+ UV_CACHE_DIR=/tmp/uv-cache
+
+# --inexact keeps every package the main image already has. The other flags and the /code project
+# path match the uv sync in Dockerfile, so uv sees the installed packages as up to date and only
+# adds the group.
+WORKDIR /code
+RUN --mount=from=uv,source=/uv,target=/bin/uv \
+ --mount=type=cache,id=uv-dagster-ml,target=/tmp/uv-cache,uid=10001,gid=10001 \
+ --mount=type=bind,source=uv.lock,target=uv.lock \
+ --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
+ --mount=type=bind,source=tools/hogli,target=tools/hogli \
+ --mount=type=bind,source=packages/owners-yaml,target=packages/owners-yaml \
+ --mount=type=bind,source=packages/personhog-proto,target=packages/personhog-proto \
+ uv sync --locked --inexact --no-dev --no-editable --no-install-project \
+ --no-binary-package lxml --no-binary-package xmlsec --group inbox-ranking
+
+# Fail the build if torch is not the CPU-only build, or if any CUDA library got in.
+RUN python - <<'PY'
+import importlib.metadata
+import pathlib
+
+import onnx # noqa: F401
+import onnxruntime
+import torch
+
+assert torch.version.cuda is None, f"torch is a CUDA build: {torch.version.cuda}"
+assert not torch.cuda.is_available()
+cuda_dists = sorted(
+ name for d in importlib.metadata.distributions() if (name := d.metadata["Name"]).lower().startswith("nvidia-")
+)
+assert not cuda_dists, f"CUDA packages installed: {cuda_dists}"
+cuda_libs = sorted(
+ str(p)
+ for p in pathlib.Path("/python-runtime").rglob("lib*.so*")
+ if p.name.startswith(("libcuda", "libcudart", "libcudnn", "libcublas", "libnccl"))
+)
+assert not cuda_libs, f"CUDA libraries present: {cuda_libs}"
+print("torch", torch.__version__, "onnxruntime", onnxruntime.__version__)
+PY
+
+# Root is needed only so bin/docker-server can drop the app to nobody with setpriv, as in Dockerfile.
+# nosemgrep: dockerfile.security.last-user-is-root.last-user-is-root
+USER root
diff --git a/products/signals/dags/inbox_ranking/README.md b/products/signals/dags/inbox_ranking/README.md
index cc6a78e22e19..9ccfade08d9c 100644
--- a/products/signals/dags/inbox_ranking/README.md
+++ b/products/signals/dags/inbox_ranking/README.md
@@ -339,7 +339,7 @@ Nothing here touches the prod bucket: the reader credential is read-only and the
`multihead/` is an offline experiment and not a dag. It tests whether one shared network over the report vector, with or without the team's project vectors, beats the per-head XGBoost champion recipe of the `report_embeddings` family. It reads the disk cache the sync script fills, so sync with `INBOX_RANKING_SYNC_EMBEDDINGS=1` first. The dev stack does not need to be up.
-The network is PyTorch. torch is not a main dependency: it comes from the `inbox-ranking` dependency group, with the CPU-only wheels, together with `onnx` and `onnxruntime`. The group conflicts with the `sentiment` group, which takes the CUDA build of torch, so install one or the other. No image installs the group.
+The network is PyTorch. torch is not a main dependency: it comes from the `inbox-ranking` dependency group, with the CPU-only wheels, together with `onnx` and `onnxruntime`. The group conflicts with the `sentiment` group, which takes the CUDA build of torch, so install one or the other. Only `Dockerfile.dagster-ml`, the image of the `signals_ml` code location, installs the group.
```bash
uv sync --group inbox-ranking
diff --git a/pyproject.toml b/pyproject.toml
index 06bfdc6324ac..b28865a20dfb 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -302,8 +302,8 @@ sentiment = [
"optimum[onnxruntime]~=1.25.0",
"torch~=2.9.0",
]
-# The inbox ranking multi-head network (products/signals/dags/inbox_ranking/multihead/). Dev only:
-# no image installs it. torch comes from the CPU wheel index, see [tool.uv.sources].
+# The inbox ranking multi-head network (products/signals/dags/inbox_ranking/multihead/). Only
+# Dockerfile.dagster-ml installs it. torch comes from the CPU wheel index, see [tool.uv.sources].
inbox-ranking = [
"onnx~=1.21.0",
"onnxruntime~=1.30.0",