emailext - 仅当 html 报告可用时才应发送电子邮件,否则不应发送电子邮件

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

我的目标是 -- emailext 经常发送带有错误消息的电子邮件,例如

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

我的代码是这样的

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

这个错误信息是正确的。没有生成 html 报告。但我希望只有在有 html 报告时才发送这封电子邮件,而当没有 html 报告时 发送错误消息。

关于如何实现此行为的任何想法?

非常感谢!

您可以使用 groovy 的 File.exists 方法来检查给定文件是否存在,并 运行 相应的脚本。

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"}',
  )
}