Jenkins Pipeline按需禁用并发构建报错,求正确实现方式
解决Jenkins流水线选择性禁用并发构建的类型转换错误
直接上正确的实现代码:
List propsList = [ [$class: "jenkins.model.BuildDiscarderProperty", strategy: [$class: "LogRotator", numToKeepStr: "50", artifactNumToKeepStr: "20"]], parameters([ string(name: "BUILD_NODE", defaultValue: "linux", description: "Build node to use."), choiceParam(name: "DEBUG_LEVEL", choices: ["0", "1", "2", "3"], description: "Debugger verbosity.") ]) ] if (env.DONT_WANT_CONCURRENT_BUILDS?.toBoolean()) { propsList.add(disableConcurrentBuilds()) } properties(propsList)
错误原因解析
- 类型转换多余且错误:
disableConcurrentBuilds()返回的是Jenkins流水线专用的UninstantiatedDescribableWithInterpolation对象,用来延迟初始化任务属性,不需要强制转换成JobProperty,强行转换就会触发你看到的类型转换异常。 - 未将属性添加到列表:你之前只是声明了变量
jp,但没有把这个属性对象添加到propsList里,就算类型没问题,也不会生效。 - 空安全优化:加
?.操作符是为了避免当DONT_WANT_CONCURRENT_BUILDS环境变量不存在时,调用toBoolean()抛出空指针异常。
内容的提问来源于stack exchange,提问作者Chris F
相关产品推荐
相关产品推荐

