Jenkins布尔参数在groovy中始终为true

 卫宇欢试试 发布于 2022-12-15 20:02

我有一个调用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的唯一方法是不将其转换为布尔值,而只是将其作为字符串读取并将其用作字符串.

我宁愿使用它作为布尔值,所以任何有关此事的建议仍然受到赞赏.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有