Tuesday, February 10, 2015

Jenkins : duplicate jenkins enviromnent and apply SCM change on every job

Duplicate Continuous integration (Jenkins) enviromnent

Just a reminder to explain and speak about jenkins scripting console.

Our problem is :
  • we have many hundred maven projets
  • each project has its job in jenkins (project choice)
  • when preparing a new release we "branch" all sources in our SCM

question how can we duplicate jenkins environement ?

Cookbook

  • install a new jenkins instance 
  • install in this instance a plugin called Job import plugin
  • use this plugin and import all job from your initial instance
  • Then use the script console  ${host}/${Jenkins_inst_name}/script . This jenkins feature is very powerfull. It's allow to walk through all job and mofify them if needed. In our case we apply this groovy script (I start from this source wiki.jenkins-ci.org/display/JENKINS/Change+Version-Number+in+SVN-path)
import hudson.scm.*
hudsonInstance = hudson.model.Hudson.instance
overrideExistingValues = false
allItems = hudsonInstance.items
allItems.each { job-> println "Name : "+job.name;
if(!(job instanceof hudson.model.ExternalJob)) {
   if (job.scm instanceof SubversionSCM) {
     def newSvnPath = [][]
     println "SCM job : "+job.name;
     job.scm.locations.each{
       println "Scm location : "+it.remote;
       newRemote = it.remote
       newRemote = newRemote.replaceAll("franckys/svn/LOCAL/trunk",
        "franckys/svn/LOCAL/branches/preV2")
       newSvnPath.add(new hudson.scm.SubversionSCM.
        ModuleLocation(newRemote,it.local))
       println "Scm new location : "+newRemote;
     }
     newscm = new hudson.scm.SubversionSCM(newSvnPath,
        job.scm.workspaceUpdater, job.scm.browser,
     job.scm.excludedRegions, job.scm.excludedUsers, job.scm.excludedRevprop, 
        job.scm.excludedCommitMessages, job.scm.includedRegions)
     if (overrideExistingValues){
       job.scm = newscm;
     }
   }
 }
}
If you have interest in script you could have a look at : https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console

No comments:

Post a Comment