I'm trying to utilize a custom function with a default boolean value that can be overridden. Issue is it doesn't override the default. All iterations match "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}
"""
}
}
Try compare to the string value :
if ( prod == 'true' )
This happens because environment variables are always strings, and true without qoutes is a boolean so its never equal:
consider this:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With