Jenkins groovy (SharedLibrary) class 无法访问 WorkflowScript 成员
Jenkins groovy (SharedLibrary) class can't access WorkflowScript members
我有一个简单的管道脚本可以访问 groovy class(位于共享库中),
管道脚本将(对自身的)引用传递给 class,
class 然后调用在脚本中找到的函数或闭包,
该函数访问同一脚本中的成员,
结果是 groovy.lang.MissingPropertyException 异常。
所以这是实际的(可运行的)代码:
Jenkins pipeline script:
@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 class:
package my.domain
class Tester
{
Script scriptRef = null;
public Tester(Script scriptRef)
{
this.scriptRef = scriptRef;
}
public void test()
{
this.scriptRef.closure();
}
}
the exception received:
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.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.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.access0(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.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 transform 过程而发生的事情,脚本成员不存在不再(不是按名称而且不在范围内[可能]),
- 有没有人有解决方案/想法/方向?
- 有没有人知道关于 CPS 转换过程的信息(特别是 Jenkins,不是 CPS 理论),或者我如何才能看到转换后的最终产品 WorkflowScript?
基于备注中的[=15=]解决方案:
- jenkins 管道脚本作业是用 groovy
编写的
- 然后将其编译并(在运行期间)将 CPS 转换为 WorkflowScript groovy class,并使用执行的单个方法
并且因为它只是一个 groovy class 那么应该应用 groovy 范围规则,
在 groovy 中,如果你声明一个 var / 'def' 你可以在没有 'def' 前缀的情况下做到这一点,这将声明它在脚本中的任何地方都全局可用,
因此将管道脚本中的声明更改为:
tester = new Tester(this)
就是这样!
Jenkins pipeline script:
@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 注解并保持声明原样,
how-do-i-create-and-access-the-global-variables-in-groovy
因此我们也可以将管道更改为如下所示:
Jenkins pipeline script:
@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();
}
我有一个简单的管道脚本可以访问 groovy class(位于共享库中), 管道脚本将(对自身的)引用传递给 class, class 然后调用在脚本中找到的函数或闭包, 该函数访问同一脚本中的成员,
结果是 groovy.lang.MissingPropertyException 异常。
所以这是实际的(可运行的)代码:
Jenkins pipeline script:
@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 class:
package my.domain
class Tester
{
Script scriptRef = null;
public Tester(Script scriptRef)
{
this.scriptRef = scriptRef;
}
public void test()
{
this.scriptRef.closure();
}
}
the exception received:
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.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable.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.access0(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService.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 transform 过程而发生的事情,脚本成员不存在不再(不是按名称而且不在范围内[可能]),
- 有没有人有解决方案/想法/方向?
- 有没有人知道关于 CPS 转换过程的信息(特别是 Jenkins,不是 CPS 理论),或者我如何才能看到转换后的最终产品 WorkflowScript?
基于备注中的[=15=]解决方案:
- jenkins 管道脚本作业是用 groovy 编写的
- 然后将其编译并(在运行期间)将 CPS 转换为 WorkflowScript groovy class,并使用执行的单个方法
并且因为它只是一个 groovy class 那么应该应用 groovy 范围规则, 在 groovy 中,如果你声明一个 var / 'def' 你可以在没有 'def' 前缀的情况下做到这一点,这将声明它在脚本中的任何地方都全局可用,
因此将管道脚本中的声明更改为:
tester = new Tester(this)
就是这样!
Jenkins pipeline script:
@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 注解并保持声明原样,
how-do-i-create-and-access-the-global-variables-in-groovy
因此我们也可以将管道更改为如下所示:
Jenkins pipeline script:
@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();
}