Diego Velez
Diego Velez

Reputation: 1893

Jenkins dynamic GIT BRANCH option parameter

I want to know if it's possible to list from a dropdown the GIT branches that are inside my repo dynamically on Jenkins, what I mean by this is if I currently have 3 branches (master/stage/dev) I want them to show on the dropdown when executing the job, but if tomorrow I add a new one, I want that new one to show without me to go and add the new option.

Thanks in advance!

Upvotes: 2

Views: 3162

Answers (2)

chujudzvin
chujudzvin

Reputation: 1303

There is a way to do it with a Active Choices Reactive Parameter Jenkins Plugin.

Step 1: You need to first install a plugin for Jenkins.

  1. Open Jenkins Dashboard
  2. Click Manage Jenkins
  3. Go to Manage Plugins
  4. Under Available tab search for Active Choices Plug-in

Step 2: After Plugin is installed, add a parameter to your pipeline

  1. Go to your pipeline

  2. --> Configure --> General

  3. Find Setting This project is parameterised and click it if not active yet

  4. Click Add Parameter and choose Active choices reactive parameter

  5. Give the parameter a name (e.g. BRANCH)

  6. Choose Groovy Script and add the following script as it is (only changing the link to your repo)

    def gettags = ("git ls-remote -t -h [email protected]:YOUR_REPO.git").execute() return gettags.text.readLines().collect { it.split()1.replaceAll('refs/heads/', '').replaceAll('refs/tags/','').replaceAll("\^\{\}", '') }

You're done!

enter image description here

Upvotes: 0

Mor Lajb
Mor Lajb

Reputation: 2636

did you check - https://wiki.jenkins.io/display/JENKINS/Git+Parameter+Plugin ?

you just need to add a git parameter , and a new dropdown will appear.

Upvotes: 2

Related Questions