0
0
Jenkinsdevops~5 mins

Stage conditions with when directive in Jenkins - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the when directive in a Jenkins pipeline stage?
The when directive controls whether a stage runs or is skipped based on specified conditions, helping to make pipelines more flexible and efficient.
Click to reveal answer
beginner
Name two common conditions you can use inside the when directive.
You can use conditions like branch to run a stage only on specific branches, and environment to run a stage only if an environment variable matches a value.
Click to reveal answer
intermediate
How does the when directive improve pipeline efficiency?
By skipping stages that are not needed based on conditions, it saves time and resources, similar to deciding not to cook a meal if you already have leftovers.
Click to reveal answer
beginner
What happens if the when condition in a stage evaluates to false?
The stage is skipped and does not run, but the pipeline continues with the next stages.
Click to reveal answer
beginner
Write a simple example of a Jenkins stage that runs only on the 'main' branch using the when directive.
stage('Deploy') { when { branch 'main' } steps { echo 'Deploying to production' } }
Click to reveal answer
What does the when directive do in a Jenkins pipeline stage?
AControls if the stage runs based on conditions
BDefines the steps inside a stage
CSets environment variables
DTriggers another pipeline
Which condition would you use to run a stage only on a specific branch?
Aenvironment
Bchangeset
Cexpression
Dbranch
If a when condition is false, what happens to the stage?
APipeline stops
BIt runs anyway
CIt skips the stage
DPipeline restarts
Which of these is NOT a valid when condition in Jenkins?
Abranch
Btime
Cenvironment
Dexpression
How can you combine multiple conditions in a when directive?
AUsing <code>allOf</code> or <code>anyOf</code>
BUsing <code>and</code> or <code>or</code> keywords
CUsing <code>if</code> statements
DYou cannot combine conditions
Explain how the when directive helps control the flow of a Jenkins pipeline.
Think about how you decide to do or skip tasks based on conditions in daily life.
You got /4 concepts.
    Describe how you would use multiple conditions inside a when directive to run a stage only on the 'main' branch and when an environment variable is set to 'production'.
    Combine conditions using <code>allOf</code> to require both to be true.
    You got /4 concepts.