2

Am trying to monitor jenkins builds for a deployment job but i need to get only the notifications only when the build fails 3 times in row.

Any ideas how to implement this ?

melfors
  • 21
  • 1
  • 3

1 Answers1

3

Yes, this is possible with a Pipeline script.

You can get a build's status with build.result.

You can get a list of the last 3 builds with Jenkins.instance.getItemByFullName('full/path/to/job').builds[0..2] (note this will return a list with fewer than 3 builds if the job hasn't been built 3 times yet).

Putting this together, we can make a simple script to check if the last three jobs did not succeed:

def builds = Jenkins.instance.getItemByFullName('full/path/to/job').builds[0..2]

def failedThreeTimes = builds.every { build-> build.result.toString() == 'FAILURE' }

echo("Failed three times in a row? ${failedThreeTimes}")

Note that you will need to disable the sandbox or whitelist a few methods in order to use this script. I'm not aware off the top of my head of any way to do this without whitelisting or disabling the sandbox.

jayhendren
  • 1,014
  • 5
  • 12