2017-05-01 121 views
1

不發送我使用下面的步驟在我的管道詹金斯的工作:詹金斯管線電子郵件上構建失敗

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '[email protected]', sendToIndividuals: true]) 

但是,當構建失敗(即誤差)沒有電子郵件發送。任何指針爲什麼?

P.S.電子郵件可以從這個服務器發送,我已經測試過了。

+0

確實檢查了Jenkins主配置中的電子郵件設置,而不是作業 – Torsten

回答

0

您需要手動將生成結果設置爲失敗,並確保它在工作區中運行。例如:

try { 
    throw new Exception('fail!') 
} catch (all) { 
    currentBuild.result = "FAILURE" 
} finally { 
    node('master') { 
     step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '[email protected]', sendToIndividuals: true]) 
    } 
} 

該插件檢查currentBuild.result的狀態,這是不正常改變,直到腳本完成後。

1

使用聲明管道使用新的語法,例如:

pipeline { 
    agent any 
    stages { 
     stage('Test') { 
      steps { 
       sh 'echo "Fail!"; exit 1' 
      } 
     } 
    } 
    post { 
     always { 
      echo 'This will always run' 
     } 
     success { 
      echo 'This will run only if successful' 
     } 
     failure { 
      mail bcc: '', body: "<b>Example</b><br>\n\<br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "[email protected]"; 
     } 
     unstable { 
      echo 'This will run only if the run was marked as unstable' 
     } 
     changed { 
      echo 'This will run only if the state of the Pipeline has changed' 
      echo 'For example, if the Pipeline was previously failing but is now successful' 
     } 
    } 
} 

你可以找到在官方詹金斯網站的詳細信息:

https://jenkins.io/doc/pipeline/tour/running-multiple-steps/

注意,這個新的語法讓你的管道更可讀,邏輯和可維護。

+0

不幸的是,我正在使用Jenkins的舊版本(1.6.X)。 – saikosen

+0

請檢查我的答案在這裏:http://stackoverflow.com/questions/36948606/jenkins-notifying-error-occured-in-different-steps-by-sending-mail-in-pipeline我認爲你有相同的問題 –