Prevent Email Trigger Using Groovy Script

Issue

You need to prevent emails from being sent based on specific output in a build’s console output.

Environment

Resolution

  • Click Advanced Settings in the Editable Email Notification section in a job’s configuration

  • Remove the Failue - Any Trigger

  • Add a new Script - After Build Trigger

  • Add a groovy script that will only send an email if the build result is failure AND it meets your requirements.

  • The example groovy script below prevents emails when a string parameter is invalid via the
    Validating String Parameter Plugin.

Example groovy script

// only send an email if build ends in FAILURE AND the validating string parameter plugin phrase
// is not found {{ERROR: Invalid value for parameter [version] specified:}} is NOT found in build 
// logs

def invalidParameter

invalidParameter = build.logFile.text.readLines().any{ it ==~ /.*Invalid value for parameter.*/} 

invalidParameter == false && build.result.toString().equals('FAILURE')

Please review the Triggers section on the email-ext plugin wiki page and the email-ext recipes wiki page for more examples.

References

Have more questions?

1 Comments

  • 0
    Avatar
    Harish Rai

    In above example How I can use invalidParameter  in mSubject line to show failure .

    I tried to use it $invalidParameter but It is not showing failure message .

     

    Any idea how to add errors in my subject or Default content from build log ?

Please sign in to leave a comment.