Stash and unstash for passing data in Jenkins - Time & Space Complexity
We want to understand how the time to stash and unstash data changes as the amount of data grows.
How does Jenkins handle passing files between pipeline stages as data size increases?
Analyze the time complexity of the following Jenkins pipeline snippet.
stage('Build') {
steps {
sh 'make build'
stash name: 'app', includes: '**/target/**'
}
}
stage('Test') {
steps {
unstash 'app'
sh 'make test'
}
}
This code builds the app, saves the build files with stash, then retrieves them with unstash for testing.
Look for repeated actions that affect time.
- Primary operation: Stashing and unstashing files involves copying files to and from the Jenkins master.
- How many times: Each stash and unstash runs once per pipeline execution, but the time depends on the number and size of files.
As the number or size of files grows, the time to stash and unstash grows roughly in proportion.
| Input Size (files or total size) | Approx. Operations (file copy time) |
|---|---|
| 10 small files | Low time, quick copy |
| 100 files or larger size | About 10 times longer |
| 1000 files or very large size | Much longer, roughly 100 times more work |
Pattern observation: Time grows roughly linearly with the amount of data being stashed or unstashed.
Time Complexity: O(n)
This means the time to stash and unstash grows directly with the number and size of files involved.
[X] Wrong: "Stash and unstash time stays the same no matter how many files we pass."
[OK] Correct: More files or bigger files take more time to copy, so time grows with data size.
Understanding how data passing scales helps you design efficient pipelines and explain trade-offs clearly.
"What if we compressed files before stashing? How would that affect the time complexity?"
Practice
stash and unstash in Jenkins pipelines?Solution
Step 1: Understand the role of stash
Thestashcommand saves files temporarily during a pipeline run.Step 2: Understand the role of unstash
Theunstashcommand restores those saved files in later stages or nodes.Final Answer:
To temporarily save and later restore files between pipeline stages -> Option AQuick Check:
stash + unstash = temporary file sharing [OK]
- Thinking stash permanently stores files
- Confusing stash with deleting files
- Assuming stash sends notifications
output.log in a Jenkins pipeline?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 DQuick Check:
stash uses named params: name and includes [OK]
- Using parentheses like a function call
- Using incorrect parameter names like files
- Using assignment (=) instead of colon (:)
stage('Build') {
steps {
sh 'echo Hello > message.txt'
stash name: 'msg', includes: 'message.txt'
}
}
stage('Test') {
steps {
unstash 'msg'
sh 'cat message.txt'
}
}What will be the output of the
cat message.txt command in the Test stage?Solution
Step 1: Understand stash saves message.txt
The Build stage creates message.txt with content 'Hello' and stashes it as 'msg'.Step 2: Unstash restores message.txt in Test stage
The Test stage unstashes 'msg', restoring message.txt, then runscat message.txt.Final Answer:
Hello -> Option AQuick Check:
unstash restores files, so cat shows 'Hello' [OK]
- Assuming unstash does not restore files
- Expecting an error because stages run separately
- Thinking stash deletes files
stage('Build') {
agent { label 'build' }
steps {
sh 'touch file.txt'
stash name: 'data', includes: 'file.txt'
}
}
stage('Deploy') {
agent { label 'deploy' }
steps {
unstash 'artifacts'
sh 'cat file.txt'
}
}But the Deploy stage fails with
cat: file.txt: No such file or directory. What is the most likely cause?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.txtis 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 CQuick Check:
stash and unstash names must match exactly [OK]
- Using different names in stash and unstash
- Forgetting to create the file before stash
- Omitting unstash command in later stage
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.Final Answer:
stash name: 'allFiles', includes: '**/*.log, **/*.txt' ... unstash 'allFiles' -> Option BQuick Check:
Use comma-separated globs with '**/' for multiple file types [OK]
- Using spaces instead of commas to separate globs
- Missing recursive '**/' causing files to be skipped
- Incorrect glob syntax causing no files to stash
Ready to prove it?
Pass the test and this pattern moves unknown
