Bulk change subversion repository URL with jenkins-cli and Groovy code

Create the file changeSvnUrl.groovy with this content:

import hudson.scm.*

// Access to the Hudson Singleton
jenkinsInstance = jenkins.model.Jenkins.instance

// Retrieve all jobs
allJobs = jenkinsInstance.items

svnCredentialsId = "c8xxxee-965c-4cc0-b662-8a5xxx7d41b"

// iterate over jobs with Subversion as SCM
allJobs.each { job ->
    if (job.scm instanceof SubversionSCM) {
        if (job.scm.locations[0].remote.startsWith("http://oldhost/svn")) {
            oldUrl = job.scm.locations[0].remote
            newUrl = oldUrl.replace("http://oldhost/svn", "https://newhost/svn/project")

            println("changing Subversion URL in " + job.name + " from " + oldUrl + " to " + newUrl)

            job.scm = new hudson.scm.SubversionSCM(newUrl)
            job.scm.locations[0] = job.scm.locations[0].withCredentialsId(svnCredentialsId)
        }
    }
}

Execute:

java -jar jenkins-cli.jar -s http://jenkinshost/ groovy changeSvnUrl.groovy

Authenticate jenkins-cli with ssh-key

After adding the public key to your Jenkins user profile you can execute jenkins-cli with your ssh key:

$ java -jar jenkins-cli.jar -s http://hudson.hh.arvm.de/ -i ~/.ssh/jenkins_rsa who-am-i
Authenticated as: clsa1
Authorities:
  jenkins_admin

If you are using cygwin you have to transform the path like this:

java -jar jenkins-cli.jar -s http://hudson.hh.arvm.de/ -i $(cygpath -aw "/cygdrive/c/cygwin64/home/CLSA1/.ssh/jenkins_rsa")