我们需要打印在这些作业中配置的Jenkins作业URL和GIT URL。
例如:
假设我的Jenkins URL为:http://localhost:8080,而我的git URL为ssh:// git:424
如果我从詹金斯运行groovy代码,它应该返回:
http://localhost:8080/job_name1 | ssh:// git:424 / repo_name1(在job_name1的SCM部分中配置的GIT URL)
http://localhost:8080/job_name2 | ssh:// git:424 / repo_name2(在job_name2的SCM部分中配置的GIT URL)
我有以下代码列出作业:
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。
有没有办法,我可以使用Groovy打印Jenkins职位URL及其SCM值。
感谢帮助!
答案 0 :(得分:1)
如果您使用的是WorkflowJob
,则下面的代码段应该适合您。
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());
}
}
}
}
答案 1 :(得分:0)
这对我有用:
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"
答案 2 :(得分:0)
这适用于经典作业和工作流程作业:
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)
}
}