6

I am using declarative pipeline syntax. I want to re trigger my build for 3 times if it fails. I want to add this step in my pipeline may be in post step: Something like below:

post { 
    failure{ 
        retrigger //try for 3 times atleast
    }
} 
1
  • Maybe try retry (conditions:true)? I can think of plenty of reasons why Jenkins might not want to support unlimited retries!
    – wajiii
    Commented Jul 23, 2022 at 9:07

3 Answers 3

9

The other answer is incorrect. There is indeed a builtin to retry arbitrary sections of your job called retry.

If you want to retry the whole job three times, you can wrap your entire job in a retry block:

retry(count: 3) {
  // your job definition here
}

However, if it's safe to do so, I would recommend wrapping individual steps or stages instead:

stage('my first stage') {
  retry(count: 3) {
    sh('some-command')
    sh('some-other-command')
  }
}
3
  • thanks jayhendren, this is what I was looking for. Much appreciated.
    – Ron Bob
    Commented Oct 29, 2018 at 15:49
  • @RonBob perhaps you could accept the answer
    – 030
    Commented Mar 11, 2019 at 21:29
  • When I put a retry at the top of a stage block I get "Unknown stage section "retry"." error when validating.
    – jcollum
    Commented Apr 15, 2021 at 18:49
3

There is a retry parameter in the global options when using declarative pipeline :

pipeline {
    agent any
    options {
        retry(3)
    }
    stages {
        ...
    }
}
-1

There doesn't seem to be a built in pipeline step to do this, and neither does there seem to be a existing plugin that works with pipelines. I'd recommend having a look at the stackoverflow answer: https://stackoverflow.com/a/46852240/1019835

You can implement that code as a shared library, which would allow you to share your solution between projects. Have a look at this tutorial to do that: https://jenkins.io/doc/book/pipeline/shared-libraries/

1
  • Thanks! Really appreciate. What if I just want to retrigger the job upon build failure in post step. No matter how much number of time?
    – Ron Bob
    Commented Oct 13, 2018 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.