How to change a job template instance parameter using a groovy script?

Issue

I have a job template (named job_template) with a parameter param. From this template I create a job instance (named job_instance) with the value X for param. I want to replace X by Y using a script.

Environment

  • CloudBees Jenkins Enterprise
  • CloudBees Template Plugin

Resolution

Here is the groovy script to launch in the Jenkins console (for example) to do the change

// Let's take the job to update
def job = Jenkins.instance.getItem('job_instance')
println "Updating job: ${job.fullName}"
// Let's find the instance object used to link the job to its template
def instance = com.cloudbees.hudson.plugins.modeling.impl.entity.EntityInstance.from(job)

println "* Current value of parameter is : ${instance.getValue("param")}"
// Let's update
instance.setValue("param","Y")
instance.save()
println "* New value of parameter is : ${instance.getValue("param")}"
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.