使用 groovy 脚本在 Jenkins 中选择 Git 分支 [英] Git branch select in Jenkins with groovy script

查看:33
本文介绍了使用 groovy 脚本在 Jenkins 中选择 Git 分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Jenkins 中进行参数化构建.通过这种方式,用户可以从级联菜单中选择他/她想要部署的 git 分支.

I am trying to make a Parameterized build in Jenkins. In this way user can choose the git branch he/she wants to deploy from a cascade menu.

有两种可能的方式:

  1. 在文件中写入分支名称并配置 Jenkins 以读取此文件(项目配置>扩展选择参数并选择属性文件).

问题:您必须将本地存储库作为远程存储库的镜像,并使该本地存储库与远程存储库同步.换句话说,您必须更新包含已更新可用分支名称的文件.这需要 cron 安排的作业,我不允许使用这种方法.

Problem : You have to make a local repository as a mirror of remote repo and keep this local repo in sync with remote repo. In the other words, you have to update the file containing the available branch name updated. This needs a scheduled job by cron and I am not allowed using this approach.

使用 Groovy 脚本(项目配置 > 扩展选择参数并选择Groovy 脚本").然后你需要一个 groovy 脚本来检索分支名称,如下所示:branches=master,feature/Feature-1,feature/Feature-2,hotfix/Hotfix-1,release/Release-1.

Using Groovy script (project configuration > extend choice parameter and selecting "Groovy script"). Then you need a groovy script to retrieve the branch name as follows: branches=master,feature/Feature-1,feature/Feature-2,hotfix/Hotfix-1,release/Release-1.

我在 此处 但它不起作用.我已经在我的机器上安装了 groovy.

I found a groovy script in here but it doesn't work. I have installed groovy on my machine.

有人可以帮我吗?简而言之:我需要一个 groovy 脚本来返回远程存储库的可用分支名称.

Can anybody help me? To make the story short: I need a groovy script which returns the available branch names of a remote repository.

推荐答案

下面的脚本应该会有所帮助.它基于 链接问题中的脚本.它使用简单的正则表达式过滤 git 命令输出,并为指定的 git 存储库创建分支名称列表.在 grails-core github repo 上测试:

The script below should be helpful. It's based on the scripts from linked question. It filters git command output with simple regular expression and creates list of branch names for specified git repository. Tested on 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]*	refs/heads//, '') 
}

println branches

这篇关于使用 groovy 脚本在 Jenkins 中选择 Git 分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆