4

Is there a way to get the scm git repo url string inside a Jenkins job with groovy?

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
wapgui
  • 742
  • 8
  • 10
  • Probably it is. Can You clarify the question a bit? Maybe add an example? – Opal Jul 14 '14 at 19:27
  • There is a git repository url defined in the build job scm. I only want to know this entry. Maybe something with job.scm.location. – wapgui Jul 15 '14 at 06:29
  • import jenkins.model.*; import hudson.model.*; import hudson.tasks.*; import hudson.plugins.git.*; for(project in Hudson.instance.items) { scm = project.scm; if (scm instanceof hudson.plugins.git.GitSCM) { println("SCM " + scm.toString() + " for project " + project); } } – wapgui Jul 15 '14 at 06:46

2 Answers2

6
import jenkins.model.*;
import hudson.model.*;
import hudson.tasks.*;
import hudson.plugins.git.*;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;

for(project in Hudson.instance.items) {
  scm = project.scm;
  if (scm instanceof hudson.plugins.git.GitSCM) {
    for (RemoteConfig cfg : scm.getRepositories()) {
      for (URIish uri : cfg.getURIs()) {
        println("SCM " + uri.toString() + " for project " + project);    
      }
    } 
  }  
}
wapgui
  • 742
  • 8
  • 10
2
Jenkins.instance.getAllItems(Job.class).each{
scm = it.scm;
project = it;
if (scm instanceof hudson.plugins.git.GitSCM) {
    scm.getRepositories().each{
        it.getURIs().each{
            println("SCM " + it.toString() + " in " + project);
        }
    }
}}