Note: Jenkins中配置的郵箱secret須要受權碼,而不是密碼自己。所以咱們須要獲取受權碼。html
你須要作以下截圖的配置git
#!/usr/bin/env groovy Jenkinsfile pipeline { agent any environment { def gitRepo="https://github.com/wenboyang214/jenkinsCsharpTest.git" def mailRecipients="wenbya@163.com" } stages { stage('1. check the git repo'){ steps{ git "${gitRepo}" } } stage('3. msbuild the project'){ steps{ bat "msbuild .\\JenkinsTest\\JenkinsTest.sln" } } } post { always { echo 'testing email notification' } success { echo 'This will run only if successful' emailext( body: '''${SCRIPT, template="groovy-html.template"}''', mimeType: 'text/html', subject: "[Jenkins]${currentBuild.fullDisplayName}", to: "${mailRecipients}", replyTo: "${mailRecipients}", recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'DevelopersRecipientProvider']] ) } failure { echo 'This will run only if failed' emailext( body: '''${SCRIPT, template="groovy-html.template"}''', mimeType: 'text/html', subject: "[Jenkins]${currentBuild.fullDisplayName}", to: "${mailRecipients}", replyTo: "${mailRecipients}", recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'DevelopersRecipientProvider']] ) } 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' } } }
而後直接啓動這個test itemgithub
更改msbuild的路徑,故意讓其build出錯,檢查是否能收到報錯郵件。windows
bat 「msbuild .\JenkinsTest\JenkinsTest.sln」 => bat 「msbuild .\JenkinsTest1\JenkinsTest.sln」