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
问题:
回答1:
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:
- 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 TypeasSingle 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,...
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:
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.
Update:
We use Gitolite and access branch names using git ls-remote command.
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 rm /data/branch_list_temp P.S.: Make sure that the property file is on Jenkins Master (in case of Master-Slave setup).
回答2:
I've found groovy script for this. A bit modified it. You need to select 'groovy script' instead of '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. Works well. Huge thanks @Technext for idea.