Jenkins Build Pipeline fileLoad 在库文件中不起作用

Jenkins Build Pipeline fileLoad in library files not working

我正在使用 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库如下:

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.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[=12=]1(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.call(SandboxContinuable.java:32)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.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[=12=]0(CpsThreadGroup.java:78)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.call(CpsThreadGroup.java:183)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.call(CpsVmExecutorService.java:47)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService.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)

插件的文档说:

Use static initializers within the Groovy file of the loaded file to load more context from neighbor files.

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

提前致谢!

好的,经过反复试验,我找到了问题所在。

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;

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

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

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