Jenkins pipeline MissingMethodException: No signature of method:

天涯浪子 提交于 2019-12-12 04:32:45

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/41563550/jenkins-pipeline-missingmethodexception-no-signature-of-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!