我编写了一个函数来通过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插件就可以了。谁能帮我?
您可能需要的rawbuild属性currentBuild。
rawbuild
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动作
null
RemoteCause
您可能只想返回addrhashmap而不是hashmap [ ip: addr ],就像这样
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 }
然后
def addressInfo = [ ip: getIpAdder() ]
Alos注意到,根据您Jenkins的安全性,您可能需要允许在沙盒脚本中运行扩展方法。您会注意到RejectedSandboxException
RejectedSandboxException
您可以通过Manage Jenkins-> 批准解决此问题In-process Script Approval
Manage Jenkins
In-process Script Approval
希望它能起作用