Code-your-life DevOps Jenkins

Updating Jenkins job's Git url

June 8, 2018

So today I had the task of figuring out how many jobs we have in our Jenkins server that was collecting code from our old, Git server (since the customer is migrating to an internal server). So I created this little script that lists Jenkins jobs with a specific SCM URL, it also handles folders recursively.

This was developed on a Jenkins 2.126, so if you are on another Jenkins version, some of this might not work, if you have any improvement’s feel free to make a merge request at jesstruck@github/jenkinsMaintinance (Yeah, I have not done the #movingtogitlab thing yet 😉 )

The intention of this script is to loop through all jobs on the server and print the job name and URL if the URL matches the $url_regex. This is intended for SCM configurations of type hudson.plugins.git.GitSCM.

If find a Folder object it recursively loops through this also.

If you are not looking for "", just change the url_regex, to a regular expression,
that matches your search criteria.

url_regex = ''

def checkBitbucker(job, scm){
    if ( == 'hudson.plugins.git.GitSCM') {
      if (scm.getUserRemoteConfigs() =~ /${url}/ ){
        println "Job '${}' uses the following git configuration"
          println "\tRemote:     ${scm.getUserRemoteConfigs()[0].getUrl()}"

def printJobInfo(job){
  if ( =='com.cloudbees.hudson.plugins.folder.Folder'){
    job.getAllJobs().each{ child ->
      //Recursively call it self every time it finds a Folder object
  }else if( =='org.jenkinsci.plugins.workflow.job.WorkflowJob'){
    job.getSCMs().each{ scm ->
        checkBitbucker(job, scm)
    checkBitbucker(job, job.scm)

hudson.model.Hudson.instance.items.each{ job ->

Tagged: #Jenkins #Jenkins-Maintenance #Groovy