本文介绍了groovy列出工作中使用的GIT URL的Jenkins工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要打印在这些作业中配置的Jenkins作业URL和GIT URL.

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

例如:

假设我的Jenkins URL为: http://localhost:8080 &我的git URL是ssh://git:424

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

如果我从詹金斯(Jenkins)运行groovy代码,它应该返回:

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

http://localhost:8080/job_name1 | ssh://git:424/repo_name1(在job_name1的SCM部分中配置的GIT URL)

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(在job_name2的SCM部分中配置的GIT URL)

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

我有以下代码列出作业:

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

及以下代码以列出SCM值:

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"

以上代码首先返回Jenkins职位URL,然后返回SCM URl,但我必须手动将其映射为SCM属于哪个Jenkins职位URL.

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.

有没有办法,我可以使用Groovy打印Jenkins职位URL及其SCM值.

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

感谢帮助!

推荐答案

如果您使用的是WorkflowJob,则以下代码段将对您有用.

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

这篇关于groovy列出工作中使用的GIT URL的Jenkins工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 05:15