DB, DevOps & Cloud Solutions

Quick Nuggets : How to List GIT branches dynamically in Jenkins Pipeline job

Use Case :

Now the Project is configured as parameterized Jenkins Job which requires the specific git branch  as Input to the git checkout . This parameter configured as Strign parameter . If any new Branch is created  in Git Repository  , we need add this branch Manually  configuring in the drop down every time a new bracnh is created . Some times human  error configuring the Branch names .

Solution:

we make this parameter a choice parameter and dynamically fill the drop down list with the Git branches . when New branches Created in Git Repo  It will populated Automatically in DropDown List .

Prerequisite: 

Option 1:

Usign the Jenkin PipeLine Script :

Create the PipeLine Job -> Pipeline section -> Defination of Pipeline Script -> write the Pipe line stages to build and get the Dynamic branches .

 

Pipeline Script For Build and Get the Dynamic Branches .

GITHUB_PROJECT = “https://github.com/zagadishreddy/game-of-life.git”
GITHUB_CREDENTIALS_ID = “Telcel” //maps to a Jenkins Credentials Vault ID
APPLICATION_NAME = “Telcel”
GITHUB_BRANCH = ‘${env.BRANCH_NAME}’
node{
// Stages
stage (“Listing Branches”) {
echo “Initializing workflow”
//checkout code
echo GITHUB_PROJECT
git url: GITHUB_PROJECT, credentialsId: GITHUB_CREDENTIALS_ID
sh ‘git branch -r | awk \'{print $1}\’ ORS=\’\\n\’ >branches.txt’
sh ”’cut -d ‘/’ -f 2 branches.txt > branch.txt”’
//sh “sed s’/origin”\’///g branches.txt > branch.tx”
//sed ‘s/$/from S0 to S1/’
}
stage(‘get build branch Parameter User Input’) {

liste = readFile ‘branch.txt’
echo “please click on the link here to chose the branch to build”
env.BRANCH_SCOPE = input message: ‘Please choose the branch to build ‘, ok: ‘Validate!’,
parameters: [choice(name: ‘BRANCH_NAME’, choices: “${liste}”, description: ‘Branch to build?’)]
}
stage(‘Checkout external proj’) {
echo “${env.BRANCH_SCOPE}”
git branch: “${env.BRANCH_SCOPE}”,
credentialsId: ‘Telcel’,
url: ‘https://github.com/zagadishreddy/game-of-life.git’

sh “ls -lat”
}
}

click the Build the Job -> Build History ->Console OutPut -> It will Prompt for Input pass the Branch Name .

 

Above Screen shot which Depicts the dynamically displaying the branches from  Git Repository .

Option 2: 

Install Active Choices Plugin  and need the groovy script to get the list of branches .

 

Steps Confiure the Job :

Create the New Job from the Jenkin console:

Select the  This project is parameterized option

 

Give the Name of parameter —>  Enable the Groovy Script Radio Button  -> Text Area  copy the groovy Script Code to get the branches Dynamically .

def gettags = (“git ls-remote -t -h https://username:password@github.com/zagadishreddy/game-of-life.git”).execute()
return gettags.text.readLines().collect {
it.split()[1].replaceAll(‘refs/heads/’, ”).replaceAll(‘refs/tags/’, ”).replaceAll(“\\^\\{\\}”, ”)
}

save the Job . Run the Job as Build with Parameters .

 

 


we can see the  All branches Displayed Dynamically  with out manuall effort some one to maintain to add the branch in the Choice List.

About The Author

One comment

  1. Jul

    January 7, 2019 at 6:03 pm

    Hi,

    The second solution seems better but the problem with it is that git username and password are hardcoded (and not encoded) in the groovy script.

Leave a Reply

*