2

我有一个奇怪的问题,我似乎不太明白。我编写了一个自定义step,它接受用于更轻松地克隆 github/bitbucket 存储库的参数。工作得step很好——它调用了适当checkout()的 forbranchesprs,但由于某种原因,这只有在你从 a 调用它时才有效script { gitUtils.cloneRepo(...) }。如果您不使用script { }带有超级奇怪异常的 a 包裹它,它在声明性管道中不起作用:

WorkflowScript: 25: Expected a symbol @ line 25, column 17.
               gitUtils().getCredentials(repo)
               ^

WorkflowScript: 26: Expected a symbol @ line 26, column 17.
               gitUtils().cloneRepo(url: repo)
               ^

WorkflowScript: 27: Expected a symbol @ line 27, column 17.
               gitUtils().getRevision()
               ^

WorkflowScript: 26: Invalid parameter "url", did you mean "message"? @ line 26, column 38.
               gitUtils().cloneRepo(url: repo)
                                    ^

WorkflowScript: 27: Missing required parameter: "message" @ line 27, column 17.
               gitUtils().getRevision()

任何想法为什么会发生这种情况?

import java.lang.IllegalArgumentException

def call() {
    return this
}

def cloneRepo(Map parameters = [url: null, branch: "master", credentials: null]) {
    def url = parameters.getOrDefault("url", null)
    def branch = parameters.getOrDefault("branch", "master")
    def credentials = parameters.getOrDefault("credentials", null)

    script {
        if(!url) {
            throw new IllegalArgumentException("cloneRepo() expects url argument to be present!")
        }

        if(credentials == null) {
            credentials = getCredentials(url)
        }

        if (branch.matches("\\d+") || branch.matches("PR-\\d+")) {
            if (branch.matches("PR-\\d+")) {
                branch = branch.substring(3)
            }
            checkout changelog: false, poll: false, scm: [
                    $class: 'GitSCM',
                    branches: [[name: 'pr/' + branch]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'LocalBranch', localBranch: 'pr/' + branch]],
                    submoduleCfg: [],
                    userRemoteConfigs: [[
                                                credentialsId: credentials,
                                                refspec: 'refs/pull/' + branch + '/head:pr/' + branch,
                                                url: url
                                        ]]
            ]
        } else {
            checkout changelog: false, poll: false, scm: [
                    $class: 'GitSCM',
                    branches: [[name: branch]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [],
                    submoduleCfg: [],
                    userRemoteConfigs: [[
                                                credentialsId: credentials,
                                                url: url
                                        ]]
            ]
        }
    }
}
4

1 回答 1

3

script{}步骤采用 Scripted Pipeline 块(其中包含 Groovy 语言提供的功能)并在声明式管道中执行该块。

由于gitUtils.cloneRepo(...)是脚本化的管道块,您需要使用script{}它以便将其嵌入到声明性管道步骤中。

于 2018-05-17T03:41:50.227 回答