Cloud Technology

How do I use stash and unstash to move files between different nodes in a Jenkins Pipeline?

GR Asked by Gregory Evans · 14-07-2025
0 upvotes 13,437 views 0 comments
The question

I am setting up a distributed Jenkins Pipeline where my initial build runs inside a Docker container, but the deployment needs to happen on a separate physical worker node. I need to move the compiled artifacts from the first stage to the second. What is the correct syntax for using stash and unstash to preserve these files across different workspaces, and are there any specific limitations regarding file size or persistence I should be aware of?

3 answers

0
AM
Answered on 16-07-2025

The stash command is designed specifically for saving files from the current workspace to the Jenkins controller so they can be retrieved later in the same pipeline run using unstash. For example, in your build stage, you would use stash name: 'my-artifacts', includes: 'target/*.jar'. Then, in your deployment stage on a different node, simply call unstash 'my-artifacts'. It is important to remember that stashes are temporary and are deleted once the pipeline completes. Unlike the archiveArtifacts command, stashing is meant for internal pipeline flow rather than long-term storage. However, avoid stashing very large files (over 100MB) frequently, as this can put significant pressure on the network and storage of your Jenkins controller.

0
BR
Answered on 17-07-2025

Could you clarify if you need these files to persist after the build is finished, or are they only necessary for the immediate execution of the downstream stages?

MA 18-07-2025

Brian's question is vital because it determines your storage strategy. If you need the files for audit or later use, you should use archiveArtifacts instead. However, for moving data between a build container and a worker node in a single run, stash is much faster. In complex Cloud Technology setups, I often see people use a shared filesystem like NFS or an S3 bucket for massive datasets, but for standard binaries and test reports, the built-in stash mechanism is the cleanest way to maintain a stateless environment across your Jenkins nodes.

0
JE
Answered on 20-07-2025

Using stash is great because it automatically handles the transfer between different nodes for you. Just make sure your "includes" pattern correctly matches the file structure in your build container.

GR 21-07-2025

I agree with Jessica; the glob syntax can be tricky. I always recommend using a very specific "includes" pattern to avoid accidentally stashing thousands of small log files, which can really slow down the unstash process on the target node.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session