如果想要生成动态的列表参数,那么相对复杂一点,下面以“如何获取Git的远程分支列表作为Jenkins的实时动态参数”为例,简单介绍下。
Groovy有各类脚本语言通用的强大功能,比如文件读取,字符串处理,调用其他脚本语言(如shell),我们这里就用在Groovy中调用Shell的方法,来获取Git的远程分支和提交列表。
如果你需要考虑速度的话,最好在master上(一般情况下,Groovy都会在master上执行)事先准备好一个本地克隆(假定放在了/root/temp/test.git)下,在每次获取的时候使用 git pull 和远端同步下,然后使用 git for-each-ref 来获取分支列表即可,示例代码如下:
//enter
the exist git repo, and sync with the remote
def proca = [ 'bash', '-c', 'cd /root/temp/test.git; git pull' ]
proca.execute()
//get the remote branch list, and use sed to handle
Process procb = [ 'bash', '-c', 'cd /root/temp/test.git; git
for-each-ref --sort=\'-authordate\' --format=\'%(refname)\'
refs/remotes' ].execute()
Process procc = [ 'bash', '-c', 'sed -e /master/d -e /HEAD/d -e
s#refs/remotes/origin/##g' ].execute()
//this is a pipe command
(procb | procc).text.tokenize('\n')
1
2
3
4
5
6
7
8
9
10
|
//enter the exist git repo, and sync with the remote
def proca = [ 'bash', '-c', 'cd /root/temp/test.git; git pull' ]
proca.execute()
//get the remote branch list, and use sed to handle
Process procb = [ 'bash', '-c', 'cd /root/temp/test.git; git for-each-ref --sort=\'-authordate\' --format=\'%(refname)\' refs/remotes' ].execute()
Process procc = [ 'bash', '-c', 'sed -e /master/d -e /HEAD/d -e s#refs/remotes/origin/##g' ].execute()
//this is a pipe command
(procb | procc).text.tokenize('\n')
|
原文转自:hhttp://scmbob.org/groovy-in-jenkins.html