2017-08-04 9 views
1

リモートサーバーで実行されているjenkinsfileから "myGroovyScript.groovy"を実行しようとしています。 Groovyスクリプトはサーバーのパスに保存されます(scmではチェックされません)。しかし、私は例外を得ています。 jenkinsfileもそこにあるのと同じリポジトリでgithubのgroovyスクリプトをチェックすると良い方法がありますか?jenkinsfileからgroovyスクリプトを実行中に例外が発生しましたgroovy.lang.MissingPropertyException:このようなプロパティはありません:class:groovy.lang.Bindingのargs

私はjenkinsfile上から "myGroovyScript.groovy" を実行しようとしています

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を持っています。 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) 
+0

は、サンドボックスの問題のように見えますが - それを実行してみていないサンドボックス内またはスクリプトの承認に移動し、保留中のスクリプトを承認します –

+0

jenkinsファイルから "myGroovyScript.groovy"と呼んでいる場所で何か問題があると思います。そのグルーヴィーなスクリプトは引数をとりますが、スクリプトを呼び出す際には渡していません。私はそれをする方法を知らない。 – stackoverflow

答えて

0

問題が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') 
関連する問題