我编写了一个函数,通过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]
我完全不熟悉,所以我不知道为什么它不起作用。使用EnvInj插件很好。任何人都可以帮助我吗?
答案 0 :(得分:2)
您可能需要rawbuild
的{{1}}属性。
以下脚本应该为您完成。
currentBuild
如果您使用它:
//$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
次操作
null
您可能只想返回RemoteCause
而不是哈希地图addr
,就像这样
[ 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
}
Alos请注意,根据Jenkins的安全性,您可能需要允许在沙盒脚本中运行扩展方法。您会注意到def addressInfo = [ ip: getIpAdder() ]
您可以通过RejectedSandboxException
- >批准此方法来解决此问题。 Manage Jenkins
希望它有效