I'm having an issue where I want to cancel sending an email when a job gets into the FIXED state from the UNSTABLE state. So if a job was failing but got fixed, I want to send the message. But if a job is unstable (tests not passing) and gets fixed, don't send the email. It might be that a job is marked UNSTABLE after it has been in FAILURE state. When it goes to SUCCESS (i.e. it is fixed) I want an email in that case. You can see the cases in the code below.
My problem is that when I set the variable cancel
to true
by definition[1] it should cancel the email, but it doesn't. The email gets sent every time. Of course I'm using triggers for "Failure", "Still failing" and "Fixed".
Jenkins version: 1.533. Email-ext version: 2.37.2.2
// The goal of this script is to block sending the 'FIXED' message
// when the status goes from 'UNSTABLE' to 'SUCCESS'
//
// These are the cases (where F=FAILURE, U=UNSTABLE, S=SUCCESS)
// S - S : no msg (previous state: S, current state: S)
// F - S : msg
// S - U ... U - S : no msg <-- this is the one we need to avoid sending an email
// F - U ... U - S : msg
logger.println("Entering pre-send script")
// variable definitions
def keepGoing= true
def cancelEmail = false
// object to current job
job = hudson.model.Hudson.instance.getItem("incr-build-master")
// current build number
buildNumber = build.getNumber()
logger.println("Current build number: " + buildNumber)
// if the build failed or is unstable don't to anything,
// the specific triggers should take care of the messages
if (build.result.toString().equals("SUCCESS"))
{
logger.println("Build is successful. Procesing...")
while( keepGoing )
{
// get the number of the next past build
pastBuild = job.getBuildByNumber(--buildNumber)
buildResult = pastBuild.result.toString()
switch ( buildResult )
{
case "SUCCESS":
// if the previous non-unstable build was successful
// don't send a 'FIXED' message
cancelEmail = true
keepGoing = false
logger.println("Cancel sending email")
break
case "FAILURE":
// here we exit, but we will send the 'FIXED' message
keepGoing = false
logger.println("Send email")
break
case "UNSTABLE":
// let us keep looking until we find a previous build
// that is either 'SUCCESS' or 'FAILURE*
logger.println("Build " + buildNumber + " is unstable")
break
default:
logger.println("Error in script: result string is wrong - " + buildResult)
return
}
}
}
logger.println("Emailed canceled?: " + cancelEmail)
cancel=cancelEmail
logger.println("Exiting pre-send script")
[1] from the Help icon: "You may also cancel sending the email by setting the boolean variable "cancel" to true."