2016-09-27 25 views
4

ビルドが失敗したときに電子メールを送信する旧式のポストビルドタスクをジェンキンスパイプラインに追加するにはどうすればよいですか?私はパイプラインのGUIで "ビルド後のアクション"を見つけることができません。私はビルドスクリプト全体をラップすることができることを知っていますが、ビルドスクリプトが大規模で、ジョブが手動で中止された場合でも電子メールを送信し続けると、これは醜いようです。私はpreviouse email-extベースのビルド後のアクションと同じ機能を実現したいと思います。jenkinsパイプラインのエラーで電子メールを送信

答えて

4

現時点では、この手順をtryと一緒に使用する必要があります。キャッチするとビルド後の処理が行われます。

今後、この手順は計画されています(実際はレビュー中です)。あなたは(宣言型のパイプラインの一部を参照してください)詳細な情報は、このブログの記事を読むことができます:

ジェンキンスJiraの中

https://jenkins.io/blog/2016/09/19/blueocean-beta-declarative-pipeline-pipeline-editor/

またはこのチケット:

https://issues.jenkins-ci.org/browse/JENKINS-38153

と、このプルリクエスト:

https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/13/

T彼はブログの記事の一部です:

宣言パイプラインの試行の手間をかけずに、パイプラインの最後に条件付きで物事を実行することが容易になりますポストビルドセクション...パイプラインスクリプトのキャッチを紹介します。ビルド状況は、あなたがpost > changed blockを使用することができます変更された場合にのみ、行動を起こすために

postBuild { 
always { 
sh 'echo "This will always run"' 
} 
success { 
    sh 'echo "This will run only if successful"' 
} 
failure { 
    sh 'echo "This will run only if failed"' 
} 
unstable { 
    sh 'echo "This will run only if the run was marked as unstable"' 
} 
changed { 
    sh 'echo "This will run only if the state of the Pipeline has changed"' 
    sh 'echo "For example, the Pipeline was previously failing but is now successful"' 
    sh 'echo "... or the other way around :)"' 
} 
} 
2

ステータスを変更した状態を確認するには、script blockcurrentBuild.currentResultプロパティの値を組み合わせて使用​​することができます。そのよう

は:

pipeline { 
    ... 

    post { 
     changed { 
      script { 
       if (currentBuild.currentResult == 'FAILURE') { // Other values: SUCCESS, UNSTABLE 
        // Send an email only if the build status has changed from green/unstable to red 
        emailext subject: '$DEFAULT_SUBJECT', 
         body: '$DEFAULT_CONTENT', 
         recipientProviders: [ 
          [$class: 'CulpritsRecipientProvider'], 
          [$class: 'DevelopersRecipientProvider'], 
          [$class: 'RequesterRecipientProvider'] 
         ], 
         replyTo: '$DEFAULT_REPLYTO', 
         to: '$DEFAULT_RECIPIENTS' 
       } 
      } 
     } 
    } 

} 
0

この回答verは私のジェンキンスに取り組みました。 2.96。

Jenkins pipeline email not sent on build failure - Stack Overflow

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' 
     } 
    } 
} 
関連する問題