for 循环中的作业 DSL 参数

Job-DSL parameters in a for loop

我正在使用 job-dsl 在 Jenkins 上动态创建我的工作, 我希望每个 Map 项都有自己的参数,所以我尝试这样做:

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我不见了?

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

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

试试这个:

parameters(job_conf.parameters)