0
0
Jenkinsdevops~10 mins

Stage conditions with when directive in Jenkins - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run the stage only on the 'main' branch.

Jenkins
stage('Build') {
  when {
    branch [1]
  }
  steps {
    echo 'Building...'
  }
}
Drag options to blanks, or click blank then click option'
A'main'
B'develop'
C'feature'
D'release'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a branch name other than 'main'.
Omitting quotes around the branch name.
2fill in blank
medium

Complete the code to run the stage only when the environment variable 'DEPLOY' is set to 'true'.

Jenkins
stage('Deploy') {
  when {
    environment name: 'DEPLOY', value: [1]
  }
  steps {
    echo 'Deploying...'
  }
}
Drag options to blanks, or click blank then click option'
A'no'
B'false'
C'yes'
D'true'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' or other values instead of 'true'.
Not quoting the value string.
3fill in blank
hard

Fix the error in the when condition to run the stage only if the branch is 'feature'.

Jenkins
stage('Test') {
  when {
    branch [1]
  }
  steps {
    echo 'Testing...'
  }
}
Drag options to blanks, or click blank then click option'
Afeature'
B'feature'
C"feature"
Dfeature
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the branch name.
Using mismatched or incomplete quotes.
4fill in blank
hard

Fill both blanks to run the stage only when the branch is 'release' and the environment variable 'RUN_TESTS' is 'yes'.

Jenkins
stage('Release') {
  when {
    allOf {
      branch [1]
      environment name: 'RUN_TESTS', value: [2]
    }
  }
  steps {
    echo 'Releasing with tests...'
  }
}
Drag options to blanks, or click blank then click option'
A'release'
B'yes'
C'no'
D'develop'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong branch or environment values.
Not quoting the values.
5fill in blank
hard

Fill all three blanks to run the stage only when the branch is 'hotfix', the environment variable 'DEPLOY' is 'true', and the build cause is 'SCMTrigger'.

Jenkins
stage('Hotfix Deploy') {
  when {
    allOf {
      branch [1]
      environment name: 'DEPLOY', value: [2]
      triggeredBy '[3]'
    }
  }
  steps {
    echo 'Deploying hotfix...'
  }
}
Drag options to blanks, or click blank then click option'
A'hotfix'
B'true'
CSCMTrigger
D'ManualTrigger'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting branch or environment values.
Using wrong trigger cause name.