How to migrate all jobs based on one template to another one using Script Console?

Article ID:115002026832
2 minute readKnowledge base

Issue

  • As a Jenkins admin, I would like to migrate all jobs based on OLD_JOB_TEMPLATE to NEW_JOB_TEMPLATE. Besides, Job name (name2migrate) and one pf the properties (prop2migrate) should be transferred from OLD_JOB_TEMPLATE to NEW_JOB_TEMPLATE.

Resolution

See also:

Script

The following script would solve the described issue.

Notes:

1) Please make a backup of your $JENKINS_HOME before running it. 2) Overall - RunScripts (admin) permission is needed in order to run it.

import com.cloudbees.hudson.plugins.modeling.ModelList
import com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.InstanceFromJobTemplate
import com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.JobPropertyImpl

// Job Templates - Full Names
def NEW_JOB_TEMPLATE = 'SUPPORT-Team/Templates/NEW_JOB_TEMPLATE'
def OLD_JOB_TEMPLATE = 'SUPPORT-Team/Templates/OLD_JOB_TEMPLATE'
// Job Templates - Names
def OLD_JOB_TEMPLATE_NAME = 'OLD_JOB_TEMPLATE'
// Job Templates - Property JOB_NAME_PROP. It is the default parameters for jobs templates. IT CANNOT BE CHANGED
def JOB_NAME_PROP = 'name'
// Job Templates - Property - Others:
def NEW_JOB_TEMPLATE_PROP = 'Prop New'
def OLD_JOB_TEMPLATE_PROP = 'Prop Old'


def jenkins = Jenkins.instance
def InstanceFromJobTemplate itemIntFJTempl, newIntFJTempl  = null
def numberOfJobUpdated = 0
def name2migrate = ''
def prop2migrate = ''

//1. Checking that NEW_JOB_TEMPLATE and OLD_JOB_TEMPLATE are currently included in the instance's Model List
if (ModelList.get().getItem(OLD_JOB_TEMPLATE) != null && ModelList.get().getItem(NEW_JOB_TEMPLATE) != null) {
    // 2. Retrieving all the job items (ABSTRACT) from the current Jenkins instance. Use
    jenkins.getAllItems(Job.class).each{j ->
        itemIntFJTempl = InstanceFromJobTemplate.from(j)
        //3. Filtering job items based on templates and from those, the ones with the model name $OLD_JOB_TEMPLATE
        if ((itemIntFJTempl != null) && (itemIntFJTempl.model.name == OLD_JOB_TEMPLATE_NAME)) {
            //4. Checking that properties from $OLD_JOB_TEMPLATE exist
            if (itemIntFJTempl.getValue(JOB_NAME_PROP)!=null && itemIntFJTempl.getValue(OLD_JOB_TEMPLATE_PROP)!=null){
                //5. Getting parameters values from $OLD_JOB_TEMPLATE
                name2migrate = itemIntFJTempl.getValue(JOB_NAME_PROP)
                prop2migrate = itemIntFJTempl.getValue(OLD_JOB_TEMPLATE_PROP)
                //6. Create a new Instance from the Job Template $NEW_JOB_TEMPLATE
                newIntFJTempl = new InstanceFromJobTemplate(ModelList.get().getItem(NEW_JOB_TEMPLATE))
                //7. Assigning new template (6) to the job item
                j.addProperty(new JobPropertyImpl(newIntFJTempl))
                //8. Migrating parameters from $OLD_JOB_TEMPLATE to the $NEW_JOB_TEMPLATE instance
                // 8.1 Job Templates - Property JOB_NAME_PROP
                newIntFJTempl.setValue(JOB_NAME_PROP, name2migrate)
                try {
                    // 8.2 Job Templates - Property - Others
                    newIntFJTempl.setValue(NEW_JOB_TEMPLATE_PROP, prop2migrate)
                    //9. Saving the instance (6) in order changes take place
                    newIntFJTempl.save()
                    //10. Checking final value
                    itemIntFJTempl = InstanceFromJobTemplate.from(j)
                    println "[INFO] Job '${j.name}' has changed its template to '${itemIntFJTempl.model.name}' template\n\n"
                    numberOfJobUpdated++
                }catch (IllegalArgumentException e){
                    println "[ERROR] Any of the '$NEW_JOB_TEMPLATE' properties are not correct"
                }
                catch (Exception e){
                    println "[ERROR] "+ e.toString()
                }
            } else {
                println "[ERROR] Any of the '$OLD_JOB_TEMPLATE' properties are not correct"
            }
        } // end of step 3
    } // end of step 2
    println "[INFO] Total number of updated template-based jobs : $numberOfJobUpdated"
} else {
    println "[ERROR] The '$OLD_JOB_TEMPLATE' and/or '$NEW_JOB_TEMPLATE' are not included into the Model List"
}

Console Output

If it succeeds and assuming that this example has 3 jobs based on OLD_JOB_TEMPLATE

[INFO] Job 'Job Example 1'  has changed its template to 'NEW_JOB_TEMPLATE' template

[INFO] Job 'Job Example 2'  has changed its template to 'NEW_JOB_TEMPLATE' template

[INFO] Job 'Job Example 3'  has changed its template to 'NEW_JOB_TEMPLATE' template

[INFO] Total number of updated template-based jobs : 3

Tested product/plugin versions

The latest update of this article has been tested and verified with: