更准确地说,我需要获取触发多分支构建的PR的标签列表。有可能吗?
我知道https://github.com/jenkinsci/pipeline-github-plugin,但是使用了不兼容的Jenkins版本和多分支管道。
答案 0 :(得分:0)
经过一番调查,我发现了两种获取PR标签列表的方法。
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
}
if (env.BRANCH_NAME ==~ /PR-\d+/) {
pullRequest.labels.each{
echo "label: $it"
}
}