2017-08-12 96 views
0

我想從運行在遠程服務器上的jenkins文件執行「myGroovyScript.groovy」。 Groovy腳本保存在服務器的路徑中(未在scm中籤入)。但我得到例外。異常「腳本不允許使用staticMethod」從jenkinsfile運行groovy腳本

我Jenkinsfile是如下:

attempt=1; 
maxAttempt = 90; 
uiNodesReady = false; 
uiFrontDoorReady = false; 
waitTimeMS = 10000; 
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 = '/path to myGroovyscript.groovy/' 
      def script = load "${rootDir}myGroovyscript.groovy" 
      def executeStatus= script.execute('abcd') 
} 

和我的Groovy腳本,這是 「myGroovyScript.groovy」 是如下

#!/usr/bin/env groovy 

def frontDoorUrl 
def uiNodes 

def execute(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 
     } 
    } 
} 
} 
return this 

我得到以下例外,當我跑我的jenkins工作:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods execute java.lang.String 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:189) 
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:102) 
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:148) 
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152) 
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16) 
    at Script1.execute(Script1.groovy:53) 
    at WorkflowScript.run(WorkflowScript:119) 
    at ___cps.transform___(Native Method) 
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57) 
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109) 
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:77) 
    at sun.reflect.GeneratedMethodAccessor442.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) 

回答

1

您必須手動批准該方法In-process Script Approval secti上。但我想這不會起作用,因爲在Jenkins管道中,您應該使用sh步驟來執行shell腳本。 所以不是

def result = ('curl -m 2 --write-out "%{http_code}" ' + uiNodes[i]).execute().text 

使用

def result = sh(script: 'curl -m 2 --write-out "%{http_code}" ' + uiNodes[i], returnStdout: true)