xcode 选择要在 Jenkins 中构建的分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25640028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select branch to build in Jenkins
提问by Timur Suleimanov
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
我的项目中有几个分支。是否可以在 Jenkins 的工作中进行动态分支选择?这个想法是 Jenkins 将获取当前分支的列表并将它们显示为可能的选择参数。有没有办法做到这一点?谢谢
回答by Timur Suleimanov
I've found groovy script for this. A bit modified it. You need to select 'groovy script' instead of 'Property file'
我为此找到了 groovy 脚本。稍微修改了一下。您需要选择“groovy script”而不是“Property file”
def gitURL = "ssh://[email protected]/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. Works well. Huge thanks @Technext for idea.
想法是一样的。只是现在你的关键是 ${Branch} 在工作中。效果很好。非常感谢@Technext 的想法。
回答by Technext
Yes, you can do that using Extended Choice Parameterplugin. Once you have installed the plugin, go to your job's configuration page. Now follow the steps mentioned below:
是的,您可以使用扩展选择参数插件来做到这一点。安装插件后,转到作业的配置页面。现在按照下面提到的步骤操作:
- Enable check box
This build is parameterized
. - In the dropdown menu,
Add Parameter
, selectExtended Choice Parameter
- Since you will be selecting only onebranch 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,...
- 启用复选框
This build is parameterized
。 - 在下拉菜单中
Add Parameter
,选择Extended Choice Parameter
- 由于您将只选择一个分支进行构建,因此请保留
Parameter Type
asSingle Select
- 在部分中
Choose Source for Value
,单击单选按钮Property File
。指定文件的绝对(完整)路径。 - 就在下面
Property File
,你会看到Property Key
。您必须在此处指定密钥。属性文件采用键值对的形式。例如,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 dynamicallyfetch 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-datebranch list which is then dynamicallyloaded by Jenkins.
现在,如果您已经有了分支列表,您可以按照上面指定的格式创建属性文件。然而,由于分支创建不时发生,您需要从版本控制工具动态获取列表。我们使用 Git,因此如果需要,我可以帮助您。如果您使用其他任何东西,则必须搜索所需的命令。为了动态获取分支列表,我设置了一个 cron,它不断检查 Git 存储库并获取分支列表。然后它用最新的分支列表填充属性文件,然后由 Jenkins动态加载。
Update:
更新:
We use Gitolite and access branch names using git ls-remote
command.
我们使用 Gitolite 并使用git ls-remote
命令访问分支名称。
git ls-remote [email protected]:repository_name
For example,
例如,
[tom@master ~]$ git ls-remote [email protected]: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 [email protected]:repository_name | grep -v HEAD | cut -d/ -f3 | sort > /data/branch_list_temp
tr '\n' ',' < /data/branch_list_temp | sed "s/^\(.*\)/branch_name=/" > /data/branch_list
rm /data/branch_list_temp
P.S.: Make sure that the property file is on Jenkins Master (in case of Master-Slave setup).
PS:确保属性文件在 Jenkins Master 上(在 Master-Slave 设置的情况下)。