for循环中的Job-DSL参数

发布于 2025-01-19 14:09:24 字数 1555 浏览 3 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2025-01-26 14:09:24

这里的问题是您的调用 job_conf.parameters() 执行了闭包,但不是在参数方法的上下文中,而是独立的,即使用了错误的委托。

parameters 是一个接受单个闭包作为参数的方法,因此您应该将该闭包传递给该方法,而不是结果。

试试这个:

parameters(job_conf.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:

parameters(job_conf.parameters)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文