[Proposal] Passing arguments to job-dsl via the pipeline jobDsl step

3,634 views
Skip to first unread message

vqrs

unread,
May 8, 2017, 5:13:41 AM5/8/17
to job-dsl-plugin
I would like to be able to pass values from the shared pipeline libraries for use in my Job DSL script.

I propose the following extension to the jobDsl step:


node {
    jobDsl targets: ['jobs/projectA/*.groovy', 'jobs/common.groovy'].join('\n'),
           removedJobAction: 'DELETE',
           removedViewAction: 'DELETE',
           lookupStrategy: 'SEED_JOB',
           params: [param1: 'test', param2: myExpr]
}


I have identified that I would need to change AbstractDslScriptLoader.createBinding()[1], and get the values to there from within perform()[2] and add a new @DataBoundSetter for the params.

Maybe a switch to have jobDsl print all effective global variables would also be helpful for debugging purposes.

What do you think?




vqrs

unread,
May 8, 2017, 12:52:24 PM5/8/17
to job-dsl-plugin
I have created a ticket to track this:


Also, I went ahead and implemented a version of this:


I changed "params" to "additionalParameters". Not sure where to discuss this now, Jira, GitHub or here :)

Ravitej Aluru

unread,
Nov 16, 2017, 1:28:33 PM11/16/17
to job-dsl-plugin
Hi,

How would I access the vars set in additionalParameters in the DSL? 

For example, I set additionalParameters: ['FOLDER': 'My Folder Name, 'JOB': 'My Job Name'] when calling the jobDsl step in a pipeline script. I tried using the FOLDER and JOB vars in my DSL like folder (FOLDER), but it complains No such property: FOLDER for class: seed_products

Many thanks,
Ravitej

Ravitej Aluru

unread,
Nov 16, 2017, 1:30:28 PM11/16/17
to job-dsl-plugin
I am using job dsl plugin version 1.66, by the way which I believe is the latest version. 

Ravitej Aluru

unread,
Nov 17, 2017, 10:27:02 AM11/17/17
to job-dsl-plugin
OK. Looking at the tests for job-dsl-plugin, it is clear that accessing the additionalParameters is just like any other global variable. 
def 'execute with additional parameters'() {
        setup:
        String value = 'Hello World!'
        FreeStyleProject job = jenkinsRule.createFreeStyleProject('seed')
        ExecuteDslScripts scripts = new ExecuteDslScripts(scriptText: 'println FOO', additionalParameters: [FOO: value])
        job.buildersList.add(scripts)

        when:
        FreeStyleBuild freeStyleBuild = job.scheduleBuild2(0).get()

        then:
        freeStyleBuild.result == SUCCESS
        freeStyleBuild.getLog(100).contains(value)

But it doesn't seem to work when using Jenkins 2.73.2 and Job DSL 1.66

Lee Meador

unread,
Nov 17, 2017, 3:37:34 PM11/17/17
to job-dsl-plugin
I am using Jenkins 2.46.3 and job dsl plugin 1.66. I can pass in parameters but the syntax doesn't match the example in the first post in this thread. That shows the name "params" but I used as in the example below with "additionalParameters"

additionalParameters: [
githubUrl : 'https://github.mycompany.com/orginization1/PDE_ONE',
gitCredential: 'GHE-Hook-Credential',
buildNumber : "${env.BUILD_NUMBER}",
]
Message has been deleted

Ravitej Aluru

unread,
Nov 20, 2017, 8:52:06 AM11/20/17
to job-dsl-plugin
Hi Lee,

Thank you for your reply. That's how I was passing in the additional parameters. See my code below showing passing them in and also using them. Does Script Security somehow play a role in this? I know additonalClasspath doesn't work when Script Security is enabled for DSL scripts.

stage ('Create seed jobs') {
def additionalParams = [:]
additionalParams.put('folderName', 'IFE Products Seed Jobs')
node{
deleteDir()
unstash 'code'
//assume 'products' is a valid data structure.
products.IFE.Products.each { p ->
def jobName
p.Versions.Active.each { ar ->
echo "${p.Name} (${p.ShortName}) version ${ar.Version} is actively supported"
//set job name, description, display name according to product and version info.
additionalParams.put('jobName', "${p.ShortName}-${ar.Version}-seed-job")
additionalParams.put('jobDisplayName', " Seed Job for ${p.ShortName} ${ar.Version}")
echo "additionalParameters = ${additionalParams}"
jobDsl targets: "jenkins/seed/seed_ife_product_${p.ShortName.toLowerCase()}.groovy",
removedJobAction: 'DELETE'
additionalParameters: additionalParams
}
p.Versions.Deprecated.Unsupported.each {usp ->
echo "${p.Name} (${p.ShortName}) version ${usp} is unsupported"
}
p.Versions.Deprecated.Supported.each { sp ->
echo "${p.Name} (${p.ShortName}) version ${sp.Version} is passively supported until ${sp.EndDate}"
}
}
}
}

seed_ife_product_scr.groovy
folder(folderName)
pipelineJob("${folderName}/${jobName}") {
description('This seed job generates all the relevant jobs for onboarding, releasing and testing an IFE product depending on the chosen ENVIRONMENT and END_CLIENT')
displayName (jobDisplayName)
definition {
cpsScm {....
}
}
}

Ravitej Aluru

unread,
Nov 21, 2017, 10:11:05 AM11/21/17
to job-dsl-plugin
OK. Found it. For everyone's benefit, I am posting it here at the cost of my own embarrassment in front of the crowd. Don't forget commas!! Of course the additionalParameters are not passed into the DSL script. But, it is very very annoying that Jenkins (or the plugin) doesn't complain about the missing comma.

Anyone here aware of some compiling tool for Jenkins pipeline scripts? Do groovy compile tools (groovyc, gradle etc.) work for pipeline scripts?

jobDsl targets: "jenkins/seed/seed_ife_product_${p.ShortName.toLowerCase()}.groovy",
removedJobAction: 'DELETE',
additionalParameters: additionalParams
Reply all
Reply to author
Forward
0 new messages