0
votes

I've a Jenkins pipeline with multiple stages but because of some issue, in one of the stages, it is likely to run longer unnecessarily.

Instead of aborting entire pipeline build and skip next stages, I want to kill that specific stage on which other stages are not dependent.

Is there a way to kill specific stage of Jenkins pipeline?

2
How exactly is this long-running step executed? In parallel or is it one in the series of steps running one by one?Tupteq
That stage is being executed in parallel with other stages.Alpha

2 Answers

1
votes

There are ways to skip a stage. But I'm not sure if there are options to kill a long running stage. I'd simply add a conditional expression to run the stage or not OR maybe you could put a timeout condition wrapped in a try..catch block for the long running unnecessary stage to skip and proceed to other stages you want like as below.

pipeline {
   agent any
   stages {
        stage('stage1') {
          steps {
            script {
              try {
                  timeout(time: 2, unit: 'NANOSECONDS')
                  echo "do your stuff"
              } catch (Exception e) {
                  echo "Ended the never ending stage and proceeding to the next stage"
              }
            }              
          }
        }     
        stage('stage2') {
          steps {
            script {
                echo "Hi Stage2"
            }
          }
        }           
    }
}

OR Check this page for conditional step/stage.

0
votes

You can try using "try/catch" block in the scripted pipeline. Even if there is error in a particular stage, Jenkins will continue to execute the next stage.

node {
    stage('Example') {
        try {
            sh 'exit 1'
        }
        catch (exc) {
            echo 'Something failed, I should sound the klaxons!'
            throw
        }
    }
}

You can refer documentation here: https://jenkins.io/doc/book/pipeline/syntax/