0
0
Jenkinsdevops~30 mins

Stash and unstash for passing data in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Passing Data Between Jenkins Pipeline Stages Using Stash and Unstash
📖 Scenario: You are working on a Jenkins pipeline that builds a simple project. You want to save some files in one stage and use them in another stage. This is like packing your lunch in a box (stash) and taking it to school to eat later (unstash).
🎯 Goal: Build a Jenkins pipeline script that creates a file in one stage, stashes it, then unstashes and reads it in the next stage.
📋 What You'll Learn
Create a file named message.txt with the text Hello from stash! in the Build stage
Use stash to save the message.txt file with the name myFiles
In the Test stage, use unstash to retrieve the saved files
Read and print the contents of message.txt in the Test stage
💡 Why This Matters
🌍 Real World
In real Jenkins pipelines, you often need to pass files like build outputs, test reports, or configuration files between stages. Stash and unstash help you do this easily without sharing the whole workspace.
💼 Career
Understanding stash and unstash is essential for Jenkins pipeline developers and DevOps engineers to create efficient, modular, and reliable CI/CD workflows.
Progress0 / 4 steps
1
Create a file in the Build stage
In the Build stage, create a file named message.txt with the text Hello from stash! using the sh step.
Jenkins
Need a hint?

Use the sh step to run a shell command that writes text to a file.

2
Stash the file in the Build stage
In the Build stage, after creating message.txt, add a stash step to save the file with the name myFiles and include the file message.txt.
Jenkins
Need a hint?

Use stash name: 'myFiles', includes: 'message.txt' to save the file.

3
Unstash the file in the Test stage
Add a new stage called Test. Inside its steps, use unstash with the name myFiles to retrieve the stashed file.
Jenkins
Need a hint?

Use unstash 'myFiles' inside the Test stage steps.

4
Read and print the file content in the Test stage
In the Test stage, after unstash, add a sh step to print the contents of message.txt using cat message.txt.
Jenkins
Need a hint?

Use sh 'cat message.txt' to display the file content in the console.