2016-09-27 2 views
4

빌드가 실패 할 때 전자 메일을 보내는 구식 포스트 빌드 작업을 젠킨스 파이프 라인에 어떻게 추가 할 수 있습니까? 파이프 라인을 위해 GUI에서 "빌드 후 작업"을 찾을 수 없습니다. 나는 전체 빌드 스크립트를 try/catch 할 수 있다는 것을 알고 있지만 빌드 스크립트가 크고 작업이 수동으로 중단 된 경우에도 이메일을 계속 보낼 때보기 흉한 것처럼 보입니다. 이전 빌드 email-ext 기반 빌드 후 작업과 동일한 기능을 수행하고 싶습니다.jenkins 파이프 라인 오류시 전자 메일 보내기

try { 
    // Do sth 
} catch(e) { 
    emailext body: '$DEFAULT_CONTENT', 
     recipientProviders: [ 
      [$class: 'CulpritsRecipientProvider'], 
      [$class: 'DevelopersRecipientProvider'], 
      [$class: 'RequesterRecipientProvider'] 
     ], 
     replyTo: '$DEFAULT_REPLYTO', 
     subject: '$DEFAULT_SUBJECT', 
     to: '$DEFAULT_RECIPIENTS' 
} 

답변

4

이 절차를 try와 함께 사용해야하는 시점에 빌드 후 조치를 취하십시오.

하지만 나중에이 단계가 계획됩니다 (실제로 검토 중입니다). 추가 정보는 (선언적 파이프 라인 부분을 참조)이 블로그 게시물을 읽을 수 있습니다

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

선언 파이프 라인이 쉽게 파이프 라인 스크립트하여 시도의 복잡성 ... 캐치없이 파이프 라인의 끝에서 조건부로 일을 실행하게 postBuild 섹션을 소개 : 자신의 블로그 게시물의 일부입니다. 빌드 상태 당신이 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

이 답변 버전 내 젠킨스 일했다. 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' 
     } 
    } 
}