首页 > 解决方案 > Jenkinsfile - 发布操作避免状态代码重复

问题描述

我正在使用 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"
    )
  }

标签: jenkinsjenkins-pluginsjenkins-pipeline

解决方案


你可以写一个函数并使用它,fe

 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"
    )
  }

推荐阅读