I want to disable a job if it fails three or more times. I found this solution to disable it immediately. But I want to wait three times.
Asked
Active
Viewed 2,108 times
1 Answers
5
You can get the previous result from build.previousBuild
. For example:
def failingForAtLeast(build, n) {
def failed = build.result.isWorseThan(hudson.model.Result.SUCCESS );
if( n <= 1 ) {
return failed;
} else if ( failed && build.previousBuild != null) {
return failingForAtLeast(build.previousBuild, n-1);
} else {
return false;
}
}
if ( failingForAtLeast(manager.build, 3) ) {
manager.build.project.disabled = true
}

Piotr Praszmo
- 17,928
- 1
- 57
- 65
-
Looks very good, I test it and accept if it is working. – jan Jul 16 '14 at 16:32
-
1With `def failed = build.result.isWorseOrEqualTo(hudson.model.Result.FAILURE);` it's working as I wanted. Thanks! – jan Jul 18 '14 at 06:29
-
1You forgot to mention that you need the Groovy PostBuild plugin – Cas Dec 12 '17 at 21:32