我正在尝试执行" myGroovyScript.groovy"来自在远程服务器上运行的jenkinsfile。 Groovy脚本保存在服务器的路径中(未在scm中检查)。但我得到例外。如果我在同一个repo中检查github中的groovy脚本,还有更好的方法,jenkinsfile也在那里。
我有一个jenkinsfile,如下所示
node('abcd'){
def buildInput;
echo 'Deploying build'
if(!params.buildName) {
buildInput = input(
id: 'userInput', message: 'What is the build name?', parameters: [
[$class: 'TextParameterDefinition', defaultValue: 'BuildNameIsABC', description: 'Environment', name: 'buildName']
])
}
buildToUse = params.buildName ? params.buildName : buildInput;
echo ("Env: "+buildToUse);
if ( "${params.buildParam}" == 'prequal' || !params.buildParam ){
stage('Prequal') {
echo 'Checking prequal status for my product build'
def rootDir = '/home/pathToGroovyScript'
def example = load "${rootDir}myGroovyScript.groovy"
}
我正在尝试执行" myGroovyScript.groovy"从jenkinsfile上面。 Groovy脚本如下所示
#!/usr/bin/env groovy
def maxAttempt = 90
def attempt = 1
def frontDoorUrl
def waitTimeMS = 10000
def uiNodes
def service = args[0]
def uiNodesReady = false
def uiFrontDoorReady = false
//init important data in regards to the service
if ("abcd".equalsIgnoreCase(service)) {
println 'Init config for service abcd'
frontDoorUrl = "http://myFrontDoorURL"
uiNodes = ["http://myUINodes"]
}
//ping nodes <service>
while (attempt <= maxAttempt && uiNodesReady == false) {
println 'Attempt #' + attempt
for (i = 0; i < uiNodes.size(); i++) {
def result = ('curl -m 2 --write-out "%{http_code}" ' + uiNodes[i]).execute().text
println 'UI node: ' + i + ' ::: ' + uiNodes[i] + ' status: ' + result
if (result.contains("<StatusCode>200</StatusCode>")) {
uiNodesReady = true
} else {
uiNodesReady = false
break
}
}
}
我在远程linux服务器上运行我的jenkins文件,运行后给出了下面的堆栈跟踪。你能帮忙解决这个问题吗?很抱歉很长的帖子。
groovy.lang.MissingPropertyException: No such property: args for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
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:28)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at Script1.run(Script1.groovy:8)
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.GeneratedMethodAccessor513.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:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:173)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:162)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
答案 0 :(得分:2)
问题是在myGroovyScript.groovy
中您使用def service = args[0]
但args
变量未在任何地方定义。
您可以通过这种方式将其定义为Jenkinsfile中的全局变量
//...
args = ['abcd'] //note you have to define it without def to make it global
def example = load "${rootDir}myGroovyScript.groovy"
//...
但是使用全局变量容易出错,不推荐使用。更好的方法是将myGroovyScript.groovy
包装到函数中,然后调用它并将服务作为参数传递。像这样的东西
myGroovyScript.groovy
:
#!/usr/bin/env groovy
def execute(service) {
def maxAttempt = 90
def attempt = 1
def frontDoorUrl
def waitTimeMS = 10000
def uiNodes
def uiNodesReady = false
def uiFrontDoorReady = false
// all code go into function and ramain the same
}
return this // you also need to return a reference to this script
然后在Jenkinsfile
def example = load "${rootDir}myGroovyScript.groovy"
example.execute('abc')