How to parametrize job-dsl code blocks e.g. permissions

Viewed 266

I would like to create several jobs that differ by some properties. Plain string properties work just fine, like in the example here https://github.com/sheehan/job-dsl-gradle-example/blob/master/src/jobs/example4Jobs.groovy

Additionally I would like to parametrize the permissions inside the authorization block.

I created a closure (auth) and wanted to invoke it inside the job definition but that doesn't work.

[
    [
        environment  : 'qa',
        auth: {
            permissions('dev', [
                'hudson.model.Item.Read'
            ])
        }
    ],
    [
        environment  : 'prod',
        auth: {
            permissions('admin', [
                'hudson.model.Item.Read',
                'hudson.model.Item.Build',
                'hudson.model.Item.Cancel'
            ])
        }
    ]
].each { Map config ->
    pipelineJob("job-${config.environment}") {
        displayName("job ${config.environment}")
        authorization {
            config.auth()
        }
    }
}

I'm getting the following error:

ERROR: (script, line 5) No signature of method: script.permissions() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [admin, [hudson.model.Item.Read]]
1 Answers

every closure in a job dsl declaration has a delegate object. Hence, if you pass that object as parameter in your closure you can use it to invoke that method.

[
    [
        environment  : 'qa',
        auth: { authorization ->
            authorization.permissions('dev', [
                'hudson.model.Item.Read'
            ])
        }
    ],
    [
        environment  : 'prod',
        auth: { authorization ->
            authorization.permissions('admin', [
                'hudson.model.Item.Read',
                'hudson.model.Item.Build',
                'hudson.model.Item.Cancel'
            ])
        }
    ]
].each { Map config ->
    pipelineJob("job-${config.environment}") {
        displayName("job ${config.environment}")
        authorization {
            config.auth(delegate)
        }
    }
}
Related