0

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!

Talha Junaid
  • 2,351
  • 20
  • 29
suprita shankar
  • 1,554
  • 2
  • 16
  • 47

2 Answers2

0

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"}',
  )
}
Talha Junaid
  • 2,351
  • 20
  • 29
  • I tried it and I get `scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method java.io.File exists` – suprita shankar Aug 29 '18 at 21:01
  • Try [Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject](https://stackoverflow.com/a/39412951/3358570) – Talha Junaid Aug 29 '18 at 21:03
0
  post {
    always {
        echo 'I will always say Hello again!'
        
        emailext body: '${FILE,path="report/report.html"}',
            mimeType: 'text/html',
            to: "example@example.com",
            subject: "Daily Automation Report"
    }
}
jacky
  • 21
  • 3