如果未包含在脚本中,Jenkins 库步骤将失败
Jenkins library step fails if not wrapped in script
我遇到了一个我似乎不太明白的奇怪问题。我写了一个自定义 step
,它接受用于更轻松地克隆 github/bitbucket 存储库的参数。 step
工作正常 - 它为 branches
和 pr
调用适当的 checkout()
,但出于某种原因,这仅在您从 [=17= 调用它时才有效].如果您不使用带有超级奇怪异常的 script { }
将其包装起来,它就无法在声明性管道中工作:
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
]]
]
}
}
}
script{}
步骤采用一段脚本化管道(其中包含 Groovy 语言提供的功能)并在声明式管道中执行。
由于 gitUtils.cloneRepo(...)
是脚本化管道块,您需要使用 script{}
以便它可以嵌入到声明性管道步骤中。
我遇到了一个我似乎不太明白的奇怪问题。我写了一个自定义 step
,它接受用于更轻松地克隆 github/bitbucket 存储库的参数。 step
工作正常 - 它为 branches
和 pr
调用适当的 checkout()
,但出于某种原因,这仅在您从 [=17= 调用它时才有效].如果您不使用带有超级奇怪异常的 script { }
将其包装起来,它就无法在声明性管道中工作:
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
]]
]
}
}
}
script{}
步骤采用一段脚本化管道(其中包含 Groovy 语言提供的功能)并在声明式管道中执行。
由于 gitUtils.cloneRepo(...)
是脚本化管道块,您需要使用 script{}
以便它可以嵌入到声明性管道步骤中。