2

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.

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Is there a solution which can be used in declarative jenkins file? I am not sure how to use cancel in below answers. Did you ever get an answer for your question? – adev Apr 04 '19 at 01:06

3 Answers3

2

In response to holibob's suggestion:

  1. In the case where the node is local, channel needs to be instanced

    if(build.workspace.isRemote())
    {
        channel = build.workspace.channel;
    }
    else 
    {
        channel = null;
    }
    
  2. 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
}
Vini Bono
  • 85
  • 8
1

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'

holibob
  • 51
  • 2
0

If you are open for other ideas, here we go

  1. 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.

  2. 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].

  3. You can check this answer in stackoverflow.

doelleri
  • 19,232
  • 5
  • 61
  • 65
prudviraj
  • 3,634
  • 2
  • 16
  • 22