0

我有一个简单的管道脚本,它访问一个 groovy 类(它驻留在共享库中),管道脚本将引用(对自身)传递给该类,然后该类调用在脚本中找到的函数或闭包,该函数访问同一脚本中的成员,

结果是 groovy.lang.MissingPropertyException 异常。

所以这是实际的(可运行的)代码:

詹金斯管道脚本:

@Library("testLib") _

import my.domain.Tester;

public Tester tester = new Tester(this);

def closure()
{
    echo "tester: " + tester;
}

node("master")
{
    tester.test();
}

com.domain.Tester.groovy 类:

package my.domain

class Tester
{
    Script scriptRef = null;

    public Tester(Script scriptRef)
    {
        this.scriptRef = scriptRef;
    }

    public void test()
    {
        this.scriptRef.closure();
    }
}

收到的异常:

groovy.lang.MissingPropertyException: No such property: tester for class: WorkflowScript
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.closure(WorkflowScript:9)
    at my.domain.Tester.test(Tester.groovy:8)
    at WorkflowScript.run(WorkflowScript:23)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.GeneratedMethodAccessor538.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Finished: FAILURE

所以很明显这是由于管道脚本经历的cps转换过程而发生的事情,并且脚本成员不再存在(不是按名称,也不是在范围内[可能]),

  1. 有没有人有解决方案/想法/方向?
  2. 有没有人有关于 CPS 转换过程的信息(特别是 Jenkins,而不是 CPS 理论),或者我如何在转换后看到最终产品 WorkflowScript?
4

1 回答 1

0

基于评论中的daggett解决方案:

  1. 詹金斯管道脚本作业是用 groovy 编写的
  2. 然后将其编译并 CPS 转换(在运行期间)成一个 WorkflowScript groovy 类,该类具有一个执行的方法

并且由于它只是一个 groovy 类,因此应该应用 groovy 范围规则,在 groovy 中,如果您声明 var / 'def' 您可以在没有 'def' 前缀的情况下执行此操作,这将声明它在脚本中的任何地方都可以全局使用,

所以将管道脚本中的声明更改为:

tester = new Tester(this)

就是这样!

詹金斯管道脚本:

@Library("testLib") _

import my.domain.Tester;

// note the definition without the 'def' or Tester or public
tester = new Tester(this);

def closure()
{
    echo "tester: " + tester;
}

node("master")
{
    tester.test();
}

或者:

从 Groovy 1.8 及更高版本开始,我们可以添加 @Field 注释并保持声明原样,

我如何在 groovy 中创建和访问全局变量

因此我们也可以将管道更改为如下所示:

詹金斯管道脚本:

@Library("testLib") _

import my.domain.Tester;

// note the definition without the 'def' or Tester or public
@Field Tester tester = new Tester(this);

def closure()
{
    echo "tester: " + tester;
}

node("master")
{
    tester.test();
}
于 2019-11-30T11:16:21.133 回答