4

I have more than 100 jobs in Jenkins and I have to change a Git URL in each and every job since we changed the git server. I must traverse each job and change the Git URL. Can anyone help me with a groovy script?

I was able to traverse each job, but not able to get the Git URL or change it:

import hudson.plugins.emailext.*
import hudson.model.*
import hudson.maven.*
import hudson.maven.reporters.*
import hudson.tasks.*

// For each project
for(item in Hudson.instance.items) {
 println("JOB : " + item.name);
}

I badly need help in this, please someone help me.

dan
  • 982
  • 8
  • 24
Hound
  • 837
  • 17
  • 31
  • The git instance that jenkin uses is confihured in one central place; jenkins' config page. Projects in which git is selected as SCM use that setting. – Amir Keibi Feb 08 '14 at 02:35

2 Answers2

7

The script below will modify all Git URL. You will need to fill the modifyGitUrl method. Script is written for Git plugin version 2.3.2. Check the git plugin source code to adjust it to the version you need e.g. the constructor parameters might have changed.

import hudson.plugins.git.*
import jenkins.*
import jenkins.model.*

def modifyGitUrl(url) {
  // Your script here
  return url + "modified"
}

Jenkins.instance.items.each {
  if (it.scm instanceof GitSCM) {
    def oldScm = it.scm
    def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
      new UserRemoteConfig(modifyGitUrl(it.url), it.name, it.refspec, it.credentialsId)
    }
    def newScm = new GitSCM(newUserRemoteConfigs, oldScm.branches, oldScm.doGenerateSubmoduleConfigurations,
                            oldScm.submoduleCfg, oldScm.browser, oldScm.gitTool, oldScm.extensions)
    it.scm = newScm 
    it.save()
  }
}
ceilfors
  • 2,617
  • 23
  • 33
1

I would have shut the server down and edited all the config.xml files with a script(sed/awk perl or something) and then restarted jenkins to load the new configurations.

If shutting down jenkins is not an option it is posible to get edit and post every config.xml with something like this

GET http://myserver/job/config.xml| sed s/oldurl/newurl/g |POST http://myserver/job/config.xml
Simson
  • 3,373
  • 2
  • 24
  • 38