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
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
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
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
Hint
Use sh 'cat message.txt' to display the file content in the console.
Practice
(1/5)
1. What is the main purpose of stash and unstash in Jenkins pipelines?
easy
A. To temporarily save and later restore files between pipeline stages
B. To permanently store files on the Jenkins master server
C. To delete files after a pipeline stage completes
D. To send notifications when a stage finishes
Solution
Step 1: Understand the role of stash
The stash command saves files temporarily during a pipeline run.
Step 2: Understand the role of unstash
The unstash command restores those saved files in later stages or nodes.
Final Answer:
To temporarily save and later restore files between pipeline stages -> Option A
Quick Check:
stash + unstash = temporary file sharing [OK]
Hint: Remember stash saves, unstash restores files between stages [OK]
Common Mistakes:
Thinking stash permanently stores files
Confusing stash with deleting files
Assuming stash sends notifications
2. Which of the following is the correct syntax to stash files named output.log in a Jenkins pipeline?
easy
A. stash { name: 'logs', files: 'output.log' }
B. stash('logs', 'output.log')
C. stash name = 'logs' include = 'output.log'
D. stash name: 'logs', includes: 'output.log'
Solution
Step 1: Recall stash syntax
The correct stash syntax uses named parameters: stash name: '...', includes: '...'.
Step 2: Match options to syntax
stash name: 'logs', includes: 'output.log' matches the correct syntax exactly.
Final Answer:
stash name: 'logs', includes: 'output.log' -> Option D
Quick Check:
stash uses named params: name and includes [OK]
Hint: Use named parameters: name and includes for stash [OK]
But the Deploy stage fails with cat: file.txt: No such file or directory. What is the most likely cause?
medium
A. The unstash command is missing in the Deploy stage
B. The file.txt was not created before stashing
C. The stash name used in unstash does not match the stash name
D. The pipeline syntax for stash is incorrect
Solution
Step 1: Check stash and unstash names
The stash is named 'data' but unstash uses 'artifacts', so names do not match.
Step 2: Verify file creation and stash syntax
file.txt is created before stash, unstash command is present, and stash syntax looks correct. Different agents require proper stash/unstash to pass files.
Step 3: Identify the cause
Mismatched names cause unstash to fail silently, so file.txt is not available in Deploy workspace.
Final Answer:
The stash name used in unstash does not match the stash name -> Option C
Quick Check:
stash and unstash names must match exactly [OK]
Hint: Check stash and unstash names match exactly [OK]
Common Mistakes:
Using different names in stash and unstash
Forgetting to create the file before stash
Omitting unstash command in later stage
5. You want to pass multiple files from a Build stage to a Test stage in Jenkins. Which is the best way to stash and unstash these files?
hard
A. stash name: 'allFiles', includes: '*.log, *.txt'
...
unstash 'allFiles'
B. stash name: 'allFiles', includes: '**/*.log, **/*.txt'
...
unstash 'allFiles'
C. stash name: 'allFiles', includes: '**/*.log **/*.txt'
...
unstash 'allFiles'
D. stash name: 'allFiles', includes: '*.log *.txt'
...
unstash 'allFiles'
Solution
Step 1: Understand includes pattern syntax
Jenkins stash supports comma-separated glob patterns with '**/' for recursive matching.
Step 2: Compare options
stash name: 'allFiles', includes: '**/*.log, **/*.txt'
...
unstash 'allFiles' uses comma-separated recursive globs, which is correct. Options A, B, and C either lack recursion (A, B) or use spaces instead of commas (B, C). For example, '**/*.log **/*.txt' misses comma separator.