Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception "Scripts not permitted to use staticMethod " when running groovy script from jenkinsfile

I am trying to execute "myGroovyScript.groovy" from a jenkinsfile which is running at a remote server. Groovy script is kept in a path in server (not checked in scm). But I am getting exception.

My Jenkinsfile is as below:

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

and my groovy script, which is , "myGroovyScript.groovy" is as below

#!/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

I am getting below exception when I am running my jenkins job:

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)
like image 585
learner Avatar asked Sep 15 '25 23:09

learner


1 Answers

You have to approve that method manually in In-process Script Approval section. But I guess it will not work anyway because in Jenkins pipeline you should use sh step to execute shell script. So instead

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

use

def result = sh(script: 'curl -m 2 --write-out "%{http_code}" ' + uiNodes[i], returnStdout: true)
like image 91
Vitalii Vitrenko Avatar answered Sep 19 '25 16:09

Vitalii Vitrenko