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

Leave a comment