You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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)

错误原因解析

  1. 类型转换多余且错误:disableConcurrentBuilds()返回的是Jenkins流水线专用的UninstantiatedDescribableWithInterpolation对象,用来延迟初始化任务属性,不需要强制转换成JobProperty,强行转换就会触发你看到的类型转换异常。
  2. 未将属性添加到列表:你之前只是声明了变量jp,但没有把这个属性对象添加到propsList里,就算类型没问题,也不会生效。
  3. 空安全优化:加?.操作符是为了避免当DONT_WANT_CONCURRENT_BUILDS环境变量不存在时,调用toBoolean()抛出空指针异常。

内容的提问来源于stack exchange,提问作者Chris F

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 14:22:18