如何从Jenkins多分支管道中获取所有github repos url



是否有办法列出或打印与多分支管道相关的github repo ?我能够找到一种方法来获得所有的多分支管道在一个詹金斯使用以下脚本通过这个链接https://support.cloudbees.com/hc/en-us/articles/226941767-Groovy-to-list-all-jobs?page=60在詹金斯控制台,并想知道是否有一种方法来修改这个脚本,并获得所有repos的列表。也请让我知道是否有一种方法做api调用来获取此信息。

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each {it ->
println it.fullName;
}

我希望下面的阐述能回答你问题的两个部分。如果不是这样,请告诉我。

回答第一部分的问题

要使用ScriptConsole实现您的目标,您可以简单地使用结果已获取的org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject对象。一看在JavaDoc告诉您item对象的类实现了jenkins.scm.api.SCMSourceOwner,使您能够使用getSCMSources()方法。

现在你有了一堆SCMSource对象,你可以迭代它们,试着找出它们能告诉你什么:

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each {it ->
it.getSCMSources().each { item ->
println item
}
}

在我的例子中输出是这样的:

jenkins.plugins.git.GitSCMSource{id='7afecbdb-fd8e-4ffe-80ff-f5a22921ee84'}
Result: [org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject@4ee06837[my-pipeline]]

或者像这样,当使用"GitHub"而不是"Git":

org.jenkinsci.plugins.github_branch_source.GitHubSCMSource{id='774291be-4a81-4558-98e6-0b35c84b1686'}
Result: [org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject@4ee06837[my-pipeline]]

第一行告诉我们SCMSource类的实际子类是jenkins.plugins.git.GitSCMSource它也有一个JavaDoc或org.jenkinsci.plugins.github_branch_source.GitHubSCMSourceJavaDoc。现在剩下的就相当简单了,只需使用这两种类型支持的getRemote方法并打印(或使用)远程配置。

Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject).each {it ->
it.getSCMSources().each { item ->
println "${it.name}: ${item.remote}"
}
}

现在的输出是:

my-pipeline: https://github.com/swesteme/jenkins-playground.git
Result: [org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject@4ee06837[my-pipeline]]

回答问题第二部分

免责声明:我自己从未使用过REST API,所以我只能给你一个可能的解决方案,这可能不是最好的方法。

以JSON形式获取作业列表

您需要的第一个信息(如上面的脚本方法)是在Jenkins作业列表中配置的作业列表。因此,要获得JSON格式的列表,请使用以下命令运行URL请求:http://localhost:8080/api/json?pretty=true

可能的结果如下所示:

{
"_class" : "hudson.model.Hudson",
"assignedLabels" : [
{
"name" : "master"
}
],
"mode" : "NORMAL",
"nodeDescription" : "the master Jenkins node",
"nodeName" : "",
"numExecutors" : 2,
"description" : null,
"jobs" : [
{
"_class" : "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject",
"name" : "my-pipeline",
"url" : "http://localhost:8080/job/my-pipeline/"
},
{
"_class" : "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name" : "Test-Job",
"url" : "http://localhost:8080/job/Test-Job/",
"color" : "blue"
}
],
"overallLoad" : {

},
"primaryView" : {
"_class" : "hudson.model.AllView",
"name" : "all",
"url" : "http://localhost:8080/"
},
"quietDownReason" : null,
"quietingDown" : false,
"slaveAgentPort" : 50000,
"unlabeledLoad" : {
"_class" : "jenkins.model.UnlabeledLoadStatistics"
},
"url" : "http://localhost:8080/",
"useCrumbs" : true,
"useSecurity" : true,
"views" : [
{
"_class" : "hudson.model.AllView",
"name" : "all",
"url" : "http://localhost:8080/"
}
]
}

您现在可以为_class属性等于org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject的所有作业过滤jobs数组。在本例中,这将留给我们以下数组(只有一个元素):

[ 
{
"_class" : "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject",
"name" : "my-pipeline",
"url" : "http://localhost:8080/job/my-pipeline/"
}
]

获取多分支作业的配置

对于每个结果作业,您现在可以通过调用job-url来查询配置,并与config.xml连接:http://localhost:8080/job/my-pipeline/config.xml

不幸的是,结果是相当丑陋的XML(因为您需要使用您选择的XML解析器库来解析它):

<?xml version='1.1' encoding='UTF-8'?>
<org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject plugin="workflow-multibranch@2.26">
<actions/>
<description></description>
<properties/>
<folderViews class="jenkins.branch.MultiBranchProjectViewHolder" plugin="branch-api@2.7.0">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</folderViews>
<healthMetrics/>
<icon class="jenkins.branch.MetadataActionFolderIcon" plugin="branch-api@2.7.0">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</icon>
<orphanedItemStrategy class="com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy" plugin="cloudbees-folder@6.16">
<pruneDeadBranches>true</pruneDeadBranches>
<daysToKeep>-1</daysToKeep>
<numToKeep>-1</numToKeep>
</orphanedItemStrategy>
<triggers/>
<disabled>false</disabled>
<sources class="jenkins.branch.MultiBranchProject$BranchSourceList" plugin="branch-api@2.7.0">
<data>
<jenkins.branch.BranchSource>
<source class="org.jenkinsci.plugins.github_branch_source.GitHubSCMSource" plugin="github-branch-source@2.11.3">
<id>774291be-4a81-4558-98e6-0b35c84b1686</id>
<apiUri>https://api.github.com</apiUri>
<repoOwner>swesteme</repoOwner>
<repository>jenkins-playground</repository>
<repositoryUrl>https://github.com/swesteme/jenkins-playground.git</repositoryUrl>
<traits>
<org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait>
<strategyId>1</strategyId>
</org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait>
<org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait>
<strategyId>1</strategyId>
</org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait>
<org.jenkinsci.plugins.github__branch__source.ForkPullRequestDiscoveryTrait>
<strategyId>1</strategyId>
<trust class="org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustPermission"/>
</org.jenkinsci.plugins.github__branch__source.ForkPullRequestDiscoveryTrait>
</traits>
</source>
<strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
<properties class="empty-list"/>
</strategy>
</jenkins.branch.BranchSource>
</data>
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
</sources>
<factory class="org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory">
<owner class="org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" reference="../.."/>
<scriptPath>Jenkinsfile</scriptPath>
</factory>
</org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject>

注意:输出的不同取决于您是否选择了"git"。或";GitHub"项目的SCM提供商。

最新更新