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.

4.7 KB
Raw Blame
name: Set up pnpm and Node
description: |
    Puts pnpm and the `.nvmrc` Node on PATH. Installs nothing.

    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.

    Use `pnpm-install` instead when the job needs the workspace installed.

inputs:
    node-version-file:
        description: 'File specifying the Node version.'
        required: false
        default: .nvmrc
    token:
        description: |
            GitHub token for setup-node's version-manifest lookup and Node download.
            Pass an app token at high-fanout call sites to keep those API calls off
            the shared per-repo GITHUB_TOKEN core bucket.
        required: false
        default: ${{ github.token }}

runs:
    using: composite
    steps:
        # The action fetches pnpm from registry.npmjs.org on every job. npm's
        # default fetch-timeout is 5 minutes, so one stalled connection costs
        # longer than several jobs' whole timeout-minutes budget. Cap a single
        # attempt well above a healthy fetch and retry instead of waiting.
        - name: Install pnpm
          uses: pnpm/action-setup@0e279bb959325dab635dd2c09392533439d90093 # v6.0.8
          env:
              NPM_CONFIG_FETCH_TIMEOUT: 45000
              NPM_CONFIG_FETCH_RETRIES: 2
              NPM_CONFIG_FETCH_RETRY_MINTIMEOUT: 5000
              NPM_CONFIG_FETCH_RETRY_MAXTIMEOUT: 15000
              # The action bootstraps pnpm with `npm ci`, which audits by default. That audit
              # covers two packages and gates nothing, but it blocks until npm answers, so a
              # degraded audit endpoint stalls the step until npm's 300s fetch timeout. That
              # alone exhausts a 5 minute job budget before any test runs.
              NPM_CONFIG_AUDIT: 'false'
              NPM_CONFIG_FUND: 'false'

        - name: Fix node-gyp permissions
          shell: bash
          # Depot CI's restored pnpm store can land node-gyp's gyp_main.py without +x.
          # Locate it across pnpm/action-setup layouts and never fail if it is absent.
          run: find ~/setup-pnpm -name gyp_main.py -path '*/node-gyp/*' -exec chmod +x {} + 2>/dev/null || true

        - name: Resolve Node version
          id: node-version
          shell: bash
          # An empty version (missing file or non-exact pin) skips the tool cache
          # steps, so setup-node falls back to resolving the version itself. Only
          # exact x.y.z pins are cacheable: the tool-cache directory is named after
          # the resolved version, so a range spec would never match it.
          env:
              NODE_VERSION_FILE: ${{ inputs.node-version-file }}
          run: |
              version=""
              [[ -f "$NODE_VERSION_FILE" ]] && version=$(tr -d 'v[:space:]' < "$NODE_VERSION_FILE")
              [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || version=""
              echo "version=$version" >> "$GITHUB_OUTPUT"

        # A tool-cache hit turns setup-node into a filesystem lookup. On a miss it
        # fetches the version manifest from api.github.com (two rate-limited API
        # calls per job) and downloads the tarball, on every Node job, because
        # runner images don't ship the .nvmrc version.
        - name: Restore Node tool cache
          id: node-toolcache
          if: steps.node-version.outputs.version != ''
          # GitHub Actions rejects timeout-minutes in a composite step, so only this mirror bounds the restore.
          timeout-minutes: 2
          continue-on-error: true
          uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
          with:
              path: ${{ runner.tool_cache }}/node/${{ steps.node-version.outputs.version }}
              key: node-toolcache-${{ runner.os }}-${{ runner.arch }}-${{ steps.node-version.outputs.version }}

        - name: Set up Node.js
          uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
          with:
              node-version-file: ${{ inputs.node-version-file }}
              token: ${{ inputs.token }}

        # Save only on the default branch: caches written on a PR ref are scoped to
        # that ref alone, evict the shared master entry, and help only that one PR.
        - name: Save Node tool cache
          if: github.ref == 'refs/heads/master' && github.event_name != 'api' && steps.node-version.outputs.version != '' && steps.node-toolcache.outputs.cache-hit != 'true'
          uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
          with:
              path: ${{ runner.tool_cache }}/node/${{ steps.node-version.outputs.version }}
              key: node-toolcache-${{ runner.os }}-${{ runner.arch }}-${{ steps.node-version.outputs.version }}