Answers: <p>There is a retry parameter in the <a href="https://jenkins.io/doc/book/pipeline/syntax/#options" rel="nofollow noreferrer">global options</a> when using declarative pipeline :</p>
  
  <pre><code>pipeline {
      agent any
      options {
          retry(3)
      }
      stages {
          ...
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-11T08:37:21.213Z
Answers: 2019-06-11T08:37:21.213Z
Answers: Wognin
Answers: <p>The other answer is incorrect.  There is indeed a builtin to retry arbitrary sections of your job called <a href="https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-times" rel="noreferrer"><code>retry</code></a>.</p>
  
  <p>If you want to retry the whole job three times, you can wrap your entire job in a <code>retry</code> block:</p>
  
  <pre><code>retry(count: 3) {
    // your job definition here
  }
  </code></pre>
  
  <p>However, if it's safe to do so, I would recommend wrapping individual steps or stages instead:</p>
  
  <pre><code>stage('my first stage') {
    retry(count: 3) {
      sh('some-command')
      sh('some-other-command')
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-10-29T15:49:47.623Z
Answers: thanks jayhendren, this is what I was looking for. Much appreciated.
Answers: Ron Bob
Answers: CC BY-SA 4.0
Answers: 2019-03-11T21:29:15.350Z
Answers: @RonBob perhaps you could accept the answer
Answers: 030
Answers: CC BY-SA 4.0
Answers: 2021-04-15T18:49:37.187Z
Answers: When I put a `retry` at the top of a `stage` block I get "Unknown stage section "retry"." error when validating.
Answers: jcollum
Answers: CC BY-SA 4.0
Answers: 2018-10-23T22:34:00.000Z
Answers: 2018-10-23T22:34:00.000Z
Answers: jayhendren
Answers: <p>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: <a href="https://stackoverflow.com/a/46852240/1019835">https://stackoverflow.com/a/46852240/1019835</a></p>
  
  <p>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: <a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">https://jenkins.io/doc/book/pipeline/shared-libraries/</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2018-10-13T12:01:18.330Z
Answers: 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?
Answers: Ron Bob
Answers: CC BY-SA 4.0
Answers: 2018-10-12T17:44:15.233Z
Answers: 2018-10-12T17:44:15.233Z
Answers: Anrich
Body: <p>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:</p>
  
  <pre><code>post { 
      failure{ 
          retrigger //try for 3 times atleast
      }
  } 
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2022-07-23T09:07:52.837Z
Comments: Maybe try [retry](https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-times) (conditions:true)?
  I can think of plenty of reasons why Jenkins might not want to support unlimited retries!
Comments: wajiii
Tags: <jenkins-pipeline><jenkinsfile>
Title: How to retrigger a Jenkins build after failure using declarative syntax?
UserDisplayName: Ron Bob
ThreadText: # How to retrigger a Jenkins build after failure using declarative syntax?
  
  (Asked by: username_0 on 2018-10-12)
  
  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
      }
  } 
  
  ```
  
  **Tags:** jenkins-pipeline, jenkinsfile
  
  > **Comments:**
  > **username_1**: Maybe try [retry](https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-times) (conditions:true)? I can think of plenty of reasons why Jenkins might not want to support unlimited retries!  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2018-10-23. Score: 9
  
  The other answer is incorrect. There is indeed a builtin to retry arbitrary sections of your job called [`retry`](https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#retry-retry-the-body-up-to-n-times).
  
  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')
    }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: thanks username_3, this is what I was looking for. Much appreciated.  
  > **username_4**: @username_0 perhaps you could accept the answer  
  > **username_5**: When I put a `retry` at the top of a `stage` block I get "Unknown stage section "retry"." error when validating.  
  
  ---
  
  ### Answer by username_2 on 2019-06-11. Score: 3
  
  There is a retry parameter in the [global options](https://jenkins.io/doc/book/pipeline/syntax/#options) when using declarative pipeline :
  
  ```
  pipeline {
      agent any
      options {
          retry(3)
      }
      stages {
          ...
      }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_6 on 2018-10-12. Score: -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/>
  
  > **Comments:**
  > **username_0**: 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?  
  
  ---
  
  