1

I'm trying to send an email when a Pipeline build on Jenkins fails. An example can be found here: https://github.com/jenkinsci/pipeline-examples/blob/master/jenkinsfile-examples/nodejs-build-test-deploy-docker-notify/Jenkinsfile

My concrete groovy script looks as follows:

#!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
      }
  }

}

Actually, nothing ever happens, although the exception is being caught correctly and the build fails. Is there anything required to be able to use mail?! Maybe another Jenkins plugin or something?

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • Did you configure address of your smtp server in Jenkins? If yes, then you should check if address is reachable from machine using groovy shell. – Krzysztof Atłasik Sep 14 '17 at 07:43
  • it's configured correctly in the 'Email Notifications' section and also in 'Extended E-mail Notification', which I installed additionally – s1m0nw1 Sep 14 '17 at 09:10

1 Answers1

1

I was able to fix it myself by using emailext plugin on Jenkins instead of mail. The code looks as follows now:

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: '...'
s1m0nw1
  • 76,759
  • 17
  • 167
  • 196