HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to retrigger a Jenkins build after failure using declarative syntax?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
afterdeclarativejenkinssyntaxusinghowretriggerbuildfailure

Problem

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
    }
}

Solution

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')
  }
}

Code Snippets

retry(count: 3) {
  // your job definition here
}
stage('my first stage') {
  retry(count: 3) {
    sh('some-command')
    sh('some-other-command')
  }
}

Context

StackExchange DevOps Q#5173, answer score: 9

Revisions (0)

No revisions yet.