for循环中的Job-DSL参数
我正在用Job-DSL动态在Jenkins上创建我的工作, 我希望每个地图项目都有自己的参数,所以我尝试做类似的事情:
def jobs = [
[
name: "query-es-statistics",
repo_name: "https://github.com/spotinst/shared-libraries.git",
branch: "master",
scriptPath: "jobs/query-es-statistics.Jenkinsfile",
parameters: {->
choiceParam(
'ENV_NAME',
[ 'dev', 'prod' ],
'This param is required to understand what role to use with the running agent'
)
}
]
]
for (job_conf in jobs) {
pipelineJob(job_conf.name) {
properties {
disableConcurrentBuilds {}
}
parameters {
job_conf.parameters()
}
definition {
cpsScm {
lightweight(true)
scm {
git {
branch(job_conf.branch)
remote {
credentials('github-ci-user')
url(job_conf.repo_name)
}
}
}
scriptPath(job_conf.scriptPath)
}
}
}
}
当然,除了参数外,一切都在起作用,我遇到了此错误:
ERROR: (script, line 10) No signature of method: script.choiceParam() is applicable for argument types: (java.lang.String, java.util.ArrayList, java.lang.String) values: [ENV_NAME, [dev, prod], This param is required to understand what role to use with the running agent]
也许我缺少简单的groovy?
I'm creating my jobs on Jenkins dynamically with job-dsl,
I want every Map item to have its own parameters, so I tried to do something like that:
def jobs = [
[
name: "query-es-statistics",
repo_name: "https://github.com/spotinst/shared-libraries.git",
branch: "master",
scriptPath: "jobs/query-es-statistics.Jenkinsfile",
parameters: {->
choiceParam(
'ENV_NAME',
[ 'dev', 'prod' ],
'This param is required to understand what role to use with the running agent'
)
}
]
]
for (job_conf in jobs) {
pipelineJob(job_conf.name) {
properties {
disableConcurrentBuilds {}
}
parameters {
job_conf.parameters()
}
definition {
cpsScm {
lightweight(true)
scm {
git {
branch(job_conf.branch)
remote {
credentials('github-ci-user')
url(job_conf.repo_name)
}
}
}
scriptPath(job_conf.scriptPath)
}
}
}
}
Of course everything is working except for the parameters, where I get this error:
ERROR: (script, line 10) No signature of method: script.choiceParam() is applicable for argument types: (java.lang.String, java.util.ArrayList, java.lang.String) values: [ENV_NAME, [dev, prod], This param is required to understand what role to use with the running agent]
Maybe it's simple groovy I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是您的调用
job_conf.parameters()
执行了闭包,但不是在参数方法的上下文中,而是独立的,即使用了错误的委托。parameters
是一个接受单个闭包作为参数的方法,因此您应该将该闭包传递给该方法,而不是结果。试试这个:
The problem here is that your call
job_conf.parameters()
executes the closure, but not in the context of the parameters method, but standalone, i.e. with the wrong delegate.parameters
is a method that accepts a single closure as parameter, so you should pass that closure, not the result to the method.Try this: