Jenkins job to catch a particular error message from downstream job and retrigger it

Viewed 32

I am trying to retrigger a downstream job if the job fails during the first build with the error "invalid JWT token", I want this job to retrigger again with changed parameters.

I am able to retrigger it with different parameters as of now, but what I want to achieve here is want the job to retrigger only if I get the error as " invalid Jwt token" only.

can someone help me with this, I am trying to make use of try-catch block

this is the pipeline job as of now

enter image description here

1 Answers

I assume you decide on the error by looking at the log of the second Job. If that's the case have a look at the following. Here I'm using propagate: false

pipeline {
    agent any
    stages {
        stage('Job') {
            steps {
                script {
                    def jobBuild = build(job: 'SecondJob', wait: true,  propagate: false)
                    def result = jobBuild.getResult()
                    if(result == "FAILURE"){
                        def log = jobBuild.getRawBuild().getLog()
                        if(log.contains("invalid JWT token")){
                            echo "Rerunning the JOB!!!!"
                        } else {
                            error "Downstream Job failed due to other error." 
                        }
                    } 
                }
            }
        }
    }
}
Related