SKM IT World

Just another blog about IT

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

4 Comments


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.


def oldHostName = "old.hostname.com"
def newHostName = "new.hostname.com"

Hudson.instance.items.each { item ->

  if(item.scm instanceof hudson.scm.SubversionSCM) {
    println("JOB : "+item.name)

    def newLocations = new ArrayList<hudson.scm.SubversionSCM.ModuleLocation>()

    item.scm.locations.each {location ->

      println("SCM Location Remote : " + location.remote)
      def newRemoteUrl = location.remote.replaceFirst(oldHostName, newHostName)

      newLocations.add(new hudson.scm.SubversionSCM.ModuleLocation(newRemoteUrl, location.local, location.depthOption,location.ignoreExternalsOption))
    }

    def newScm = new hudson.scm.SubversionSCM(newLocations, item.scm.workspaceUpdater,
    item.scm.browser, item.scm.excludedRegions, item.scm.excludedUsers, item.scm.excludedRevprop, item.scm.excludedCommitMessages,
    item.scm.includedRegions, item.scm.ignoreDirPropChanges, item.scm.filterChangelog)

    newScm.locations.each { location ->
      println("New SCM Location Remote : " + location.remote)
    }

    item.scm = newScm
    item.save()
    println("\n=======\n")
  }
}

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

Links

[1] Jenkins Script Console

Advertisements

4 thoughts on “Groovy Script for Jenkins Script Console to Rename the Subversion Host Name

  1. Pingback: Groovy Script for Jenkins Script Console to Add or Replace Subversion Repository Browser | SKM IT World

  2. Pingback: Groovy Script for Jenkins Script Console to Add or Modify Discard Old Builds Setting | SKM IT World

  3. Hi,
    I have a question – could you show how to change SVN URL’s in all Jenkins Jobs?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s