Jenkins 管道 Groovy 脚本:在 Jenkinsfile 中使用 `mail`

Jenkins Pipeline Groovy Script: Use `mail` in Jenkinsfile

我正在尝试在 Jenkins 上的 Pipeline 构建失败时发送电子邮件。可以在此处找到示例:https://github.com/jenkinsci/pipeline-examples/blob/master/jenkinsfile-examples/nodejs-build-test-deploy-docker-notify/Jenkinsfile

我的具体 groovy 脚本如下所示:

#!groovy
node('') {
   def env = ["JAVA_HOME=${tool 'jdk1.8.0_131'}", "PATH+MAVEN=${tool 'maven_3.1.1'}/bin:${env.JAVA_HOME}/bin", "PATH+GRADLE=${tool 'gradle_4.1'}/bin:${env.JAVA_HOME}/bin" ]

   def err = null
   currentBuild.result = "SUCCESS"

  try {
   stage('errorStage') {
        dir('error') {
            git url: "unknown", branch: "master"
            withEnv(env) {
                sh "mvn -Pjenkins-build clean deploy"
            }
        }
    }
  } catch (caughtError) {
      println "caught error :" + caughtError
      err = caughtError
      currentBuild.result = "FAILURE"
      mail (body:
            "Pipeline error: ${err}\nFix me.",
            from: 'jenkins@x.com',
            subject: 'Pipeline build failed',
            to: 'recipient@x.com')
  } finally {
      /* Must re-throw exception to propagate error */
      if (err) {
          throw err
      }
  }

}

实际上,什么也没有发生,尽管异常被正确捕获并且构建失败。需要什么才能使用 mail 吗?!也许另一个 Jenkins 插件或什么的?

我可以通过在 Jenkins 上使用 emailext 插件而不是 mail 自行修复它。代码现在如下所示:

emailext body: "Pipeline error: ${err}\nPlease go to ${BUILD_URL} and verify the build",
                recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
                subject: "'${JOB_NAME}' (${BUILD_NUMBER}) failed",
                to: '...'