Cache persistence
Cache persistence is the post-install part of the action that stores the pnpm store for a later run. It runs in the GitHub Actions post phase and saves the store only when caching is enabled, the current dependency state did not already produce an exact cache hit, and the store path exists.
Post-install save flow
The post-install flow uses state recorded during the main phase to decide whether the pnpm store can be saved. It prunes the store first when the action also ran dependency installation, then checks the cache state and store path before it sends the save request.
The flow has these stages:
- GitHub Actions calls the action's post entry point.
- The action restores the input values from saved state.
- The store is pruned when at least one install command was configured.
- Cache saving stops when the
cacheinput is disabled. - Cache saving stops when the restored key equals the current primary key.
- Cache saving stops when the saved store path does not exist.
- Otherwise, the pnpm store is saved under the current primary key.
This diagram shows how the main-phase cache state controls the post-phase save decision.
flowchart TD
A[Post phase starts] --> B[Read saved inputs and cache state]
B --> C{Install commands configured?}
C -- Yes --> D[Prune pnpm store]
C -- No --> E{Caching enabled?}
D --> E
E -- No --> F[Do not save]
E -- Yes --> G{Restored key equals primary key?}
G -- Yes --> F
G -- No --> H{Store path exists?}
H -- No --> F
H -- Yes --> I[Save pnpm store under primary key]
References: src/index.ts• L10-15 src/index.ts• L33-37 src/…/index.ts• L5-18 src/…/index.ts• L5-13 src/…/run.ts• L5-29
Cache eligibility
An eligible save represents a store that may add useful data for a future dependency state. The action uses the cache input, the relationship between the restored key and the primary key, and the presence of the store path as its save guards.
| Condition | Result |
|---|---|
cache is false | The post phase does not try to save the store. |
cache is true and the restored key equals the primary key | The action treats the cache as an exact hit and does not save it again. |
cache is true, the keys differ or no cache was restored, and the store path exists | The action attempts to save the store under the primary key. |
| The store path does not exist | The action does not send a cache-save request. |
The primary key includes the runner operating system, CPU architecture, and a hash of the configured dependency paths. A prefix match can restore an older store, but it does not count as an exact hit. This lets the install step add data for a changed lockfile before the post phase saves a new key. See Cache restoration for how these keys are made and restored.
References: src/…/index.ts• L5-12 src/…/run.ts• L5-29 src/…/run.ts• L12-32 src/…/index.ts• L5-12 action.yml• L18-25
Store preparation
Store pruning prepares the cache after installation by removing pnpm data that is no longer needed. The action runs this step only when run_install produced one or more install commands; a prune error or non-zero exit status produces a warning and does not block the later cache-save attempt.
When no install command is configured, the action skips pruning because the run did not change the store through this action. The post phase still evaluates the cache-save guards when caching is enabled.
References: src/index.ts• L33-37 src/…/index.ts• L5-29 src/…/run-install.ts• L21-30
Future-run effect
The saved store becomes a candidate for restoration by a later run whose cache settings and dependency paths produce a matching key or restore-key prefix. An exact key avoids repeating installation work, while a prefix match supplies reusable package data and still allows the current run to create a more specific cache.
This page covers the save side of the lifecycle. See Action runtime orchestration for the full run, Cache restoration for the pre-install side, and pnpm store pruning for the cleanup step. Related install behavior is described in Dependency installation.
References: src/…/run.ts• L17-40 src/…/run.ts• L9-29 action.yml• L18-25
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the post-phase state names aligned with the cache-restore state that supplies the store path, primary key, and restored key.
- Keep the primary-key inputs aligned with the dependency paths used to hash the cache. A change can silently create misses or save unrelated store data.
- Preserve the distinction between an exact cache hit and a restore-key prefix match. The latter must remain eligible for a new save after installation.
- Keep the
cacheinput guard consistent between the main-phase restore and post-phase save paths. - Keep store pruning before cache saving so stale store data is not persisted after an install run.
- Keep the store-path check before the cache service call so a missing pnpm store cannot create an invalid save request.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The restored key equals the primary key | The action skips the save because the current cache already represents the dependency state. | Compare the saved restored key with the saved primary key before calling the cache service. |
| A restore-key prefix matches but the primary key differs | The run can reuse an older store, but it must save the resulting store under the new primary key. | Treat only an exact primary-key match as a cache hit. |
| The saved store path does not exist | The action skips the save instead of sending a path that cannot be cached. | Check the path before calling the cache service. |
| A configured install run leaves stale store data | Unneeded package data can be included in the next cache. | Prune the store before the save attempt. |
| Store pruning returns an error or non-zero status | The cache save still runs and may include unpruned data. | Emit a warning and review the prune failure when cache size or contents are unexpected. |
Key files
| File | Why to read it |
|---|---|
| src/index.ts | Shows the main and post entry points and the order of pruning and cache saving. |
| src/…/index.ts | Shows the top-level guard that disables saving when caching is off. |
| src/…/run.ts | Shows the exact-hit check, store-path check, and cache save request. |
| src/…/run.ts | Shows how the store path, primary key, and restored key are prepared for the post phase. |
| src/…/index.ts | Shows when pruning runs and why its failures do not stop the save attempt. |
| action.yml | Defines the cache inputs and the GitHub Actions main/post lifecycle. |