Jenkinsfile-后期操作避免状态代码重复

时间:2018-07-10 10:20:55

标签: jenkins jenkins-plugins jenkins-pipeline

我正在使用Jenkins管道来运行构建。

如何避免执行相同代码(失败和不稳定)的2种帖子状态的代码重复?

示例代码段:

  post {
  failure
  {
    emailext(
    attachmentsPattern: '**/log.txt', 
    body: "Something is wrong with ${env.BUILD_URL}", 
    subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", 
    to: "test@test.gmail"
    )
  }
  unstable
  {
    emailext(
    attachmentsPattern: '**/log.txt', 
    body: "Something is wrong with ${env.BUILD_URL}", 
    subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", 
    to: "test@test.gmail"
    )
  }

1 个答案:

答案 0 :(得分:2)

您可以编写函数并使用它,例如

 post {
  failure
  {
    sendMail()
  }
  unstable
  {
    sendMail()
  }

  def sendMail() {
    emailext(
    attachmentsPattern: '**/log.txt', 
    body: "Something is wrong with ${env.BUILD_URL}", 
    subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", 
    to: "test@test.gmail"
    )
  }