Search code examples
jenkinsjenkins-plugins

emailext - should send email only if html report is available else should not email


My goal is -- Very often emailext sends emails with error messages like

ERROR: File 'path/to/index.html' does not exist

My code looks like this

emailext(to: 'jane.doe@foo.com',
        subject: 'Build report',
        mimeType: 'text/html',
        body: '${FILE,path="path/to/index.html"}',
)

This error message is correct. There was no html report produced. But I want this email to be sent out only if there is an html report and to not send error messages when there is no html report.

Any ideas on how to achieve this behavior?

Thanks much!


Solution

  • You can use groovy's File.exists method to check if given file exists or not, and run the script accordingly.

    def file = new File( 'path/to/index.html' )
    
    // If it exists
    if( file.exists() ) {
      emailext(to: 'jane.doe@foo.com',
            subject: 'Build report',
            mimeType: 'text/html',
            body: '${FILE,path="path/to/index.html"}',
      )
    }