user3296316
user3296316

Reputation: 157

Jenkins pipeline MissingMethodException: No signature of method:

I wrote a function to insert inject a variable through the EnvInj-plugin. Following script I used:

import hudson.model.*
import static hudson.model.Cause.RemoteCause

@com.cloudbees.groovy.cps.NonCPS
def call(currentBuild) {
    def ipaddress=""
    for (CauseAction action : currentBuild.getActions(CauseAction.class)) {

        for (Cause cause : action.getCauses()) {
            if(cause instanceof RemoteCause){
                ipaddress=cause.addr
                break;
            }
        }
    }
    return ["ip":ipaddress]
}

When I put it the the folder $JENKINS_HOME/workflow-libs/vars as a global function, i get the following error:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper.getActions() is applicable for argument types: (java.lang.Class) values: [class hudson.model.CauseAction]

I am completly new in groovy, so I don't know why it is not working. With the EnvInj-plugin it was fine. Can anyone help me?

Upvotes: 0

Views: 13072

Answers (1)

Rik
Rik

Reputation: 3887

You will probably need the rawbuild property of the currentBuild.

The following script should do it for you.

//$JENKINS_HOME/workflow-libs/vars/getIpAddr.groovy
@com.cloudbees.groovy.cps.NonCPS
def call() {
    def addr = currentBuild.rawBuild.getActions(CauseAction.class)
        .collect { actions ->
            actions.causes.find { cause -> 
                cause instanceof hudson.model.Cause.RemoteCause 
            }
        }
    ?.first()?.addr
    [ ip: addr ]
}

if you use it like:

def addressInfo = getIpAddr()
def ip = addressInfo.ip

Note that it will be null if there are no RemoteCause actions

You might want to return only the addr instead of the hashmap [ ip: addr ], like so

//$JENKINS_HOME/workflow-libs/vars/getIpAddr.groovy
@com.cloudbees.groovy.cps.NonCPS
def call() {
    currentBuild.rawBuild.getActions(CauseAction.class)
        .collect { actions ->
            actions.causes.find { cause -> 
                cause instanceof hudson.model.Cause.RemoteCause 
            }
        }
    ?.first()?.addr
}

and then

def addressInfo = [ ip: getIpAdder() ]

Alos note that, depending on the security of your Jenkins, you might need to allow the running of extension methods in sandboxed scripts. You will notice a RejectedSandboxException

You can solve this by approving this through Manage Jenkins -> In-process Script Approval

Hope it works

Upvotes: 2

Related Questions