2017-01-10 71 views
0

我寫了一個函數來插入通過EnvInj-plugin插入一個變量。下面的腳本我用:Jenkins管道MissingMethodException:沒有方法的簽名:

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 /工作流庫/乏作爲一個全球性的功能,我得到以下錯誤:

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它很好。誰能幫我?

回答

1

您可能需要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 

注意,這將是null如果沒有RemoteCause行動

你可能只想返回的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() ] 

阿洛斯注意的是,根據您的詹金斯的安全性,您可能需要允許在沙盒腳本的擴展方法運行。你會發現一個RejectedSandboxException

您可以通過Manage Jenkins通過批准這個解決這個 - >In-process Script Approval

希望我的作品

+1

首先一個正常工作!非常感謝Rik !!!!!最好的人:) – user3296316

相關問題