In Jenkins, I need a groovy script to cancel sending email in Email-Ext plugin by below condition.
If status.html
file is not available in Jenkins workspace, then I need to cancel sending email.
In Jenkins, I need a groovy script to cancel sending email in Email-Ext plugin by below condition.
If status.html
file is not available in Jenkins workspace, then I need to cancel sending email.
In response to holibob's suggestion:
In the case where the node is local, channel needs to be instanced
if(build.workspace.isRemote())
{
channel = build.workspace.channel;
}
else
{
channel = null;
}
I believe it is missing the actual existence check, otherwise the constructor always returns a valid instance even when the file doesn't exist:
if ((fp != null) && fp.exists())
{
cancel = false;
}
Finally, in the case of needing a wildcard in the path, I'm afraid exists() doesn't work, but the following does:
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import javax.mail.internet.*
import javax.mail.*
import javax.activation.*
def workspace = build.getEnvVars()["WORKSPACE"]
listener.logger.println('WS: ' + workspace)
if(build.workspace.isRemote())
{
channel = build.workspace.channel;
}
else
{
channel = null
}
fp = new hudson.FilePath(channel, build.workspace.toString() )
if ((fp != null) && fp.list("**/path/to/*.html").size()>0)
{
listener.logger.println('file found')
cancel = false
}
else
{
// file not found
listener.logger.println('file not found, cancelling email')
cancel = true // disable email
}
Groovy is the way to go, and simply see if status.html is present in WORKSPACE. The Email-ext pre-send script option in the plugin provides predefined variables including 'cancel - a boolean, which when set to true will cancel the sending of the email'
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import javax.mail.internet.*;
import javax.mail.*
import javax.activation.*
def workspace = manager.build.getEnvVars()["WORKSPACE"]
manager.listener.logger.println('WS: ' + workspace)
if(manager.build.workspace.isRemote())
{
channel = manager.build.workspace.channel;
}
fp = new hudson.FilePath(channel, manager.build.workspace.toString() + "/status.html")
if(fp != null)
{
manager.listener.logger.println('status.html found')
cancel = false
}
else
{
// file not found
cancel = true // disable email
}
An more expansive example of the use of cancel can be seen here 'Disable email notifications using Jenkins job parameter'
If you are open for other ideas, here we go
We can use email triggers for this condition, we can explicitly fail the job when status.html file is not found, and then we can configure email triggers only on success condition.
If you do not want to play to job status, then we can create a new Jenkins job for email triggering and from your original job use conditional build step and check for file existence, accordingly you can either trigger the downstream job for emailing or ignore calling child [email job].
You can check this answer in stackoverflow.