Home > Software design >  Job-DSL parameters in a for loop
Job-DSL parameters in a for loop

Time:04-12

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?

CodePudding user response:

The problem here is that your call job_conf.parameters() executes the closure, nur 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)
  • Related