Bird
Raised Fist0
Jenkinsdevops~20 mins

Stash and unstash for passing data in Jenkins - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stash and Unstash Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Jenkins pipeline snippet?
Consider this Jenkins pipeline stage snippet using stash and unstash:
stage('Build') {
  steps {
    writeFile file: 'app.txt', text: 'version 1.0'
    stash name: 'appFiles', includes: 'app.txt'
  }
}
stage('Test') {
  steps {
    unstash 'appFiles'
    script {
      def content = readFile('app.txt')
      echo content
    }
  }
}

What will be printed in the Test stage?
AError: No such file 'app.txt'
Bversion 1.0
CEmpty output
Dversion 2.0
Attempts:
2 left
💡 Hint
Remember that stash saves files from one stage and unstash restores them in another.
🧠 Conceptual
intermediate
1:30remaining
Why use stash and unstash in Jenkins pipelines?
Which of the following best explains the purpose of stash and unstash in Jenkins pipelines?
ATo pass files between different stages or nodes during a pipeline run
BTo permanently save files to the Jenkins master workspace
CTo create backups of the entire Jenkins workspace
DTo deploy files directly to production servers
Attempts:
2 left
💡 Hint
Think about how pipeline stages might run on different machines or agents.
❓ Troubleshoot
advanced
2:00remaining
Why does unstash fail with 'No such stash' error?
You have this Jenkins pipeline snippet:
stage('Build') {
  steps {
    writeFile file: 'output.log', text: 'log data'
    stash name: 'logs', includes: 'output.log'
  }
}
stage('Deploy') {
  steps {
    unstash 'logfiles'
  }
}

When running, the Deploy stage fails with:
No such stash 'logfiles'
What is the most likely cause?
AThe Deploy stage runs on a different node that cannot access the stash
BThe file 'output.log' was not created before stashing
CThe stash command must be inside the same stage as unstash
DThe stash name used in unstash does not match the stash name used in stash
Attempts:
2 left
💡 Hint
Check the exact names used in stash and unstash commands.
🔀 Workflow
advanced
1:30remaining
Identify the correct sequence to share files between stages using stash/unstash
You want to share a file created in the 'Compile' stage with the 'Test' stage in a Jenkins pipeline. Which sequence of steps is correct?
ACompile stage: unstash file; Test stage: create file, stash it
BCompile stage: create file; Test stage: read file without unstash
CCompile stage: create file, stash it; Test stage: unstash file, read it
DCompile stage: create file; Test stage: stash file, then unstash it
Attempts:
2 left
💡 Hint
Remember stash saves files and unstash restores them in later stages.
✅ Best Practice
expert
2:30remaining
What is the best practice when using stash to pass large files between Jenkins pipeline stages?
When passing large files between stages using stash and unstash, which practice helps avoid performance issues?
AOnly stash the necessary files using the 'includes' pattern to minimize data size
BAlways stash the entire workspace to ensure all files are available
CUse unstash multiple times in the same stage to improve speed
DAvoid using stash and unstash; copy files manually between nodes
Attempts:
2 left
💡 Hint
Think about how much data is transferred and stored temporarily.

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

  1. Step 1: Understand the role of stash

    The stash command saves files temporarily during a pipeline run.
  2. Step 2: Understand the role of unstash

    The unstash command restores those saved files in later stages or nodes.
  3. Final Answer:

    To temporarily save and later restore files between pipeline stages -> Option A
  4. 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

  1. Step 1: Recall stash syntax

    The correct stash syntax uses named parameters: stash name: '...', includes: '...'.
  2. Step 2: Match options to syntax

    stash name: 'logs', includes: 'output.log' matches the correct syntax exactly.
  3. Final Answer:

    stash name: 'logs', includes: 'output.log' -> Option D
  4. Quick Check:

    stash uses named params: name and includes [OK]
Hint: Use named parameters: name and includes for stash [OK]
Common Mistakes:
  • Using parentheses like a function call
  • Using incorrect parameter names like files
  • Using assignment (=) instead of colon (:)
3. Given this Jenkins pipeline snippet:
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?
medium
A. Hello
B. No such file or directory
C. Empty output
D. Error: stash not found

Solution

  1. Step 1: Understand stash saves message.txt

    The Build stage creates message.txt with content 'Hello' and stashes it as 'msg'.
  2. Step 2: Unstash restores message.txt in Test stage

    The Test stage unstashes 'msg', restoring message.txt, then runs cat message.txt.
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    unstash restores files, so cat shows 'Hello' [OK]
Hint: Unstash restores files exactly as stashed [OK]
Common Mistakes:
  • Assuming unstash does not restore files
  • Expecting an error because stages run separately
  • Thinking stash deletes files
4. You wrote this Jenkins pipeline snippet:
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?
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

  1. Step 1: Check stash and unstash names

    The stash is named 'data' but unstash uses 'artifacts', so names do not match.
  2. 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.
  3. Step 3: Identify the cause

    Mismatched names cause unstash to fail silently, so file.txt is not available in Deploy workspace.
  4. Final Answer:

    The stash name used in unstash does not match the stash name -> Option C
  5. 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

  1. Step 1: Understand includes pattern syntax

    Jenkins stash supports comma-separated glob patterns with '**/' for recursive matching.
  2. 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.
  3. Final Answer:

    stash name: 'allFiles', includes: '**/*.log, **/*.txt' ... unstash 'allFiles' -> Option B
  4. Quick Check:

    Use comma-separated globs with '**/' for multiple file types [OK]
Hint: Use comma-separated globs with '**/' for multiple files [OK]
Common Mistakes:
  • Using spaces instead of commas to separate globs
  • Missing recursive '**/' causing files to be skipped
  • Incorrect glob syntax causing no files to stash
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard