1

I have a jenkins master with loads of pipeline (workflow) jobs. I need to change the scm git repo names in all the jobs using a groovy script run via script console.

I came across a bunch of answers for it to work in freestyle jobs. Also i came across a solution for pipeline job as this.

groovy to list Jenkins jobs with GIT URL used in jobs

It just lists downs the job names and the git repos.

I want to modify the git scm. Any suggestions?

Jenisha
  • 225
  • 1
  • 2
  • 13

1 Answers1

1

The Git remote URL is set with the src/main/java/hudson/plugins/git/UserRemoteConfig.java#UserRemoteConfig class, and its groovy GUI configuration.

You can see an example of changing the URL in "How to change a Git URL in all Jenkins jobs"

def newUserRemoteConfigs = oldScm.userRemoteConfigs.collect {
  new UserRemoteConfig(modifyGitUrl(it.url), it.name, it.refspec, it.credentialsId)
}

The OP reports a more up-to-date way to update the Git SCM URL, in "How to update job config files using the REST API and cURL?"

# Get current config
curl -X GET http://developer:developer@localhost:8080/job/test/config.xml -o mylocalconfig.xml

# Post updated config
curl -X POST http://developer:developer@localhost:8080/job/test/config.xml --data-binary "@mymodifiedlocalconfig.xml"

Obviously, replace:

  • developer:developer with your username:password
  • localhost:8080 with your Jenkins URL
  • test with your job name

This is based on the JENKINS Remote access API.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This doesn't work for the latest git version though. It errors out early at the line ```def oldScm = it.scm``` – Jenisha Dec 17 '18 at 06:53
  • I found a easier way to update it thru api. https://support.cloudbees.com/hc/en-us/articles/218353308-How-to-update-job-config-files-using-the-REST-API-and-cURL- thanks @VonC – Jenisha Dec 17 '18 at 06:54
  • @Jenisha I have included your comment in the answer for more visibility. – VonC Dec 17 '18 at 07:33