gpt4 book ai didi

Jenkins 管道可选 bool 参数

转载 作者:行者123 更新时间:2023-12-05 00:48:10 27 4
gpt4 key购买 nike

我正在尝试使用具有可被覆盖的默认 bool 值的自定义函数。问题是它不会覆盖默认值。所有迭代都匹配“else”。

pipeline {

agent {
label 'any'
}

stages {
stage('Foo') {
steps {
doThing('/opt/prod','athos',true)
doThing('/opt/demo','aramis',true)
doThing('/opt/test','porthos')
doThing('/opt/dev','dartagnan')
}
}
}
}

def doThing(def targetDir, def stackName, def prod=false) {
if ( env.prod == true ) {
sh """
execute-bin \
-Dbin.target=${targetDir} \
-Dbin.stackName=${stackName} \
-Dbin.prod=true
"""
} else {
sh """
execute-bin \
-Dbin.target=${targetDir} \
-Dbin.stackName=${stackName}
"""
}
}

最佳答案

尝试比较字符串值:

  if ( prod == 'true' ) 

这是因为环境变量总是字符串,而没有 qoutes 的 true 是一个 bool 值,所以它永远不等于:

考虑一下:

def doThing(def prod=false) {
if ( prod == true ) {
println 'TRUE'
} else {
println 'FALSE'
}
}

// this is how environment are passed into the pipeline from jenkins UI
doThing('true')
> FALSE
doThing('false')
> FALSE

// if environment variables were boolean (and they are not) it would be ok
doThing(true)
> TRUE
doThing(false)
> FALSE

// the current equality check is always false
println true=='true'
> false
println true=='false'
> false

关于Jenkins 管道可选 bool 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53052400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com