如何获取触发我的多分支Jenkins构建的Github PR的标签列表

nnt7mjpx  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(147)

更准确地说,我需要得到触发我的多分支构建的PR的标签列表,这可能吗?
我知道https://github.com/jenkinsci/pipeline-github-plugin,但使用了Jenkins和多分支管道的不兼容版本。

lskq00tm

lskq00tm1#

经过一番调查,我发现2种方法获得公关标签列表。
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"
         }
     }

相关问题