Skip to main content
In this lesson you will learn how to use the Jenkins stash and unstash Pipeline steps to move files between stages within a single pipeline run. This technique is commonly used to cache build artifacts or dependency directories (for example, node_modules) so later stages, possibly running on different agents, can reuse them without repeating expensive operations.
  • stash archives a set of files from the current workspace so they can be restored later in another stage of the same pipeline run.
  • unstash restores a previously created stash by name into the current workspace.
  • By default, stashes are discarded at the end of the pipeline run. Use persistent artifact storage (or plugins that support retention) if you need data to persist across separate runs.
A screenshot of the Jenkins documentation page describing the "stash" Pipeline step. The page shows the heading "stash: Stash some files to be used later in the build," explanatory text and option fields on the right, and a User Handbook navigation sidebar on the left.
Stashes are specific to a single pipeline run. If you want to retain artifacts between runs, consider using persistent artifact storage or plugins that support preserving stashes.

Typical workflow

  1. Install dependencies or create artifacts in an early stage.
  2. stash the produced files (e.g., node_modules/**) immediately after they are created.
  3. unstash the named stash in a later stage, even if that stage runs on a different agent.
  4. Continue with tests or other tasks using the restored files.
Example stash invocation (simple snippet):

Example: Node.js pipeline before and after stashing

Initial Jenkinsfile fragment (before stashing):
Add the stash immediately after npm install so the installed dependencies are archived for later stages:
If you prefer the later stage to skip npm install entirely and rely solely on the previously stashed node_modules, remove the install step and unstash instead:

Console output (example excerpts)

From the stage that creates the stash:
From the stage that restores the stash:

Why use stash + unstash

  • Enables moving files between stages that run on different agents (e.g., different nodes, Docker containers, or Kubernetes pods).
  • Avoids repeating expensive operations like re-installing dependencies during the same pipeline run.
  • Keeps workspaces clean by allowing you to store only the required files and restore them on demand.

Quick comparison: stash vs archived artifacts

Notes and gotchas

  • Keep your includes/excludes precise. Stash patterns use Ant-style globs (e.g., node_modules/**). Over-broad patterns increase stash size and transfer time.
  • Stashes are per-run and ephemeral. To reuse artifacts across separate builds, use archiveArtifacts, an external cache, or a plugin that retains stashes.
  • Some filesystem issues (rename, locking) can surface during install; stashing can help avoid repeating a problematic step within the same run but does not fix the underlying filesystem issue.
  • If your pipeline creates many or very large stashes, consider using a persistent caching mechanism (artifact repository, shared volume, or build cache) for efficiency.
Stashed files are discarded when the pipeline run finishes. Do not rely on stash for long-term caching across separate builds—use artifact storage or a caching plugin for persistent reuse.

Best practices

  • Stash only what is necessary (e.g., node_modules/** rather than **).
  • Name stashes clearly (e.g., solar-system-node-modules) so they are easy to reference in later stages.
  • Use options { retry(n) } around fragile steps like installs to reduce flakiness before stashing.
  • Monitor stash sizes and transfer times; large stashes may negate the performance benefits.
That’s the essence of using stash and unstash to move files between stages inside the same Jenkins pipeline run—an effective way to cache dependencies and reduce redundant work.

Watch Video