Answer a question

I'm trying to create a DSL job that will create jobs from a list of maps iterating throughout them like the following:

def git_branch = "origin/awesomebranch"
def credential_id = "awesomerepocreds"
def jobs = [
  [
    title: "AwesomePipeline",
    description: "This pipeline is awesome.",
    directory: "awesomepath",
    repo: "ssh://git@bitbucket.XXX.XX.XX:XXXX/repo/repo.git"
  ]
]

jobs.each { i ->
    pipelineJob(i.title) {
        description("${i.description}\n\n__branch__: ${git_branch}")
        parameters {
            stringParam('branch', defaultValue='origin/develop', description='Branch to build')
        }
        definition {
            cpsScm {
                scm {
                    git {
                        branch('$branch')
                        remote {
                            url(i.repo)
                            credentials(credential_id)
                        }
                    }
                    scriptPath("jenkins/${i.directory}/Jenkinsfile")
                }
            }
        }
    }
}

For jobs without parameters this works great but I don't know how to pass a list into the map of a job that will be used by the parameters block, something like

....
def jobs = [
  [
    title: "AwesomePipeline",
    description: "This pipeline is awesome.",
    directory: "awesomepath",
    repo: "ssh://git@bitbucket.XXX.XX.XX:XXXX/repo/repo.git",
    params: [
        stringParam('branch', defaultValue='origin/develop', description='Branch to build'),
        stringParam('sawesomeparam', defaultValue='awesomevalue', description='awesomething')
    ]
  ]
]
...

That might be used someway as some sort of each but not sure how to formulate this properly.

....
jobs.each { i ->
    pipelineJob(i.title) {
        description("${i.description}\n\n__branch__: ${git_branch}")
        parameters {
            i.params.each { p ->
                p
            }
        }
....

Thanks in advance

Answers

This is a relatively old question, and I came across this a few days ago. This is how I ended up solving this.

First we need a class that has a static context method, that (I think) gets called by Groovy. This context is then later reused to actually add the parameters. I suppose you should use the additionalClasspath feature of job-dsl plugin to separate the Params class into a separate file so you can re-use it.

import javaposse.jobdsl.dsl.helpers.BuildParametersContext
class Params {
    protected static Closure context(@DelegatesTo(BuildParametersContext) Closure params) {
        params.resolveStrategy = Closure.DELEGATE_FIRST
        return params
    }

    static Closure extend(Closure params) {
        return context(params)
    }
}

Then you can setup your job using a params attribute that contains a list of closures.

def jobs = [
    [
        name: "myjob",
        params: [
            {
                stringParam('ADDITIONAL_PARAM', 'default', 'description')
            }
        ]
    ]
]

Lastly, you can call the parameters property using the static extend of above class.

jobs.each { j ->
    pipelineJob(j.name) {
        // here go all the default parameters
        parameters {
            string {
                name('SOMEPARAM')
                defaultValue('')
                description('')
                trim(true)
            }
        }
        j.params.each { p ->
            parameters Params.extend(p)
        }

        // additional pipelineJob properties...
    }
}

The reason I made the params attribute of the map into a list, is because this way you can have additional methods that return Closure that contain additional params. For example:

class DefaultParams {
    static Closure someParam(String valueDefault) {
        return {
            string {
                name('SOME_EXTRA_PARAM')
                defaultValue(valueDefault)
                description("It's a description bonanza")
                trim(true)
            }
        }
    }
}

def myjobs = [
    [
        name: "myjob",
        params: [
            defaultParams.someParam('default')
            {
                stringParam('ADDITIONAL_PARAM', 'default', 'description')
            }
        ]
    ]
]

You should be able to use all the different syntaxes that is showing on the job-dsl api page.

Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐