Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip stage in jenkins if my build is getting failed and want directly jump on destroy stage?(Unresolved) (Important)

I want to skip stage in jenkins if my build is getting failed or time exceeds and want directly jump on destroy stage I Tried

stage('Build') {
        options {                                                                                                                                                                   
            timeout(time: 10, unit: 'MINUTES')
        }
        steps {
            // Build steps
        }
    }
    // Other stages
}

} but i am unable to destroy my build, the build gets aborted also tried this :

pipeline {
agent any
stages {
    stage('Stage 1') {
        steps {
            // Add your stage 1 steps here
        }
    }
    stage('Stage 2') {
        steps {
            // Add your stage 2 steps here
        }
    }
    // Repeat for stages 3 to 8
    stage('Last Stage') {
        steps {
            // Add your last stage steps here
        }
    }
}
post {
    always {
        script {
            if (currentBuild.result == 'FAILURE') {
                stage('Last Stage') {
                    // Add your last stage steps here
                }
            }
        }
    }
}

But this also didn't worked for me

not able to jump on destroy stage if getting error r timeout not able to jump on destroy stage if getting error r timeout

what exactly i want is - This I want

like image 983
ADITYA GUPTA Avatar asked Jan 18 '26 00:01

ADITYA GUPTA


2 Answers

You can try this :

} catch (error) {
    println error
    failure(message: "Timeout reached.")
}
like image 159
Anshika Gupta Avatar answered Jan 21 '26 01:01

Anshika Gupta


You can add a post failure stage that destroys if that stage is reached:

post {
    failure {
        // destroy steps
    }
}

If your build fails then it must reach this step. However if it aborts due to timeout it will not come here. You will instead have to catch the timeout error and convert it to a failure by doing this:

} catch (error) {
    println error
    failure(message: "Timeout reached.")
}

Look at this question and its answer to see how you can do that.

You could also put your destroy logic in a function and call that function in both failure and aborted cases:

def destroy () {
    ...
}

...

post {
    failure {
        destroy()
    }
    aborted {
        destroy()
    }
}
like image 45
M B Avatar answered Jan 21 '26 00:01

M B