0
0
Jenkinsdevops~20 mins

Stage conditions with when directive in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stage Conditions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a stage with a simple when condition
Given the following Jenkins pipeline snippet, what will be the output if the branch name is 'feature-xyz'?
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      when {
        branch 'main'
      }
      steps {
        echo 'Building on main branch'
      }
    }
  }
}
AThe stage 'Build' will be skipped and no output from this stage
BThe stage 'Build' will run and print 'Building on main branch'
CPipeline will fail with a syntax error
DThe stage 'Build' will run but print nothing
Attempts:
2 left
💡 Hint
Check the branch name condition in the when directive.
🧠 Conceptual
intermediate
2:00remaining
Understanding multiple conditions in when directive
Which of the following when conditions will cause the stage to run only if the branch is 'develop' and the build is triggered by a user?
Awhen { branch 'develop' && triggeredBy 'UserIdCause' }
Bwhen { allOf { branch 'develop'; triggeredBy 'UserIdCause' } }
Cwhen { anyOf { branch 'develop'; triggeredBy 'UserIdCause' } }
Dwhen { branch 'develop' || triggeredBy 'UserIdCause' }
Attempts:
2 left
💡 Hint
Look for the condition that requires both checks to be true.
Troubleshoot
advanced
2:00remaining
Why does this when condition cause a syntax error?
Identify the error in this when directive snippet: when { branch == 'main' }
Jenkins
when {
  branch == 'main'
}
AThe when block must be empty
BMissing quotes around 'main'
CThe branch condition requires parentheses
DUsing '==' inside when block is invalid syntax
Attempts:
2 left
💡 Hint
Check the correct syntax for branch condition in when directive.
🔀 Workflow
advanced
2:00remaining
Order of evaluation in multiple when conditions
Given this when directive, in what order are the conditions evaluated? when { allOf { branch 'release' environment name: 'DEPLOY_ENV', value: 'prod' } }
Abranch condition is checked first, then environment
Benvironment condition is checked first, then branch
CBoth conditions are checked simultaneously
DOnly the branch condition is checked
Attempts:
2 left
💡 Hint
Think about how allOf logically works in Jenkins when directive.
Best Practice
expert
2:00remaining
Choosing the best when condition for skipping stages on pull requests
You want to skip a stage when the build is triggered by a pull request. Which when condition is the best choice?
Awhen { branch 'PR-*' }
Bwhen { expression { return env.CHANGE_ID == null } }
Cwhen { not { changeRequest() } }
Dwhen { triggeredBy 'PullRequestCause' }
Attempts:
2 left
💡 Hint
Consider the official Jenkins way to detect pull requests.