If statement 如何在jenkins脚本化管道作业中使用布尔参数编写条件步骤?

If statement 如何在jenkins脚本化管道作业中使用布尔参数编写条件步骤?,if-statement,jenkins,jenkins-pipeline,If Statement,Jenkins,Jenkins Pipeline,我脚本中的这个条件总是被评估为true,并打印“Yes equal-running the stage” 即使我通过设置env.build_TESTING2=false来启动构建,它仍然会进入条件并打印“Yes equal-running the stage” 我还尝试了以下语法: stage('test cond'){ if(env.BUILD_TESTING2){ echo "Yes equal - running the stage" } else

我脚本中的这个条件总是被评估为true,并打印“Yes equal-running the stage”

即使我通过设置env.build_TESTING2=false来启动构建,它仍然会进入条件并打印“Yes equal-running the stage”

我还尝试了以下语法:

stage('test cond'){  
    if(env.BUILD_TESTING2){  
        echo "Yes equal - running the stage"
    } else {
        echo "Not equal - skipping the stage"
    }
}
但它也总是被评估为


如何在Jenkins脚本化管道中编写带有布尔参数的条件步骤?

您需要使用toBoolean()函数将此环境变量(字符串类型)转换为布尔值:

stage('test cond'){  
    if(env.BUILD_TESTING2.toBoolean()){  
        echo "Yes equal - running the stage"
    } else {
        echo "Not equal - skipping the stage"
    }
}

最好通过参数而不是env引用参数,这样它们就具有正确的对象类型。因此,请使用:

stage('test cond') {
    if(params.BUILD_TESTING2) {
        echo "Yes equal - running the stage"
    } else {
        echo "Not equal - skipping the stage"
    }
}

我认为您遇到了这里列出的类似问题-。将其设置为false实际上是将其设置为字符串而不是布尔值,因此任何非false或null的值都将被计算为true。如果您使用的是布尔型Jenkins构建参数,这是正确的答案。证据在@ben5556提供的链接中是的,但这不是问题所在。不过我还是投票了,因为这是一个很好的练习。谢谢@Davis8988。问题是“如何在Jenkins脚本化管道中编写带有布尔参数的条件步骤?”答案是“在条件步骤中通过参数而不是env引用参数”
stage('test cond') {
    if(params.BUILD_TESTING2) {
        echo "Yes equal - running the stage"
    } else {
        echo "Not equal - skipping the stage"
    }
}