Jenkins 管道 MissingMethodException:没有方法签名:

Jenkins pipeline MissingMethodException: No signature of method:

我编写了一个函数来通过 EnvInj 插件插入一个变量。以下是我使用的脚本:

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]
}

当我将其作为全局函数放入文件夹 $JENKINS_HOME/workflow-libs/vars 时,出现以下错误:

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]

我是 groovy 的新手,所以我不知道为什么它不起作用。使用 EnvInj-plugin 没问题。谁能帮帮我?

您可能需要 currentBuildrawbuild 属性。

以下脚本应该可以为您完成。

//$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 ]
}

如果你像这样使用它:

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

请注意,如果没有 RemoteCause 个操作,它将是 null

您可能只想 return addr 而不是 hashmap [ ip: addr ],像这样

//$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
}

然后是

def addressInfo = [ ip: getIpAdder() ]

另外请注意,根据 Jenkins 的安全性,您可能需要在沙盒脚本中允许 运行 扩展方法。你会注意到 RejectedSandboxException

您可以通过 Manage Jenkins -> In-process Script Approval

批准来解决这个问题

希望有用