def call(Map params = [:], Closure inner) { catchError { inner.call() } def notifyCulprits = params.getOrDefault('notifyCulprits', true) def extraMails = params.getOrDefault('extraMails', []) if (shouldSendMail(currentBuild)) { def providers = [] if (notifyCulprits) { providers.add(culprits()) } emailext subject: 'Jenkins - $PROJECT_NAME - Build #$BUILD_NUMBER - $BUILD_STATUS', body: '${SCRIPT, template="groovy-html.template"}', mimeType: 'text/html', recipientProviders: providers, to: extraMails.join(',') } } /** * Send email when either the current build is failed or the previous build has failed (so we send the * "the build is back to normal" mails) * * @param currentBuild The current build, as defined by the pipeline * @return true if a mail should be send, false otherwise */ @NonCPS def shouldSendMail(currentBuild) { def previousBuild = currentBuild.previousBuild return currentBuild.resultIsWorseOrEqualTo("UNSTABLE") || (previousBuild != null && previousBuild.resultIsWorseOrEqualTo("UNSTABLE")) }