【问题标题】:how to fetch all github repos url from Jenkins multibranch pipelines如何从 Jenkins 多分支管道中获取所有 github repos url
【发布时间】:2021-11-23 09:40:52
【问题描述】:

有没有办法列出或打印与多分支管道关联的 github 存储库。我能够通过 Jenkins 控制台中的此链接 https://support.cloudbees.com/hc/en-us/articles/226941767-Groovy-to-list-all-jobs?page=60 找到使用以下脚本在 jenkins 中获取所有多分支管道的方法,并且想知道是否有办法修改此脚本并获取所有 repos 的列表。 另外请让我知道是否有办法通过 api 调用来获取此信息。

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

【问题讨论】:

    标签: jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli


    【解决方案1】:

    我希望以下详细说明能够回答您问题的两个部分。如果不是这样,请告诉我。

    回答您问题的第 1 部分

    要使用 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 其中还有一个 JavaDocorg.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/"
      }
    ]
    

    获取多分支作业的配置

    对于每个生成的作业,您现在可以通过调用作业 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 提供程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多