Dynamically List Git Branches in Jenkins Parameter

16 May 2016

Jenkins git parameter plugin doesn’t list git branches if you work with bitbucket or at least it didn’t work for me, so I had to find other solution.

To get my git branches dynamically in a parameter I’m using Active Choices Plug-in with two scripts: one groovy that return the results to jenkins and the other one is a wrapper in bash which the groovy script use to get a list of git branches (because I don’t really know groovy:))

Prerequisite

Before using this workaround you need to configure git on your jenkins server

How To

vi /usr/local/bin/get_git_branches.sh
#!/bin/bash
GIT_URL=$1
git ls-remote --heads --tags ${GIT_URL} | awk -F" " '{print $NF}'
chmod +x /usr/local/bin/get_git_branches.sh
tags = []
text = "get_git_branches.sh https://user:[email protected]/project/repo_name.git".execute().text
text.eachLine { tags.push(it) }
return tags

if you have better suggestions or better groovy script please write a comment