Saravanan Jothilingam
Saravanan Jothilingam

Reputation: 99

Jenkins - set timeout to Stage and continue with next stage

I have a stage which runs one shell script at remote node. If the script takes very long time to execute, the stage should wait for sometime and should move to next stage without aborting the subsequent stages.

Could you please let me know the required syntax to achieve this.

Upvotes: 1

Views: 5784

Answers (1)

yorammi
yorammi

Reputation: 6458

in a declarative pipeline, add this stage:

stage ("do-and-skip-for-timeout") {
   steps {
      script {
        try {
          timeout (time: 10, unit: 'MINUTES') {
             echo "do something here, that can take some time" // replace this line with your code
          }
        }
        catch (error) {
           def user = error.getCauses()[0].getUser()
           if('SYSTEM' == user.toString()) { // SYSTEM means timeout.
             echo "Timeout reached, continue to next stage"
           } 
           else {
             throw new Exception("[ERROR] stage failed!")
           }
        }
   }
}

Upvotes: 10

Related Questions