首页 > 解决方案 > 使用 Groovy 脚本列出具有活动选择的分支

问题描述

我正在尝试使用主动选择插件列出参数化 Jenkins 作业的 github 分支。我尝试了以下 groovy 脚本,但无济于事 - 下拉列表中未检索/列出任何分支。

1.

def process = ("ssh-agent bash -c 'ssh-add /home/ubuntu/.ssh/id_rsa; git ls-remote -t -h git@github.com:username/repository.git'").execute()
return process.text.readLines().collect { 
  it.split()[1].replaceAll('refs/heads/', '').replaceAll('refs/tags/', '').replaceAll("\\^\\{\\}", '')
}

2.

def command = 'ssh-add /home/ubuntu/.ssh/id_rsa; git ls-remote -t -h git@github.com:username/repository.git'
def process = ["ssh-agent", "bash", "-c", command].execute()
return process.text.readLines().collect { 
  it.split()[1].replaceAll('refs/heads/', '').replaceAll('refs/tags/', '').replaceAll("\\^\\{\\}", '')
}

3.

def process = ["ssh-agent", "bash", "-c", "ssh-add", "/home/ubuntu/.ssh/id_rsa", "git", "ls-remote", "-t", "-h", "git@github.com:username/repository.git"].execute()
return process.text.readLines().collect { 
  it.split()[1].replaceAll('refs/heads/', '').replaceAll('refs/tags/', '').replaceAll("\\^\\{\\}", '')
}

我怀疑这与 groovy 如何解析嵌套的 shell 命令有关,但我不确定。

您的帮助将不胜感激。

更新

我按照@cfrick 的建议在 groovysh 中尝试了脚本 2,它工作得非常好。似乎这个问题与我的 Jenkins 版本有关,因为可扩展选择参数插件与我的 groovy 脚本有同样的问题。

我决定解决我的需要,并放弃了这整件事。

标签: bashshelljenkinsgroovy

解决方案


推荐阅读