0
0
Jenkinsdevops~10 mins

Stash and unstash for passing data in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Stash and unstash for passing data
Start Pipeline
Stash files
Move to another stage
Unstash files
Use files in new stage
Pipeline continues or ends
This flow shows how Jenkins saves files with stash, then retrieves them later with unstash to share data between pipeline stages.
Execution Sample
Jenkins
stage('Build') {
  stash name: 'app', includes: '**/*.jar'
}
stage('Test') {
  unstash 'app'
  sh 'java -jar app.jar'
}
This Jenkins pipeline stashes jar files in the Build stage, then unstashes and runs them in the Test stage.
Process Table
StepActionFiles AffectedPipeline StageResult
1Start Build stagenoneBuildBuild stage begins
2Stash files named 'app'*.jar filesBuildFiles saved for later stages
3End Build stagenoneBuildBuild stage ends
4Start Test stagenoneTestTest stage begins
5Unstash files named 'app'*.jar filesTestFiles restored for use
6Run 'java -jar app.jar'app.jarTestApplication runs using unstashed files
7End Test stagenoneTestTest stage ends
8Pipeline endsnonePipelineAll stages complete
💡 Pipeline ends after all stages complete and files are passed via stash/unstash
Status Tracker
VariableStartAfter Step 2After Step 5Final
Stash 'app'empty*.jar files savedfiles restoredfiles used in Test stage
Key Moments - 3 Insights
Why do we need to stash files in one stage and unstash in another?
Because each Jenkins stage runs in a separate workspace, stashing saves files so they can be restored later in a different stage, as shown in steps 2 and 5.
What happens if you try to unstash files that were never stashed?
The pipeline will fail at the unstash step (step 5) because there are no saved files with that stash name, causing an error.
Does unstashing overwrite existing files in the workspace?
Yes, unstashing restores files and can overwrite files with the same name in the current workspace, as seen when files are restored in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what files are saved during the stash step?
A*.jar files
BNo files
C*.txt files
DAll files
💡 Hint
Check Step 2 in the execution table where stash saves files.
At which step does the pipeline restore the stashed files?
AStep 7
BStep 2
CStep 5
DStep 1
💡 Hint
Look for the unstash action in the execution table.
If the stash name was changed in the unstash step to 'app2', what would happen?
AFiles would still be restored
BPipeline would fail at unstash
CFiles would be partially restored
DPipeline would skip unstash silently
💡 Hint
Refer to key moment about unstashing files that were never stashed.
Concept Snapshot
Jenkins stash saves files in one stage for later use.
Unstash restores those files in another stage.
Use stash name to identify files.
Each stage has separate workspace.
Stash/unstash passes data between stages safely.
Full Transcript
This visual execution shows how Jenkins pipelines use stash and unstash to pass files between stages. First, files matching a pattern are saved with stash in the Build stage. Then, in the Test stage, unstash restores those files so they can be used. Each step is tracked showing when files are saved, restored, and used. Key points include that stash is needed because each stage runs separately, unstash requires the correct stash name, and unstashing overwrites files in the workspace. The quiz tests understanding of when files are saved and restored and what happens if names don't match. This helps beginners see the step-by-step flow of passing data in Jenkins pipelines.