选择分支以在Jenkins中构建 [英] Select branch to build in Jenkins
问题描述
我在我的项目中有几个分支。是否可以在Jenkins的工作中进行动态分支选择?
的想法是Jenkins将获得当前分支的列表,并将其显示为可能的选择参数。有什么办法吗?感谢
I have several branches in my project. Is it possible to make dynamic branch selection in Jenkins's job? The idea is that Jenkins will get list of current branches and display them as possible choice parameter. Is there any way to do that? Thanks
推荐答案
是的,您可以使用扩展选择参数插件。安装插件后,转到作业的配置页面。现在按照以下步骤操作:
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:
- 启用复选框
此构建已参数化
。 - 在下拉菜单中,
添加参数
,选择扩展选择参数
/ li>
- 由于您将只选择一个构建的分支,将
参数类型
保留为单选
- 在
部分选择价值来源
,单击单选按钮属性文件
。指定文件的绝对路径。 - 刚刚在
属性文件
下,您将看到键
。这里你必须指定键。属性文件采用键值对的形式。
例如,key = value1,value2,...
- Enable check box
This build is parameterized
. - In the dropdown menu,
Add Parameter
, selectExtended Choice Parameter
- Since you will be selecting only one branch for a build, leave the
Parameter Type
asSingle Select
- In section
Choose Source for Value
, click on radio buttonProperty File
. Specify the absolute (full) path to the file. - Just below
Property File
, you will seeProperty Key
. Here you have to specify the key. The property file is in the form of key-value pairs. For ex.,key=value1,value2,...
正如你可以从下面显示的属性文件中看到的,我将使用 branch_name
作为属性键
框。
As you can see from the property file content shown below, i will be using branch_name
as the key in Property Key
box.
[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:
>
现在如果您已经拥有分支列表,则可以使用上面指定的格式创建属性文件。但是,由于分支创建时不时发生,您需要从您的版本控制工具中动态获取列表。我们使用Git,所以我可以帮助你,如果需要。如果您使用其他任何内容,则必须搜索所需的命令。要动态获得分支列表 ,我设置了一个cron,它保持检查Git仓库并获取分支列表。然后,使用最新分支列表填充属性文件,然后由Jenkins动态地 加载。
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. 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. It then populates the property file with the up-to-date branch list which is then dynamically loaded by Jenkins.
更新:
我们使用Gitolite并使用 git ls- code>命令。
We use Gitolite and access branch names using git ls-remote
command.
git ls-remote gitolite@git.server.com:repository_name
例如,
[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
要过滤掉分支名称,并以键值对的形式在文件中填充相同的名称,可以使用以下脚本: / p>
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 :确保属性文件在Jenkins Master上(在主从设置的情况下)。
P.S.: Make sure that the property file is on Jenkins Master (in case of Master-Slave setup).
这篇关于选择分支以在Jenkins中构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!