gaurav sharma
gaurav sharma

Reputation: 133

How to trigger stage B only when stage A fails in jenkins pipeline

I have two stages in a jenkins Pipeline. Stage A and then Stage B. I would like to trigger stage B only if stage A fails. If stage A is successful then skip stage B. How can I achieve this one ?

Upvotes: 0

Views: 294

Answers (2)

yong
yong

Reputation: 13722

Try as below:

def stageA_Fail = false

pipeline {
 stages {
   stage('A') {
     steps {
       script {
         try {
            // put all steps of stage A in try
         }
         catch() {
           stageA_Fail = true
         }
       }
     }
   }
 
   stage('B') {
     when {expression {return stageA_Fail} }
     steps {}
   }
 }
}

Upvotes: 1

Pankaj Saini
Pankaj Saini

Reputation: 1648

You can use try and catch block, and put stage B in catch block. You need to catch the error/exception though

Upvotes: 0

Related Questions