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

从Jenkins流水线任务test调用new_job时出现属性缺失异常

Jenkins流水线调用子任务时MissingPropertyException报错解决方法

问题场景

在Jenkins中,流水线任务test调用另一个流水线任务new_job并传递变量时出现报错,但两个任务单独运行均正常。

Test任务代码

pipeline {
    agent any
    stages {
        stage('Executing First job') {
            steps {
                sh 'echo "Running first job"'
                }
        }
        stage('Running another job') {
             when {
                     equals expected: true, actual: params.deploy2job
             }
             steps {
                  build job: 'new_job', parameters: [string(name: 'abc', value: qa), string(name: 'xyz', value: test)]
             }
         }
    }
}

错误信息

[Pipeline] { (Running another job)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: qa for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:251)
    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:333)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:19)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
    at 
....
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Finished: FAILURE

错误原因

报错核心是groovy.lang.MissingPropertyException: No such property: qa for class: groovy.lang.Binding,Groovy解析代码时,将qa和test识别为需要引用的变量,但这两个变量在当前流水线上下文中并未定义。而你实际的需求应该是传递字符串字面量"qa"和"test"。

解决方案

将需要传递的字符串值用单引号或双引号包裹,明确告知Groovy这是字符串字面量,而非变量引用:

修改后的流水线代码

pipeline {
    agent any
    stages {
        stage('Executing First job') {
            steps {
                sh 'echo "Running first job"'
            }
        }
        stage('Running another job') {
             when {
                     equals expected: true, actual: params.deploy2job
             }
             steps {
                  build job: 'new_job', parameters: [
                      string(name: 'abc', value: 'qa'), 
                      string(name: 'xyz', value: 'test')
                  ]
             }
         }
    }
}

额外说明

如果qa和test确实是需要引用的变量(比如流水线参数、环境变量),需要确保变量已正确定义并通过正确的方式引用:

  • 若为流水线参数:使用params.qa、params.test
  • 若为环境变量:使用env.qa、env.test

内容的提问来源于stack exchange,提问作者Nitesh Singhal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 02:10:19