我有一个调用groovy脚本的Jenkins作业,groovy脚本使用Jenkins参数来完成它的工作。除了布尔参数,我可以检索所有参数而没有问题。布尔参数是Jenkins中的复选框
我将jenkins参数读入Groovy,如下所示:
boolean libraryBranch = config.get('library_branch_feature');
现在我打印'libraryBranch'变量
out.println "-- Library branch feature?: " + libraryBranch.toString();
我得到以下印刷品:
- 图书馆分支功能?:true
因此,如果选择了布尔Jenkins参数并不重要,我在Groovy中总是有一个布尔值'true'。读取同一作业中的所有其他(字符串)参数没有问题。
有人可以帮我解决这个问题吗?
修改
好的我已经决定尝试以其他几种方式检索代码并找到一个好的解决方案:
Boolean libraryBranch = build.buildVariableResolver.resolve("library_branch_feature");
String libraryBranchString = build.buildVariableResolver.resolve("library_branch_feature").toString();
Boolean libraryBranchStringAsBoolean = build.buildVariableResolver.resolve("library_branch_feature") as Boolean;
然后打印上述变量:
out.println "-- Library branch feature?: " + libraryBranch;
out.println "-- Library branch feature to String: " + libraryBranch.toString();
out.println "-- Library branch feature to String: " + libraryBranch.toString();
out.println "-- Library branch feature as String: " + libraryBranchString;
out.println "-- Library branch feature String as Boolean: " + libraryBranchStringAsBoolean;
以上打印件的输出结果如下:
-- Library branch feature?: true
-- Library branch feature to String: true
-- Library branch feature to String: true
-- Library branch feature as String: false
-- Library branch feature String as Boolean: true
因此,将布尔值正确读取为false的唯一方法是不将其转换为布尔值,而只是将其作为字符串读取并将其用作字符串。
我宁愿使用它作为布尔值,所以对此事的任何建议仍然受到赞赏。
答案 0 :(得分:13)
答案是将Jenkins参数读为String,然后使用方法.toBoolean()将其转换为布尔值;所以我的libraryBranch现在设置如下:
boolean libraryBranch = build.buildVariableResolver.resolve("library_branch_feature").toString().toBoolean();
答案 1 :(得分:2)
出于好奇,我尝试了以下方法:
设置变量' testmode'作为管道设置中的布尔值。 启动作业,testmode设置为false(未选中)。 在管道脚本中,在输入时立即运行以下代码:
testmode=testmode.toBoolean()
if (testmode==false) { print "testmode tests true"}else{print "testmode tests false"}
您将看到结果' testmode tests false'。
验证我声称“布尔”'变量testmode在它进来时是一个字符串,我试过了:
testmode=testmode.toBoolean()
testmode=testmode.toBoolean()
它在第二个' toBoolean'上爆炸了。我会给你一个错误信息......
所以,我声称testmode是字符串" false"而不是作为布尔值(无论是因为晋升,降级,强迫等)。
所以,如果它是一个布尔参数,你真的想把它当成一个布尔值而不必说'if(booleanvar ==" true")',那么如上所述将其转换为布尔值,您就完成了。
答案 2 :(得分:0)
我在读取java属性时遇到了同样的问题 - 检索到的布尔值总是为true。所以,假设我有一个settings
文件,其属性为debugging=false
。此测试将失败:
class ConfigurationTest extends GroovyTestCase {
void testProcessConfiguration() {
Properties properties=new Properties()
FileReader fileReader=new FileReader('settings')
properties.load(fileReader)
boolean debug=properties.getOrDefault('debugging',false)
assert !debug
fileReader.close()
}
}
但是如果你使它properties.getOrDefault('debugging',false).toString().toBoolean()
,它将返回正确的值。可能这是因为胁迫?
答案 3 :(得分:0)
问题和答案有些陈旧。现代方法
使用一个布尔参数是使用params.
全局值。 参数
正确地显示布尔参数,以便以下内容都能正常工作
不需要字符串/布尔值转换。
if (params.library_branch_feature) {
// truth logic here
}
// or
if (params.library_branch_feature != false) {
// truth logic here
}
从管道语法中获取参数。:
将构建中定义的所有参数公开为具有各种类型值的只读映射。示例:
如果(params.BOOLEAN_PARAM_NAME){doSomething()} 或提供非平凡的默认值:
if(params.getOrDefault('BOOLEAN_PARAM_NAME',true)){doSomething()}
注意: 布尔参数可以没有值可能没有意义, 但是'.getOrDefault()'可用于设置字符串参数的默认值。