选择要在 Jenkins 中构建的分支 [英] Select branch to build in 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:
- 启用复选框
此构建已参数化
. - 在下拉菜单中,
Add Parameter
,选择Extended Choice Parameter
- 由于您将为构建仅选择 一个 分支,请将
Parameter Type
保留为Single Select
- 在
Choose Source for Value
部分,单击单选按钮Property File
.指定文件的绝对(完整)路径. - 就在
Property File
下方,您将看到Property Key
.在这里您必须指定密钥.属性文件采用键值对的形式.例如,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
作为 Property Key
框中的键.
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 存储库并获取分支列表.然后它使用 up-to-date 分支列表填充属性文件,然后 动态 由 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-remote
命令访问分支名称.
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
要仅过滤掉分支名称并以键值对的形式将其填充到文件中,您可以使用以下脚本:
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 '
' ',' < /data/branch_list_temp | sed "s/^(.*)/branch_name=1/" > /data/branch_list
rm /data/branch_list_temp
P.S.:确保属性文件位于 Jenkins Master 上(以防主从设置).
P.S.: Make sure that the property file is on Jenkins Master (in case of Master-Slave setup).
这篇关于选择要在 Jenkins 中构建的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!