【问题标题】:How to get the list of labels of the Github PR that triggered my multibranch Jenkins build如何获取触发我的多分支 Jenkins 构建的 Github PR 的标签列表
【发布时间】:2019-01-27 18:17:55
【问题描述】:

更准确地说,我需要获取触发我的多分支构建的 PR 的标签列表。有可能吗?

我知道https://github.com/jenkinsci/pipeline-github-plugin,但使用了不兼容的 Jenkins 版本和多分支管道。

【问题讨论】:

    标签: jenkins github jenkins-pipeline multibranch-pipeline


    【解决方案1】:

    经过一番调查,我发现了两种获取 PR 标签列表的方法。

    1. 需要 Jenkins 凭据中的 github 用户配置,并且需要分配节点以通过 curl 发送 http 请求。
    def getLabels() {
    
        def labels
    
        def scmHead = jenkins.scm.api.SCMHead.HeadByItem.findHead(currentBuild.rawBuild.getParent())
        def owner = scmHead.getSourceOwner()
        def repo = scmHead.getSourceRepo()
        def prId = scmHead.getId()
    
        withCredentials([usernamePassword(credentialsId: 'GITHUB_CREDENTIALS_ID', usernameVariable: 'UUU', passwordVariable: 'PPP')]) {
            def json = sh(
                    script: "curl -u ${env.UUU}:${env.PPP} https://api.github.com/repos/${owner}/${repo}/issues/${prId}",
                    returnStdout: true
            )
    
            // requires https://plugins.jenkins.io/pipeline-utility-steps plugin
            def prInfo = readJSON(text: json)
    
            labels = prInfo.labels
        }
        return labels
    } 
    
    1. 通过https://github.com/jenkinsci/pipeline-github-plugin,需要升级 Jenkins 到 v2.128 或更高版本。
         if (env.BRANCH_NAME ==~ /PR-\d+/) {
             pullRequest.labels.each{
                echo "label: $it"
             }
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多