简体   繁体   English

选择分支以在Jenkins中构建

[英]Select branch to build in Jenkins

I have several branches in my project. 我的项目中有几个分支。 Is it possible to make dynamic branch selection in Jenkins's job? 是否有可能在Jenkins的工作中进行动态分支选择? The idea is that Jenkins will get list of current branches and display them as possible choice parameter. 想法是Jenkins将获得当前分支的列表并将其显示为可能的选择参数。 Is there any way to do that? 有没有办法做到这一点? Thanks 谢谢

I've found groovy script for this. 我已经为此找到了groovy脚本。 A bit modified it. 有点修改它。 You need to select 'groovy script' instead of 'Property file' 您需要选择'groovy script'而不是'Property file'

def gitURL = "ssh://jenkins@phabricator.com/project.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\//, '') 
}   
return branches.join(",")

Idea is the same. 想法是一样的。 Only now your key is ${Branch} in the job. 只有现在你的关键是工作中的$ {Branch}。 Works well. 效果很好。 ?Huge thanks @Technext for idea. ?非常感谢@Technext的想法。

Yes, you can do that using Extended Choice Parameter plugin. 是的,您可以使用扩展选择参数插件来实现。 Once you have installed the plugin, go to your job's configuration page. 安装插件后,转到作业的配置页面。 Now follow the steps mentioned below: 现在按照下面提到的步骤操作:

  1. Enable check box This build is parameterized . 启用复选框This build is parameterized
  2. In the dropdown menu, Add Parameter , select Extended Choice Parameter 在下拉菜单中, Add Parameter ,选择Extended Choice Parameter
  3. Since you will be selecting only one branch for a build, leave the Parameter Type as Single Select 由于您只为构建选择一个分支,因此将Parameter Type保留为Single Select
  4. In section Choose Source for Value , click on radio button Property File . 在“ Choose Source for Value部分中,单击单选按钮“ Property File Specify the absolute (full) path to the file. 指定文件的绝对(完整)路径。
  5. Just below Property File , you will see Property Key . Property File下方,您将看到Property Key Here you have to specify the key. 在这里你必须指定密钥。 The property file is in the form of key-value pairs. 属性文件采用键值对的形式。 For ex., key=value1,value2,... 例如, key=value1,value2,...

As you can see from the property file content shown below, i will be using branch_name as the key in Property Key box. 从下面显示的属性文件内容中可以看到,我将使用branch_name作为Property Key框中的键。

[tom@master ]# cat /data/branch_list
branch_name=master,mainline,branch_A,branch_B,branch_C,branch_N,

Refer snapshot below for better understanding of what i explained above: 请参阅下面的快照,以便更好地理解我上面解释的内容

在此输入图像描述

Now if you already have the branch list with you, you can create the property file in the format specified above. 现在,如果您已经拥有分支列表,则可以使用上面指定的格式创建属性文件。 However, since branch creation happens from time-to-time, you need to dynamically fetch the list from your version control tool. 但是,由于分支创建时有发生,因此您需要从版本控制工具中动态获取列表。 We use Git so i can help you with that, if need be. 我们使用Git,如果需要,我可以帮助你。 If you use anything else, you will have to search for the required command. 如果您使用其他任何东西,则必须搜索所需的命令。 To get the branch list dynamically , i have set-up a cron which keeps checking the Git repo and fetches the list of branch. 为了动态获取分支列表,我设置了一个cron,它继续检查Git repo并获取分支列表。 It then populates the property file with the up-to-date branch list which is then dynamically loaded by Jenkins. 然后,它使用最新的分支列表填充属性文件,然后由Jenkins 动态加载。

Update: 更新:

We use Gitolite and access branch names using git ls-remote command. 我们使用Gitolite并使用git ls-remote命令访问分支名称。

git ls-remote gitolite@git.server.com:repository_name

For example, 例如,

[tom@master ~]$ git ls-remote gitolite@git.server.com:repository_name
08a119f0aec5d4286708d2e16275fcd7d80d2c25        HEAD
a91ef29f1be5bfe373598f6bb20d772dcc65b8ca        refs/heads/dev-mob
d138356cf752a46fd8c626229809c9eaae63a719        refs/heads/dev-ssorel
e7d7e2c617c4a42b299b29c0119283813800f1bb        refs/heads/dev-omni
3193b36d678f1af2dcc3a291c6313f28ede97149        refs/heads/dev-pay
72fd9d8586708011c763cd7bc4f7bd2a3513a12f        refs/heads/dev-sell
39455fc2672039a7f325e9cafe3777ed563368ef        refs/heads/dev-apis
a22eb000ffa1ac0fbbf51b6bc8aea31b040567a3        refs/heads/dev-front
78a63105ec754d7ba758af97d542e749ceb9c533        refs/heads/dev-tpsp
82d99796690b6c562872ea68655c74ebc3f0abfb        refs/heads/mainline
fd82522f9999cedb11e245b515d480187c2e9cc6        refs/heads/master

To filter out branch names only and populate the same in a file in the form of key-value pair, you can use this script: 要仅过滤掉分支名称并以键值对的形式在文件中填充分支名称,可以使用以下脚本:

#!/bin/bash

git ls-remote gitolite@git.server.com:repository_name | grep -v HEAD | cut -d/ -f3 | sort > /data/branch_list_temp
tr '\n' ',' < /data/branch_list_temp | sed "s/^\(.*\)/branch_name=\1/" > /data/branch_list

rm /data/branch_list_temp

PS : Make sure that the property file is on Jenkins Master (in case of Master-Slave setup). PS :确保属性文件在Jenkins Master上(在Master-Slave设置的情况下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM