groovy 列出具有作业中使用的 GIT URL 的 Jenkins 作业 [英] groovy to list Jenkins jobs with GIT URL used in jobs
问题描述
我们需要打印这些作业中配置的 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 作业 URLS,然后是 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.
感谢帮助!
推荐答案
这适用于经典作业和工作流作业:
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 + " "+ 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)
}
}
这篇关于groovy 列出具有作业中使用的 GIT URL 的 Jenkins 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!