jenkins - 从 jenkinsfile 执行 groovy 脚本时出现异常 groovy.lang.MissingPropertyException : No such property: args for class: groovy. lang.Binding

标签 jenkins groovy jenkins-pipeline

我正在尝试从远程服务器上运行的 jenkinsfile 执行“myGroovyScript.groovy”。 Groovy 脚本保存在服务器的路径中(未在 scm 中检查)。但我遇到了异常(exception)。如果我在 github 中 checkin jenkinsfile 所在的同一存储库中的 groovy 脚本,是否有更好的方法。

我有一个如下的 jenkins 文件

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"
}

我正在尝试从上面的 jenkinsfile 执行“myGroovyScript.groovy”。 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 服务器中运行我的 jenkinsfile,运行后给出以下堆栈跟踪。你能帮忙看看有什么问题吗?抱歉,帖子很长。

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)

最佳答案

问题在于,在 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')

关于jenkins - 从 jenkinsfile 执行 groovy 脚本时出现异常 groovy.lang.MissingPropertyException : No such property: args for class: groovy. lang.Binding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45514585/

相关文章:

jenkins - 如何调用Jenkins中存储的凭据以从Jenkinsfile中部署到自定义Nexus?

jenkins - Pipeline 中定义的 DynamicReferenceParameter

encryption - Jenkins 中存储的加密 key 在哪里?

git - 在 Centos 上使用 Jenkins 配置 git

groovy - 如何在元类中使用该方法更改 groovy 中方法的行为

grails - 与Grails的零对多关系?

jenkins - 为什么我的Gradle Jenkins插件尝试在任务执行前通过命令行开关

jenkins - 在从属节点上运行构建时,Docker为什么有用?

java - groovy 和 lombok @Value 中的命名参数构造函数

jenkins - 如何屏蔽 Jenkins Pipeline 项目中的密码字段?