1

We need to print Jenkins jobs URLs and GIT URL configured inside these jobs.

For example:

Assume my Jenkins URL is : http://localhost:8080 & my git URL is ssh://git:424

If i run groovy code from Jenkins, It should return:

http://localhost:8080/job_name1 | ssh://git:424/repo_name1 (GIT URL configured in SCM section of job_name1)

http://localhost:8080/job_name2 | ssh://git:424/repo_name2 (GIT URL configured in SCM section of job_name2)

I have below code to list jobs :

Jenkins.instance.getAllItems(AbstractProject.class).each {it ->
println it.fullName;
}

and below code to list SCM value:

Jenkins.instance.getAllItems(hudson.model.AbstractProject.class).each {it ->
  scm = it.getScm()
  if(scm instanceof hudson.plugins.git.GitSCM)
  {
    println scm.getUserRemoteConfigs()[0].getUrl()
  }
}
println "Done"

Above code first returns Jenkins job URLS and then SCM URl but i have to map it manually what SCM belongs to what Jenkins job URL.

Is there a way, i can print Jenkins job URL and its SCM value using groovy.

Appreciate help !

user3232823
  • 1,867
  • 3
  • 18
  • 27

3 Answers3

2

This works with classic jobs and workflow jobs:

import org.jenkinsci.plugins.workflow.job.WorkflowJob;

def printScm(project, scm){
    if (scm instanceof hudson.plugins.git.GitSCM) {
        scm.getRepositories().each {
            it.getURIs().each {
                println(project + "\t"+ it.toString());
            }
        }
    }
}

Jenkins.instance.getAllItems(Job.class).each {

    project = it.getFullName()
    if (it instanceof AbstractProject){
        printScm(project, it.getScm())
    } else if (it instanceof WorkflowJob) {
        it.getSCMs().each {
            printScm(project, it)
        }
    } else {
        println("project type unknown: " + it)
    }

}
froque
  • 442
  • 2
  • 13
1

If you are using a WorkflowJob then the below snippet should work for you.

Jenkins.instance.getAllItems(Job.class).each{
scm = it.getTypicalSCM();
project = it.getAbsoluteUrl();
if (scm instanceof hudson.plugins.git.GitSCM) {
scm.getRepositories().each{
    it.getURIs().each{
        println(project.toString() +":"+ it.toString());
    }
  }
 }
}
Ram
  • 1,154
  • 6
  • 22
  • thanks ! Tried it. Getting following error. Any suggestions: hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.job.WorkflowJob.getScm() is applicable for argument types: () values: [] Possible solutions: getSCMs(), getACL(), getACL(), getACL(), getApi(), getName() – user3232823 Nov 09 '18 at 14:14
0

This worked for me:

Jenkins.instance.getAllItems(Job.class).each {
  scm = it.getScm();
  project = it.getAbsoluteUrl();

  if (scm instanceof hudson.plugins.git.GitSCM) {
    scm.getRepositories().each {
      it.getURIs().each {
          println(project.toString() +":"+ it.toString());
      }
    }
  }
}
println "done"
Agung Pratama
  • 3,666
  • 7
  • 36
  • 77