0

I am trying to print/list all our jenkins (Freestyle and Pipeline) Jobs separately along with SCM Details such as (Git URL & Branch details) using below groovy. I am able to list our freestyle & scripted pipeline jobs names separately.

import jenkins.model.*
import hudson.model.*
import hudson.triggers.*
import org.jenkinsci.plugins.workflow.job.*

println("--- Jenkins Pipeline jobs List ---")
Jenkins.getInstance().getAllItems(WorkflowJob.class).each() { println(it.fullName) };

println("\n--- Jenkins FreeStyle jobs List ---")
Jenkins.getInstance().getAllItems(FreeStyleProject.class).each() { println(it.fullName) };

println '\nDone.'

With the below groovy code i can able to print the both freestyle & pipeline Git URLs, But it is printing separately.

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"

Need help in listing/printing both Job name & Git URL along with respective Job.

user4948798
  • 1,924
  • 4
  • 43
  • 89

1 Answers1

0

If I run this code in Jenkins Script console,i see in output DSL scripted pipeline (Jenkinsfile) jobs as well

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)
    }

}
vasja
  • 11
  • 1
  • No it is not listing `pipeline script` based jobs. it listing only `freestyle jobs` note - `Script written in Jenkins GUI itself. not calling from Jenkinsfile` – user4948798 Jun 18 '20 at 02:50
  • In my case all jobs comming from source code system and is as a code Jenkinsfile. Im getting output: - Create infra ssh://git@github.com:user/jenkins.git - Deploy hello world ssh://git@github.com:user/jenkins.git etc – vasja Jun 29 '20 at 14:25
  • Vasja, Actually i don't have Jenkinsfile. In my Job itself groovy code written, So it is not printing/listing pipeline jobs git url in script console. Whereas the code given in my question through that i can able to print both freestyle & Pipeline jobs Git URLs. – user4948798 Jul 06 '20 at 08:08