5. You want to run two build steps in a Jenkins pipeline: first compile code, then run tests only if compile succeeds. Which pipeline snippet correctly implements this?
hard
A. stage('Build') {
steps {
sh 'compile.sh'
script {
if (currentBuild.result != 'FAILURE') {
sh 'test.sh'
}
}
}
}
B. stage('Build') {
steps {
sh 'compile.sh || true'
sh 'test.sh'
}
}
C. stage('Build') {
steps {
sh 'compile.sh'
}
}
stage('Test') {
steps {
sh 'test.sh'
}
}
Step 1: Understand conditional execution in Jenkins pipeline
To run tests only if compile succeeds, you need to check the build status before running tests.
Step 2: Analyze options
stage('Build') {
steps {
sh 'compile.sh'
script {
if (currentBuild.result != 'FAILURE') {
sh 'test.sh'
}
}
}
} uses a script block to check if the build is not failed before running tests, ensuring tests run only after successful compile.
Final Answer:
sh 'compile.sh'; script { if (currentBuild.result != 'FAILURE') { sh 'test.sh' } } -> Option A
Quick Check:
Conditional test run uses script and build status check [OK]
Hint: Use script block to conditionally run steps [OK]
Common Mistakes:
Running all steps unconditionally
Using shell || true which ignores compile failure
Splitting stages without controlling flow
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.