How to change a Git URL in all Jenkins jobs

7.8k Views Asked by At

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.

3

There are 3 best solutions below

0
Simson On

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
1
ceilfors On

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()
  }
}
0
Marslo On

base on the answer of @ceilfors, the it.scm = newScm not working for pipeline ( WorkflowJob ), It can be achieved by new a CpsScmFlowDefinition ( sample as below )

    Boolean orgLightweight = job.definition?.lightweight

    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
                           )
    CpsScmFlowDefinition flowDefinition = new CpsScmFlowDefinition( newScm, job.definition.scriptPath )

    job.definition = flowDefinition
    job.definition.lightweight = orgLightweight
    job.save()

here are full script ( can be found in gist/update-jenkins-scm.groovy as well ):

#!/usr/bin/env groovy

import hudson.plugins.git.GitSCM
import hudson.plugins.git.UserRemoteConfig
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition
import com.cloudbees.plugins.credentials.common.StandardCredentials
import com.cloudbees.plugins.credentials.CredentialsProvider

// .. to modify according your situation ..
String newCredId = 'ED25519_SSH_CREDENTIAL'

if ( CredentialsProvider.lookupCredentials( StandardCredentials.class, jenkins.model.Jenkins.instance)
                        .any { newCredId == it.id }
) {

  jenkins.model.Jenkins.instance.getAllItems( WorkflowJob.class ).findAll {
    it.definition instanceof org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition &&
    ! it.definition?.scm?.userRemoteConfigs.collect { it.credentialsId }.contains( newCredId )
  }.each { job ->

    GitSCM orgScm          = job.definition?.scm
    Boolean orgLightweight = job.definition?.lightweight

    List<UserRemoteConfig> newUserRemoteConfigs = orgScm.userRemoteConfigs.collect {
      // .. to modify according your situation ..
      newUrl = ...
      new UserRemoteConfig( newUrl, it.name, it.refspec, newCredId )
    }
    GitSCM newScm = new GitSCM( newUserRemoteConfigs, orgScm.branches, orgScm.doGenerateSubmoduleConfigurations,
                                orgScm.submoduleCfg, orgScm.browser, orgScm.gitTool, orgScm.extensions
                              )
    CpsScmFlowDefinition flowDefinition = new CpsScmFlowDefinition( newScm, job.definition.scriptPath )

    job.definition = flowDefinition
    job.definition.lightweight = orgLightweight
    job.save()

    println ">> " + job.fullName + " DONE !"
  }

} else {
  println "${newCredId} CANNOT be found !!"
}

"DONE"