用于条件后生成步骤的Jenkinsfile声明式语法

时间:2018-10-30 21:53:34

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

我有一个Jenkinsfile用于这样的多分支管道:

pipeline {
    agent any
    stages {
        // ...
    }
    post { 
        failure { 
            mail to: 'team@example.com',
                 subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
                 body: "Something is wrong with ${env.BUILD_URL}"
        }
    }
}

我只希望在master分支上发送失败电子邮件。有没有办法使邮件步骤成为条件邮件?根据文档,when指令只能在stage内部使用。

1 个答案:

答案 0 :(得分:4)

就像您已经注意到何时仅在阶段中起作用一样。并且发布条件中只能使用有效的步骤。 您仍然可以在 script 块内使用 scripted语法,并且 script 块是有效的 step 。因此,您应该能够在脚本块内使用 if 以获得所需的行为。

...
  post {
    failure {
      script {
        if (env.BRANCH_NAME == 'master') {
          ... # your code here
        }
      }
    }
  }
}

请参阅JENKINS-52689

相关问题