Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Fill Jenkins Choice Parameter With Git Branches In a Specified Repo

I have a parameterized Jenkins job which requires the input of a specific Git branch in a specific Git repo. Currently this parameter is a string parameter.

Is there any way to make this parameter a choice parameter and dynamically fill the drop down list with the Git branches? I don't want to require someone to maintain this choice parameter by manually configuring the drop down every time a new branch is created.

like image 877
mkez00 Avatar asked May 03 '12 14:05

mkez00


People also ask

How do I pass a git branch as parameter in Jenkins?

If you want to be able to dynamically give a Git branch to use in a Jenkins build then you'll need to do a couple of things. Then, in your Pipeline configuration, under Branches to build, add your parameter name inside the Branch Specifier box, surrounded by ${} . Jenkins will expand your variable when the job runs.

How do I add extended choice parameters in Jenkins?

You need to select Extended Choice Parameter from the drop-down list as shown below. Once you select that option, you will see another drop-down with the name Parameter Type as shown in the snapshot below. Select Multi Select from that drop-down. Enter the server names in Value box.


2 Answers

I tried a couple of answers mentioned in this link, but couldn't figure out how to tell Jenkins about the user-selected branch. As mentioned in my previous comment in above thread, I had left the branch selector field empty.

But, during further investigations, I found another way to do the same thing - https://wiki.jenkins-ci.org/display/JENKINS/Git+Parameter+Plugin I found this method was a lot simpler, and had less things to configure!

Here's what I configured -

  1. Installed the git parameter plugin
  2. Checked the 'This build is parameterized' and added a 'Git parameter'
  3. Added the following values: Git Parameter plugin config in the job

  4. Then in the git SCM section of the job I added the same value mentioned in the 'Name' section, as if it were an environment variable. (If you read the help for this git parameter plugin carefully, you will realize this) Branch Selector

After this I just ran the build, chose my branch(Jenkins checks out this branch before building) and it completed the build successfully, AND by choosing the branch that I had specified.

like image 121
Ravindra Mijar Avatar answered Sep 18 '22 13:09

Ravindra Mijar


Extended Choice Parameter plugin will allow you to read the choices from a file.

Of course, now you have another problem: how to make sure the file is up-to-date (that can be done with a post-commit hook) and propagated to all the users (that can be done by placing it on a shared file server). But there may be better solutions.

like image 27
malenkiy_scot Avatar answered Sep 19 '22 13:09

malenkiy_scot