|
| 1 | +name: Prime pnpm store cache |
| 2 | + |
| 3 | +# Saving the pnpm store cache used to happen inside every job that called |
| 4 | +# node-setup, so any push to main that triggered more than one workflow |
| 5 | +# raced to save the same cache key. Do it once, here, decoupled from CI. |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + # Only the latest push's lockfile matters for the cache, so a superseded |
| 14 | + # run for an older commit is wasted work — cancel it instead of queuing. |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +permissions: |
| 18 | + actions: write # Required to delete stale pnpm-store caches |
| 19 | + contents: read |
| 20 | + |
| 21 | +jobs: |
| 22 | + prime-cache: |
| 23 | + name: Save pnpm store cache |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v7 |
| 27 | + |
| 28 | + - name: Setup pnpm |
| 29 | + uses: pnpm/action-setup@v6 |
| 30 | + |
| 31 | + - name: Setup Node.js |
| 32 | + uses: actions/setup-node@v7 |
| 33 | + with: |
| 34 | + node-version: 26 |
| 35 | + |
| 36 | + - name: Get pnpm store path |
| 37 | + id: pnpm-store |
| 38 | + run: echo "path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT" |
| 39 | + |
| 40 | + - name: Check for an existing exact-match cache |
| 41 | + id: pnpm-store-restore |
| 42 | + uses: actions/cache/restore@v6 |
| 43 | + with: |
| 44 | + path: ${{ steps.pnpm-store.outputs.path }} |
| 45 | + key: pnpm-store-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }} |
| 46 | + lookup-only: true |
| 47 | + |
| 48 | + - name: Install dependencies |
| 49 | + if: steps.pnpm-store-restore.outputs.cache-hit != 'true' |
| 50 | + run: pnpm install --frozen-lockfile |
| 51 | + |
| 52 | + # Cache keys are immutable, so every lockfile change saves a brand-new |
| 53 | + # store on top of the previous one and old package versions never get |
| 54 | + # dropped. Pruning here keeps the saved cache scoped to what this |
| 55 | + # lockfile actually needs instead of growing unbounded. |
| 56 | + - name: Prune pnpm store |
| 57 | + if: steps.pnpm-store-restore.outputs.cache-hit != 'true' |
| 58 | + run: pnpm store prune |
| 59 | + |
| 60 | + - name: Save pnpm store |
| 61 | + if: steps.pnpm-store-restore.outputs.cache-hit != 'true' |
| 62 | + uses: actions/cache/save@v6 |
| 63 | + with: |
| 64 | + path: ${{ steps.pnpm-store.outputs.path }} |
| 65 | + key: pnpm-store-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }} |
| 66 | + |
| 67 | + # Cache keys are immutable, so every save creates a brand-new entry |
| 68 | + # instead of replacing the previous one — old pnpm-store entries for |
| 69 | + # main never get evicted by anything but GitHub's 10GB LRU quota. |
| 70 | + # Delete them here, keeping a couple of recent fallbacks so an |
| 71 | + # in-flight workflow always finds a cache to restore from. |
| 72 | + - name: Delete stale pnpm store caches |
| 73 | + if: steps.pnpm-store-restore.outputs.cache-hit != 'true' |
| 74 | + continue-on-error: true |
| 75 | + env: |
| 76 | + GH_TOKEN: ${{ github.token }} |
| 77 | + run: | |
| 78 | + currentKey="pnpm-store-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}" |
| 79 | + cacheIds=$(gh cache list --repo "${{ github.repository }}" --ref refs/heads/main \ |
| 80 | + --key "pnpm-store-${{ runner.os }}-" --limit 100 \ |
| 81 | + --json id,key,createdAt \ |
| 82 | + --jq "sort_by(.createdAt) | reverse | .[2:] | .[] | select(.key != \"$currentKey\") | .id") |
| 83 | + for id in $cacheIds; do |
| 84 | + echo "Deleting stale pnpm store cache $id" |
| 85 | + gh cache delete "$id" --repo "${{ github.repository }}" || true |
| 86 | + done |
0 commit comments