如果想要生成動態的列表參數,那么相對復雜一點,下面以“如何獲取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