简体   繁体   English

从jenkinsfile groovy.lang.MissingPropertyException执行groovy脚本时发生异常:无此类属性:groovy.lang.Binding类的args

[英]exception while executing groovy script from jenkinsfile groovy.lang.MissingPropertyException: No such property: args for class: groovy.lang.Binding

I am trying to execute "myGroovyScript.groovy" from a jenkinsfile which is running at a remote server. 我正在尝试从在远程服务器上运行的jenkinsfile执行“ myGroovyScript.groovy”。 Groovy script is kept in a path in server (not checked in scm). Groovy脚本保存在服务器中的路径中(未在scm中检查)。 But I am getting exception. 但是我越来越例外了。 Is there a better way to do if I checkin the groovy script in github in same repo where jenkinsfile is also there. 如果我在同样存在jenkinsfile的回购中在github中的groovy脚本中签入,还有更好的方法吗?

I have a jenkinsfile as below 我有一个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"
}

I am trying to execute "myGroovyScript.groovy" from above jenkinsfile. 我正在尝试从jenkinsfile上面执行“ myGroovyScript.groovy”。 Groovy script is as below 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
        }
    }
}

I am running my jenkinsfile in remote linux server and after running it gives below stack trace. 我在远程linux服务器上运行我的jenkinsfile,运行后给出下面的堆栈跟踪。 could you please help whats the problem. 你能帮忙解决什么问题吗? Sorry for long post. 抱歉,很长的帖子。

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)

The problem is that in myGroovyScript.groovy you use def service = args[0] but args variable is not defined anywhere. 问题是在myGroovyScript.groovy您使用的是def service = args[0]args变量未在任何地方定义。 You can define it as a global variable in Jenkinsfile this way 您可以这样在Jenkinsfile中将其定义为全局变量

//...
args = ['abcd'] //note you have to define it without def to make it global
def example = load "${rootDir}myGroovyScript.groovy" 
//...

But using global variables is error prone and not recommended. 但是使用全局变量容易出错,不建议使用。 The better way would be wrapping myGroovyScript.groovy into function and then call it and pass service as parameter. 更好的方法是将myGroovyScript.groovy包装到函数中,然后调用它并将服务作为参数传递。 Something like this 像这样
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

And then in Jenkinsfile 然后在Jenkinsfile中

def example = load "${rootDir}myGroovyScript.groovy"
example.execute('abc')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 groovy.lang.MissingPropertyException:没有这样的属性:类的脚本:groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: script for class: groovy.lang.Binding 发生异常:groovy.lang.MissingPropertyException:没有这样的属性:类的内容:groovy.lang.Binding - Exception occurred: groovy.lang.MissingPropertyException: No such property: content for class: groovy.lang.Binding 詹金斯 Android groovy.lang.MissingPropertyException: 没有这样的属性: 类的 HOME: groovy.lang.Binding - Jenkins Android groovy.lang.MissingPropertyException: No such property: HOME for class: groovy.lang.Binding groovy.lang.MissingPropertyException:否此类属性:类groovy.lang.Binding的管道 - groovy.lang.MissingPropertyException: No such property: pipeline for class: groovy.lang.Binding groovy.lang.MissingPropertyException:没有这样的属性:class 的 sh:groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: sh for class: groovy.lang.Binding groovy.lang.MissingPropertyException:没有此类属性:类的节点:groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: node for class: groovy.lang.Binding groovy.lang.MissingPropertyException: 没有这样的属性: jenkins for class: groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding groovy.lang.MissingPropertyException:无此类属性:类别:groovy.lang.Binding的kubernetes-当变量为空时 - groovy.lang.MissingPropertyException: No such property: kubernetes for class: groovy.lang.Binding - when variable is empty groovy.lang.MissingPropertyException: 没有这样的属性: buildJobArray for class: groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: buildJobArray for class: groovy.lang.Binding 获取 groovy.lang.MissingPropertyException: No such property: datepart for class: groovy.lang.Binding - Getting groovy.lang.MissingPropertyException: No such property: datepart for class: groovy.lang.Binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM