Jenkins Build Pipeline fileLoad库文件无法正常工作

时间:2016-07-03 16:59:34

标签: jenkins groovy jenkins-workflow jenkins-pipeline

我正在使用Workflow Remote Loader插件的fileLoad函数来加载我的构建脚本的依赖项。在我的主文件中,我有以下内容:

集结app.groovy

def java
def util

node {
    stage "Setup"
    fileLoader.withGit('git@github.com:myorg/build-scripts.git', 'master', '234720asdf8ed1a2', '') {

        java = fileLoader.load('lib/java.groovy');
        util = fileLoader.load('lib/util.groovy');
    }

    util.checkIfFileExists('myFile.txt')
    def version = java.getJarVersion("api")
}

util.checkIfFileExists()效果很好,但调用java.getJarVersion时会出现问题。在getJarVersion中,还存在对util的依赖。我的java lib如下:

java.groovy

def util = fileLoader.load('lib/util.groovy')

def getJarVersion() {
    ...
    util.checkIfFileExists('myFile.txt')
    ...
}

当我在Jenkins中运行时出现以下错误:

groovy.lang.MissingPropertyException: No such property: util for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:62)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
    at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:23)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17)
    at Script4.checkIfFileExists(Script4.groovy:39)
    at Script4.getJarVersion(Script4.groovy:23)
    at WorkflowScript.run(WorkflowScript:18)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:62)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:54)
    at sun.reflect.GeneratedMethodAccessor254.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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:58)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:32)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:29)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:29)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:78)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:183)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

该插件的文档说:

  

在加载文件的Groovy文件中使用静态初始值设定项   从邻居文件加载更多上下文。

我已尝试static def util = fileLoader.load('lib/util.groovy'),但这会产生同样的错误。我也试过各种其他组合无济于事。也许这是对groovy语言缺乏了解?不太确定。

提前致谢!

2 个答案:

答案 0 :(得分:3)

好的,经过多次试验和错误,我发现了问题。

之前的java.groovy:

def util = fileLoader.load('lib/util.groovy')

def getJarVersion() {
    ...
    util.checkIfFileExists('myFile.txt')
    ...
}

return this;

java.groovy之后:

def getJarVersion() {
    ...
    util.checkIfFileExists('myFile.txt')
    ...
}

this.util = fileLoader.load('lib/util.groovy')
return this;

原始帖子中省略了return this;行,因为它位于远程文件加载器插件的文档中,但它是关键。当从build.groovy加载java.groovy时,它使用返回的this作为调用java.groovy的范围。当定义为util时,此范围不包括def util = fileLoader.load('lib/util.groovy')。在返回之前将其添加到此文件中,将util放在文件中所有函数调用的范围内。

答案 1 :(得分:3)

对于那些来自Google的类似错误消息,您必须安装Pipeline Remote Loader插件。否则会给你:

groovy.lang.MissingPropertyException: No such property: fileLoader for class: groovy.lang.Binding