Jenkins pipeline: git branch as parameter

Install the Active Choices Plug-in in Jenkins-> Manage Jenkins -> Manage Plugin

Then in the Jenkins pipeline add the parameter “Active Choices Reactive Parameter”
Add the name as BRANCH

Then select the groovy script option. Then in the script section add the following:

tags = []
text = "get_git_branches.sh git@bitbucket.org:kompeld/k-apid.git".execute().text
text.eachLine { tags.push(it) }
return tags

In your jenkins machine create the following script:
vi /usr/local/bin/get_git_branches.sh

#!/bin/bash
GIT_URL=$1
git ls-remote –heads ${GIT_URL} | sed ‘s?.*refs/heads/??’

Then in the groovy pipeline you can access this variable as params.BRANCH

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.