groovy to list Jenkins jobs with GIT URL used in jobs

前端 未结 3 551
春和景丽
春和景丽 2021-01-28 18:02

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 s

相关标签:
3条回答
  • 2021-01-28 18:32

    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)
        }
    
    }
    
    0 讨论(0)
  • 2021-01-28 18:33

    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());
        }
      }
     }
    }
    
    0 讨论(0)
  • 2021-01-28 18:53

    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"
    
    0 讨论(0)
提交回复
热议问题