Jenkins脚本化管道NoSuchMethodError:在步骤之间找不到这样的DSL方法“ setLatestRevision”

时间:2018-12-05 10:38:05

标签: jenkins groovy jenkins-pipeline

能否请您帮我,这段代码我在哪里出错?

下面的管道脚本:

def executeShInEnvironment = { String shFunction, String parameters ->
    sh '. $SCRIPTS_DIR/build_functions.sh; ' + shFunction + " " + parameters
}

def setLatestRevision = { String parameters ->
    executeShInEnvironment('GET_LAST_REVISION', parameters)
}

def buildApp(svnRootUrl, propertiesDir, propertiesFileName) {
    def String fromRevisionPropertiesKey = "LAST_REVISION_FOR_02"
    def String toRevisionPropertiesKey = "CURRENT_SVN_REVISION"
    def String svnPath = svnRootUrl + "/svn_path"

    echo "Find latest SVN revision"
    withEnv(['SVN_URL=' + svnPath ]) {
        def shFunctionParameters = propertiesFileName + " " + toRevisionPropertiesKey
        setLatestRevision(shFunctionParameters)           
    }
}

def prepareRun(shellSvn, propertiesSvn) {
    stage 'Prepare run'
         .... do checkouts
}

node
{ 
    def workspace = pwd()
    def String shellSvn = "/shell"
    def String propertiesSvn = "/property_files"
    def String propertiesDir =  workspace + propertiesSvn
    def String scriptsDir = workspace + shellSvn
    def String svnRootUrl = "http://SVN_URL_goes_here"
    def String propertiesFileName = "main_delivery.properties"

    prepareRun(shellSvn, propertiesSvn)

    withCredentials(............... {
        withEnv(...........){
            buildApp(svnRootUrl, propertiesDir, propertiesFileName)
        }    
    }    
}

为什么脚本说他看不到setLatestRevision方法?

  

java.lang.NoSuchMethodError:没有这样的DSL方法“ setLatestRevision”   在步骤中找到

无法使用的方法定义

setLatestRevision = { String parameters ->

工作方法

setLatestRevision(String parameters) {

有人可以解释为什么吗?

1 个答案:

答案 0 :(得分:0)

在第一种情况下,setLatestRevision是一个变量(闭包):

setLatestRevision = { String parameters ->

在第二种情况下,它是一种方法:

setLatestRevision(String parameters) {

Script class documentation的状态(在上一节 3.4变量中)指出:

如果变量如第一个示例中所声明[具有类型定义],则它是局部变量。它将在编译器将生成的run方法中声明,并且在脚本主体外部不可见。特别是,该变量在脚本的其他方法中将不可见

还有:如果未声明变量,它将进入脚本绑定。从方法[...]

可以看到绑定

因此,从def中删除setLatestRevision可以修复错误:

def executeShInEnvironment = { String shFunction, String parameters ->
    sh '. $SCRIPTS_DIR/build_functions.sh; ' + shFunction + " " + parameters
}

setLatestRevision = { String parameters ->
    executeShInEnvironment('GET_LAST_REVISION', parameters)
}

def buildApp(svnRootUrl, propertiesDir, propertiesFileName) {
    def String fromRevisionPropertiesKey = "LAST_REVISION_FOR_02"
    def String toRevisionPropertiesKey = "CURRENT_SVN_REVISION"
    def String svnPath = svnRootUrl + "/svn_path"

    echo "Find latest SVN revision"
    withEnv(['SVN_URL=' + svnPath ]) {
        def shFunctionParameters = propertiesFileName + " " + toRevisionPropertiesKey
        setLatestRevision(shFunctionParameters)           
    }
}

def prepareRun(shellSvn, propertiesSvn) {
    stage 'Prepare run'
         .... do checkouts
}

node
{ 
    def workspace = pwd()
    def String shellSvn = "/shell"
    def String propertiesSvn = "/property_files"
    def String propertiesDir =  workspace + propertiesSvn
    def String scriptsDir = workspace + shellSvn
    def String svnRootUrl = "http://SVN_URL_goes_here"
    def String propertiesFileName = "main_delivery.properties"

    prepareRun(shellSvn, propertiesSvn)

    withCredentials(............... {
        withEnv(...........){
            buildApp(svnRootUrl, propertiesDir, propertiesFileName)
        }    
    }    
}