Dan*_*ial 12 git groovy jenkins
我想Parameterized build在詹金斯做一个.通过这种方式,用户可以从级联菜单中选择他/她想要部署的git分支.
有两种可能的方法:
在文件中编写分支名称并配置Jenkins以读取此文件(project configuration > extend choice parameter and selecting Property file).
问题:您必须将本地存储库作为远程存储库的镜像,并使此本地存储库与远程存储库保持同步.换句话说,您必须更新包含更新的可用分支名称的文件.这需要cron的预定作业,我不允许使用这种方法.
使用Groovy脚本(project configuration > extend choice parameter and selecting "Groovy script").然后你需要一个groovy脚本来检索分支名称,如下所示:branches=master,feature/Feature-1,feature/Feature-2,hotfix/Hotfix-1,release/Release-1.
我在这里找到了一个groovy脚本,但它不起作用.我在我的机器上安装了groovy.
有谁能够帮我?为了简化故事:我需要一个groovy脚本,它返回远程存储库的可用分支名称.
Paw*_*cyk 14
下面的脚本应该会有所帮助.它基于链接问题的脚本.它使用简单的正则表达式过滤git命令输出,并为指定的git存储库创建分支名称列表.测试grails-core github repo:
def gitURL = "https://github.com/grails/grails-core.git"
def command = "git ls-remote -h $gitURL"
def proc = command.execute()
proc.waitFor()
if ( proc.exitValue() != 0 ) {
println "Error, ${proc.err.text}"
System.exit(-1)
}
def branches = proc.in.text.readLines().collect {
it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '')
}
println branches
Run Code Online (Sandbox Code Playgroud)
尝试使用
使用以下步骤:
向您的作业添加可扩展的选择参数
在 Choice 参数字段中选择System Groovy Choice 参数
将以下脚本放在Groovy 脚本文本框中,并将占位符“< >”替换为所需的值。
import groovy.json.JsonSlurper;
try{
List<String>params = new ArrayList<String>()
URL apiUrl = "https://api.github.com/users/<repo-owner>/repos?access_token=<github-access-token>".toURL()
List branches = new JsonSlurper().parse(apiUrl.newReader())
for (branch in branches ) {
params.add(branch.name)
}
return params
}
catch(IOException ex){
print ex
}
Run Code Online (Sandbox Code Playgroud)为您的工作添加主动选择反应参数
将参考参数作为存储库和以下脚本放置在 Groovy 脚本文本框中
import groovy.json.JsonSlurper;
try{
List<String>params = new ArrayList<String>()
URL apiUrl = "https://api.github.com/repos/<repo-owner>/$repository/branches?access_token=<github-access-token>".toURL()
List json = new JsonSlurper().parse(apiUrl.newReader())
for (repo in json ) {
params.add(repo.name)
}
return params
}
catch(IOException ex){
print ex
}
Run Code Online (Sandbox Code Playgroud)
笔记:
$repository 从存储库参数中获取动态值。现在尝试使用参数构建作业
查看 github api 文档以获取更多信息。希望这对你有帮助..!!;-)
| 归档时间: |
|
| 查看次数: |
27717 次 |
| 最近记录: |