在Jenkins参数中列出所有存储库分支

wfauudbj  于 2023-03-07  发布在  Jenkins
关注(0)|答案(2)|浏览(132)

我正在尝试为Jenkins作业添加Active choice参数来填充所有的github分支。我已经在Jenkins凭据中添加了个人访问令牌并尝试使用它。到目前为止,我已经尝试了下面的代码片段。

def gettags = ("git ls-remote https://<username>@github.com/<username>/first-project.git").execute()
return gettags.text.readLines().collect { 
 it.split()[1].replaceAll('refs/heads/', '').replaceAll('refs/tags/', '').replaceAll("\\^\\{\\}", '')
}

但是,它不起作用,所以我尝试在jenkins脚本中执行,它一直运行,当我尝试运行第一行时,它给我Result: Process[pid=17092, exitValue="not exited"],有人能帮助我吗?

ybzsozfc

ybzsozfc1#

用GitHub API代替怎么样?

def accessToken = "ACCESS_TOKEN".bytes.encodeBase64().toString()
def get = new URL("https://api.github.com/repos/ORG/REPO/branches").openConnection();
get.setRequestProperty("authorization", "Basic " + accessToken)

def content = get.getInputStream().getText()

def jsonSlurper = new groovy.json.JsonSlurper()
def object = jsonSlurper.parseText(content)
return object["name"]
7jmck4yq

7jmck4yq2#

    • 我得到了答案**,实际上,作为用户名,我使用的是我设置为Jenkins凭据的用户ID,而不是实际的GitHub用户名,所以我替换了该用户名,groovy脚本运行正常。

谢谢你。

相关问题