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.

2.2 KB
Raw Blame
#!/bin/bash

# Builds the headless player, the nodejs dist and the rasterizer image (cached by dependency hash).
# Shared by bin/temporal-recording-rasterizer-worker and bin/rasterize-recording-file.

set -e
set -o pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"

IMAGE_NAME="recording-rasterizer:local"
DOCKERFILE="$ROOT_DIR/Dockerfile.recording-rasterizer"
HASH_FILE="$ROOT_DIR/.posthog/.generated/rasterizer-image.hash"

# Build for the host: chrome-headless-shell ships an arm64 build, and an emulated amd64 Chromium is
# far too slow to render with. Apple Silicon and the arm64 devboxes both land here.
case "$(uname -m)" in
arm64 | aarch64) PLATFORM="linux/arm64" ;;
*) PLATFORM="linux/amd64" ;;
esac

cd "$ROOT_DIR"

# --- Step 1: Build TypeScript locally ---

echo "Building headless player..."
pnpm --filter=@posthog/replay-headless build

echo "Building recording-rasterizer worker..."
pnpm --filter=@posthog/nodejs build

# --- Step 2: Build Docker image if needed ---

# Hash the Dockerfile and key dependency files to detect when a rebuild is needed.
# Source code changes are picked up via volume mounts, so we only need to rebuild
# when the image structure (deps, base image, system packages) changes.
# The platform is part of the hash so a cached image from another architecture is rebuilt.
current_hash=$( { cat \
    "$DOCKERFILE" \
    "$ROOT_DIR/pnpm-lock.yaml" \
    "$ROOT_DIR/nodejs/package.json" \
    "$ROOT_DIR/common/replay-headless/package.json" \
    2>/dev/null; echo "$PLATFORM"; } | shasum -a 256 | cut -d' ' -f1)

previous_hash=""
if [ -f "$HASH_FILE" ]; then
    previous_hash=$(cat "$HASH_FILE")
fi

# Also rebuild if the image doesn't exist
if ! docker image inspect "$IMAGE_NAME" >/dev/null 2>&1; then
    previous_hash="missing"
fi

if [ "$current_hash" != "$previous_hash" ]; then
    echo "Docker image outdated or missing, rebuilding..."
    docker build \
        --platform "$PLATFORM" \
        -f "$DOCKERFILE" \
        -t "$IMAGE_NAME" \
        "$ROOT_DIR"

    mkdir -p "$(dirname "$HASH_FILE")"
    echo "$current_hash" > "$HASH_FILE"
    echo "Docker image built and cached."
else
    echo "Docker image up-to-date (cached)."
fi