Bird
0
0

In a Jenkins declarative pipeline, how do you ensure the 'Deploy' stage runs only if the 'Test' stage completes successfully?

hard📝 Workflow Q8 of 15
Spring Boot - Docker and Deployment
In a Jenkins declarative pipeline, how do you ensure the 'Deploy' stage runs only if the 'Test' stage completes successfully?
Astage('Deploy') { steps { echo 'Deploying...' } post { failure { echo 'Skipping deploy' } } }
Bstage('Deploy') { when { expression { currentBuild.currentResult == 'SUCCESS' } } steps { echo 'Deploying...' } }
Cstage('Deploy') { steps { echo 'Deploying...' } }
Dstage('Deploy') { when { branch 'main' } steps { echo 'Deploying...' } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional execution

    Use 'when' directive to conditionally run stages.
  2. Step 2: Check for success condition

    Checking currentBuild.currentResult == 'SUCCESS' ensures deploy runs only if prior stages succeeded.
  3. Final Answer:

    stage('Deploy') { when { expression { currentBuild.currentResult == 'SUCCESS' } } steps { echo 'Deploying...' } } -> Option B
  4. Quick Check:

    Use 'when' with success condition for deploy [OK]
Quick Trick: Use 'when' with success check to conditionally run stages [OK]
Common Mistakes:
  • Not using 'when' for conditional stages
  • Checking branch instead of build result
  • Using post failure block incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes