【问题标题】:How to use conditional post action in Jenkins pipeline?如何在 Jenkins 管道中使用条件后操作?
【发布时间】:2022-12-09 05:15:57
【问题描述】:

我正在使用 Jenkins 声明式管道,并希望根据构建状态执行一些构建后操作。

更准确地说,我希望这些条件成立:

beforeAgent 真 && jobName == '赛普拉斯测试'

这是我的代码:

post {
        always {
            script {
                passwordIDs.each{ pw ->
                    credentialFetch.deleteTemporaryCredential(env.BUILD, pw, expireTime)
                  }
              }
        }
    }

知道我可以在哪里使用我的条件吗?另外,如何使用它们,因为 Post 不支持 when 条件

【问题讨论】:

    标签: jenkins groovy cypress jenkins-plugins jenkins-groovy


    【解决方案1】:

    您可以在 post 条件中的 script 块中使用正常的 if 条件,就像在正常阶段中所做的那样。例如,我在我的一项工作中使用了它:

    post {
        failure {
            script {
                def response = httpRequest '${env.BUILD_URL}/consoleText'
                if (response.content.contains("Automatic merge failed; fix conflicts")){
                    env.BUILD_FAILURE_MESSAGE = "Checkout Failing! Make sure that there are no merge conflicts...
                } else {
                    env.BUILD_FAILURE_MESSAGE = "Checkout Failing! Check the build log and re-run the build if the issue seems unrelated to your commit...
                }
            }
        }
    }
    

    如您所见,这是一个普通的if条件,用于检查字符串是否包含文本。您应该能够以类似的方式使用您的条件:

    if (beforeAgent && jobName == 'Cypress Test')
    

    【讨论】:

    • 谢谢,似乎工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多