【问题标题】:Jenkins Pipeline Groovy Script: Use `mail` in JenkinsfileJenkins Pipeline Groovy 脚本:在 Jenkinsfile 中使用“mail”
【发布时间】:2018-06-13 15:20:08
【问题描述】:

当基于 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中配置了你的smtp服务器地址吗?如果是,那么您应该检查是否可以使用 groovy shell 从机器访问地址。
  • 它在“电子邮件通知”部分以及我另外安装的“扩展电子邮件通知”中配置正确

标签: jenkins groovy continuous-integration jenkins-pipeline continuous-delivery


【解决方案1】:

我能够通过在 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: '...'

【讨论】:

    猜你喜欢
    • 2016-12-25
    • 2022-09-29
    • 2017-11-23
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多