Groovy Script for Jenkins Script Console to Rename the Subversion Host Name

Share on:

The host name of a Subversion instance is renamed and now many Jenkins jobs have to be adjusted. One possibility is to change every job by hand. But this approach is very time-consuming and error-prone. A better possibility is to have a script based approach, that rename the Subversion host name of all jobs in one go. Jenkins has a feature, that helps us thereby. Jenkins has a script console to run Groovy script on Jenkins server [1]. Below you can see a Groovy script  that renames the Subversion host name in all jobs.

 1def oldHostName = "old.hostname.com"
 2def newHostName = "new.hostname.com"
 3
 4Hudson.instance.items.each { item ->
 5
 6  if(item.scm instanceof hudson.scm.SubversionSCM) {
 7    println("JOB : "+item.name)
 8
 9    def newLocations = new ArrayList<hudson.scm.SubversionSCM.ModuleLocation>()
10
11    item.scm.locations.each {location ->
12
13      println("SCM Location Remote : " + location.remote)
14      def newRemoteUrl = location.remote.replaceFirst(oldHostName, newHostName)
15
16      newLocations.add(new hudson.scm.SubversionSCM.ModuleLocation(newRemoteUrl, location.local, location.depthOption,location.ignoreExternalsOption))
17    }
18
19    def newScm = new hudson.scm.SubversionSCM(newLocations, item.scm.workspaceUpdater,
20    item.scm.browser, item.scm.excludedRegions, item.scm.excludedUsers, item.scm.excludedRevprop, item.scm.excludedCommitMessages,
21    item.scm.includedRegions, item.scm.ignoreDirPropChanges, item.scm.filterChangelog)
22
23    newScm.locations.each { location ->
24      println("New SCM Location Remote : " + location.remote)
25    }
26
27    item.scm = newScm
28    item.save()
29    println("\n=======\n")
30  }
31}

This script is tested with Jenkins version 1.534 and Jenkins Subversion Plugin version 1.53.

[1] Jenkins Script Console