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): cut the pnpm store cache to what the lockfile needs (#93326)

Raúl Negrón-Oteroeccf7c5469a1
diff --git a/.agents/skills/authoring-ci-workflows/SKILL.md b/.agents/skills/authoring-ci-workflows/SKILL.md
index ca4fb7a86992..976017da5994 100644
--- a/.agents/skills/authoring-ci-workflows/SKILL.md
+++ b/.agents/skills/authoring-ci-workflows/SKILL.md
@@ -298,7 +298,7 @@ The default is 6 hours — a hung job burns paid minutes silently.
## Caching
-Route through the shared composites rather than hand-rolling `actions/cache`: `./.github/actions/pnpm-install` (single `pnpm-<os>-<lockhash>` key, save gated to master), `astral-sh/setup-uv` with `enable-cache: true`, Depot cache via `./.github/actions/build-n-cache-image`.
+Route through the shared composites rather than hand-rolling `actions/cache`: `./.github/actions/pnpm-install` (single `pnpm-<os>-<lockhash>` key, restore only; `pnpm-store-cache.yml` writes it on master), `astral-sh/setup-uv` with `enable-cache: true`, Depot cache via `./.github/actions/build-n-cache-image`.
One canonical key per artifact; gate saves to master or key deliberately per-ref.
PR-scoped cache writes nobody else can read just fragment the 10 GB LRU cap.
diff --git a/.github/actions/pnpm-install/action.yml b/.github/actions/pnpm-install/action.yml
index bbf06e15587d..de3be522b519 100644
--- a/.github/actions/pnpm-install/action.yml
+++ b/.github/actions/pnpm-install/action.yml
@@ -1,11 +1,12 @@
name: Install pnpm dependencies
description: |
- Sets up pnpm + Node, restores the shared pnpm store cache, installs, and
- saves the cache only on the default branch.
+ Sets up pnpm + Node, restores the shared pnpm store cache, and installs.
Single source of truth for the pnpm cache key. Every node workflow shares
one namespace (`pnpm-<os>-<lockfile-hash>`), so the store is cached once
instead of fragmenting across runner-arch casings or per-workflow keys.
+ Restore only: .github/workflows/pnpm-store-cache.yml writes the entry on
+ master from a cold install, and explains why no consumer may save it.
Also pre-seeds the Node tool cache (`node-toolcache-<os>-<arch>-<version>`,
master-gated save) so setup-node makes no GitHub API calls on a hit.
@@ -86,7 +87,8 @@ runs:
node-version-file: ${{ inputs.node-version-file }}
token: ${{ inputs.token }}
- # Save only on the default branch, for the same reasons as the pnpm store save below.
+ # Save only on the default branch: a cache written on a PR ref serves that
+ # PR alone and evicts the shared master entries.
- name: Save Node tool cache
if: github.ref == 'refs/heads/master' && steps.node-version.outputs.version != '' && steps.node-toolcache.outputs.cache-hit != 'true'
uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
@@ -100,7 +102,6 @@ runs:
run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
- name: Restore pnpm store
- id: restore
uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
path: ${{ steps.pnpm-store.outputs.path }}
@@ -120,13 +121,3 @@ runs:
npm_config_fetch_retry_mintimeout: 10000
npm_config_fetch_retry_maxtimeout: 60000
run: eval "$INSTALL_COMMAND"
-
- # Save only on the default branch. Caches written on a PR/branch ref are
- # scoped to that ref alone — no other branch can read them, they evict
- # the shared master cache, and they only ever help that one PR's re-runs.
- - name: Save pnpm store
- if: github.ref == 'refs/heads/master' && steps.restore.outputs.cache-hit != 'true'
- uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
diff --git a/.github/workflows/ci-agent-proxy.yml b/.github/workflows/ci-agent-proxy.yml
index 66b812c5e29d..0db8cc536a81 100644
--- a/.github/workflows/ci-agent-proxy.yml
+++ b/.github/workflows/ci-agent-proxy.yml
@@ -60,35 +60,10 @@ jobs:
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- - name: Install pnpm
- uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8
-
- - name: Set up Node.js
- uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
- with:
- node-version-file: .nvmrc
- token: ${{ github.token }}
-
- - name: Get pnpm store path
- id: pnpm-store
- run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
-
- - name: Restore pnpm cache
- uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
- restore-keys: pnpm-${{ runner.os }}-
-
- - name: Install dependencies
- run: pnpm install --frozen-lockfile --filter @posthog/agent-proxy...
-
- - name: Save pnpm cache
- if: github.ref == 'refs/heads/master'
- uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
+ - name: Install pnpm dependencies
+ uses: ./.github/actions/pnpm-install
with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
+ install-command: pnpm install --frozen-lockfile --filter @posthog/agent-proxy...
- name: Typecheck
working-directory: services/agent-proxy
diff --git a/.github/workflows/ci-e2e-playwright.yml b/.github/workflows/ci-e2e-playwright.yml
index 58e03447ba3d..14edd06b307f 100644
--- a/.github/workflows/ci-e2e-playwright.yml
+++ b/.github/workflows/ci-e2e-playwright.yml
@@ -627,8 +627,8 @@ jobs:
# This job checks out the PR head (not the merge ref), so it can't use
# the ./.github/actions/pnpm-install local action — an un-rebased head
- # may not contain it. Cache the store inline instead, sharing the same
- # pnpm-<os>-<lockhash> key as the action and gating the save to master.
+ # may not contain it. Restore the store inline instead, sharing the same
+ # pnpm-<os>-<lockhash> key as the action.
- name: Get pnpm store path
id: pnpm-store
shell: bash
@@ -698,13 +698,6 @@ jobs:
pnpm --filter=@posthog/playwright... install --frozen-lockfile
bin/turbo --filter=@posthog/frontend prepare
- - name: Save pnpm cache
- if: github.ref == 'refs/heads/master'
- uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
-
- name: Log in to Docker Hub
continue-on-error: true
if: ${{ env.DOCKERHUB_USERNAME != '' && env.DOCKERHUB_TOKEN != '' }}
diff --git a/.github/workflows/ci-frontend.yml b/.github/workflows/ci-frontend.yml
index edeb1f7eec9a..9078062f8a34 100644
--- a/.github/workflows/ci-frontend.yml
+++ b/.github/workflows/ci-frontend.yml
@@ -544,13 +544,6 @@ jobs:
- name: Install package.json dependencies with pnpm
run: pnpm --filter=@posthog/playwright... install --frozen-lockfile
- - name: Save pnpm cache
- if: github.ref == 'refs/heads/master'
- uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
-
- name: Prepare frontend
run: bin/turbo --filter=@posthog/frontend prepare
diff --git a/.github/workflows/ci-integration-service.yml b/.github/workflows/ci-integration-service.yml
index 415dbde9e58a..41550c5d0e3e 100644
--- a/.github/workflows/ci-integration-service.yml
+++ b/.github/workflows/ci-integration-service.yml
@@ -38,39 +38,14 @@ jobs:
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- - name: Install pnpm
- uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8
-
- - name: Set up Node.js
- uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
- with:
- node-version-file: .nvmrc
- token: ${{ github.token }}
-
- - name: Get pnpm store path
- id: pnpm-store
- run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
-
- - name: Restore pnpm cache
- uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
- restore-keys: pnpm-${{ runner.os }}-
-
# --ignore-scripts: nothing in this package's dependency tree runs code at
# install time. This service reads every platform integration credential we
# own, so an install-time script in a transitive package is exactly the
# supply-chain path worth closing.
- - name: Install dependencies
- run: pnpm install --frozen-lockfile --ignore-scripts --filter @posthog/integration-service...
-
- - name: Save pnpm cache
- if: github.ref == 'refs/heads/master'
- uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
+ - name: Install pnpm dependencies
+ uses: ./.github/actions/pnpm-install
with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
+ install-command: pnpm install --frozen-lockfile --ignore-scripts --filter @posthog/integration-service...
- name: Typecheck
working-directory: services/integration-service
diff --git a/.github/workflows/ci-storybook.yml b/.github/workflows/ci-storybook.yml
index f35acc0162ed..ef3fdcad53bf 100644
--- a/.github/workflows/ci-storybook.yml
+++ b/.github/workflows/ci-storybook.yml
@@ -234,8 +234,8 @@ jobs:
# This job checks out the PR head (not the merge ref), so it can't use
# the ./.github/actions/pnpm-install local action — an un-rebased head
- # may not contain it. Cache the store inline instead, sharing the same
- # pnpm-<os>-<lockhash> key as the action and gating the save to master.
+ # may not contain it. Restore the store inline instead, sharing the same
+ # pnpm-<os>-<lockhash> key as the action.
- name: Set up Node.js
uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
with:
@@ -256,13 +256,6 @@ jobs:
- name: Install dependencies
run: pnpm --filter=@posthog/storybook... install --frozen-lockfile
- - name: Save pnpm cache
- if: github.ref == 'refs/heads/master'
- uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- with:
- path: ${{ steps.pnpm-store.outputs.path }}
- key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
-
# Vite's dependency optimization cache (node_modules/.vite) is the most
# expensive part of the build — without it, Vite re-bundles every module
# from scratch, which dwarfs the rest of the build. The
@@ -489,6 +482,7 @@ jobs:
run: echo "path=$(pnpm store path --silent)" >> $GITHUB_OUTPUT
- name: Restore pnpm cache
+ id: pnpm-restore
uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
path: ${{ steps.pnpm-store.outputs.path }}
@@ -498,10 +492,11 @@ jobs:
- name: Install package.json dependencies with pnpm
run: pnpm --filter=@posthog/storybook... install --frozen-lockfile
+ # Inside the playwright container pnpm keeps its store on the workspace mount.
+ # actions/cache keys an entry on key plus path, so this is a separate entry
+ # from the one pnpm-store-cache.yml writes, and one shard saves it.
- name: Save pnpm cache
- # The cache key is identical across all visual-regression shards, so only one
- # shard needs to write it — gating to a single shard avoids redundant concurrent saves.
- if: github.ref == 'refs/heads/master' && matrix.shard == 1 && matrix.browser == 'chromium'
+ if: github.ref == 'refs/heads/master' && matrix.shard == 1 && matrix.browser == 'chromium' && steps.pnpm-restore.outputs.cache-hit != 'true'
uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
with:
path: ${{ steps.pnpm-store.outputs.path }}
diff --git a/.github/workflows/pnpm-store-cache.yml b/.github/workflows/pnpm-store-cache.yml
new file mode 100644
index 000000000000..7324cdcee67d
--- /dev/null
+++ b/.github/workflows/pnpm-store-cache.yml
@@ -0,0 +1,93 @@
+name: pnpm store cache
+
+# The one writer of the `pnpm-<os>-<lockhash>` entry that every node job restores
+# (see .github/actions/pnpm-install). A cold install from an empty store gives an
+# entry that holds exactly what the lockfile pins. Consumers only restore: a save
+# from a job that restored an older entry first would carry that entry's packages
+# forward, and the entry would grow with every lockfile change.
+#
+# GitHub-hosted and Depot runners use separate cache stores, so the job runs on both.
+#
+# The ci-storybook visual-regression shards save under the same key from inside the
+# playwright container, where the store sits at another path. actions/cache keys an
+# entry on key plus path, so that is a separate entry this job never touches.
+
+on:
+ push:
+ branches:
+ - master
+ paths:
+ - pnpm-lock.yaml
+ - .github/workflows/pnpm-store-cache.yml
+ # A PR that edits this file runs the install as a dry run; the save stays master only.
+ pull_request:
+ paths:
+ - .github/workflows/pnpm-store-cache.yml
+ workflow_dispatch:
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: false
+
+permissions:
+ contents: read
+
+env:
+ # pnpm reads npm_config_*; a cold install fetches every package, so ride out blips.
+ npm_config_fetch_retries: 3
+ npm_config_fetch_retry_mintimeout: 10000
+ npm_config_fetch_retry_maxtimeout: 60000
+
+jobs:
+ write:
+ name: Write pnpm store (${{ matrix.runner }})
+ # The dry run installs with build scripts on a Depot runner, so it is for same-repo PRs only.
+ if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
+ runs-on: ${{ matrix.runner }}
+ timeout-minutes: 30
+ strategy:
+ fail-fast: false
+ matrix:
+ runner: [ubuntu-latest, depot-ubuntu-24.04]
+ steps:
+ - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+
+ - name: Install pnpm
+ uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8
+
+ - name: Fix node-gyp permissions
+ run: find ~/setup-pnpm -name gyp_main.py -exec chmod +x {} +
+
+ - name: Set up Node.js
+ uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
+ with:
+ node-version-file: .nvmrc
+ token: ${{ github.token }}
+
+ - name: Get pnpm store path
+ id: pnpm-store
+ run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT"
+
+ - name: Check for an existing entry
+ id: lookup
+ uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
+ with:
+ path: ${{ steps.pnpm-store.outputs.path }}
+ key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
+ lookup-only: true
+
+ # Build scripts run on purpose: pnpm keeps their output in the store, so
+ # consumers get prebuilt native packages instead of compiling them per job.
+ # A PR dry run always installs, so a broken writer fails here and not on master.
+ - name: Install into an empty store
+ if: steps.lookup.outputs.cache-hit != 'true' || github.event_name == 'pull_request'
+ run: pnpm install --frozen-lockfile
+
+ # Master only: Depot Cache has no branch isolation, so a dispatch from a
+ # branch would write the key every PR restores.
+ - name: Save pnpm store
+ if: github.ref == 'refs/heads/master' && steps.lookup.outputs.cache-hit != 'true'
+ uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
+ with:
+ path: ${{ steps.pnpm-store.outputs.path }}
+ key: pnpm-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}
diff --git a/tools/hogli-commands/hogli_commands/workflow_lint/checks/cache_writes.py b/tools/hogli-commands/hogli_commands/workflow_lint/checks/cache_writes.py
index b0761f05c95f..e317fad08c4e 100644
--- a/tools/hogli-commands/hogli_commands/workflow_lint/checks/cache_writes.py
+++ b/tools/hogli-commands/hogli_commands/workflow_lint/checks/cache_writes.py
@@ -26,8 +26,8 @@ A write is FINE when any of these hold:
shared cache that fragmented.
Scans ``.github/workflows/**`` and ``.github/actions/*/action.yml`` — cache
-writes live in both (the shared ``pnpm-install`` action saves the store, and
-un-gating it would leak from every workflow that uses it). Composite actions have
+writes live in both (the shared ``pnpm-install`` action saves the Node tool cache,
+and un-gating it would leak from every workflow that uses it). Composite actions have
no triggers, so they run wherever called: condition 1 never applies to them.
"""