jenkins - Jenkins 管道可选布尔参数
问题描述
我正在尝试使用具有可以覆盖的默认布尔值的自定义函数。问题是它不会覆盖默认值。所有迭代都匹配“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 是布尔值,因此它永远不等于:
考虑一下:
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
推荐阅读
- excel - Excel VBA 复制范围(左侧的所有单元格)到嵌入的 Word 文档
- bash - 从脚本变量中提取文件名
- reactjs - 在 HTTPS 而不是 HTTP 中启动反应应用程序
- jquery - 点击甚至不适用于动态生成的元素
- sql-server - 为什么一个特定的 SQL Server 数据库无法将 VARCHAR 转换为 NUMERIC?
- sql-server - 访问连接后创建的新表
- npm - 我无法让 Node.js 安装当前版本(Windows)
- c# - 如何获取 [Authorize] 重定向到登录页面的原始 URL?
- javascript - 单击时更改生成的 div 的样式属性
- java - spring转换器接口如何保证实现的线程安全