例外"脚本不允许使用staticMethod"从jenkinsfile运行groovy脚本时

时间:2017-08-12 23:43:50

标签: jenkins groovy jenkins-pipeline

我正在尝试执行" myGroovyScript.groovy"来自在远程服务器上运行的jenkinsfile。 Groovy脚本保存在服务器的路径中(未在scm中检查)。但我得到例外。

我的Jenkins文件如下:

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')
}

和我的常规脚本,即#34; 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 个答案:

答案 0 :(得分:1)

您必须在In-process Script Approval部分手动批准该方法。但我想它无论如何都行不通,因为在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)