Using Git Parameters for Dynamic Branch Builds in Jenkins
This tutorial explains why and how to use Git parameters in Jenkins freestyle and pipeline jobs to enable dynamic branch selection, covering configuration steps, example Jenkinsfiles, and best practices for managing branch builds in DevOps environments.
Many Jenkins projects need to build different Git branches, but using static branch parameters quickly becomes hard to maintain; Git parameters allow developers to select branches dynamically at build time.
Using Git parameters in a Freestyle job – enable "Parameterized Build", add a Git parameter, configure the repository URL, and the UI will list all branches for selection. Screenshots illustrate the configuration screens.
After saving, the job’s build page shows a dropdown of branches that can be chosen and built.
Using Git parameters in a Pipeline job
Ordinary pipeline (Jenkinsfile stored inside the job) – add a srcUrl string parameter for the repository URL and a branchName git parameter for the branch. The following Jenkinsfile demonstrates checking out the selected branch:
pipeline{
agent { node { label "build"}}
stages{
stage("CheckOut"){
steps{
script{
println("${branchName}")
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'gitlab-admin-user', url: "${srcUrl}"]]])
}
}
}
}
}After saving and building once, the selected branch is checked out correctly.
Popular pipeline (Jenkinsfile stored in Git) – the same parameters are defined in the Jenkinsfile, and the job references the external Jenkinsfile repository. The code is identical to the ordinary pipeline example.
Pipeline as Code – all parameter definitions are placed inside the Jenkinsfile, eliminating the need for job‑level parameters. The Declarative Directive Generator can help create the syntax. A complete Jenkinsfile example is provided:
pipeline{
agent { node { label "build"}}
parameters {
string defaultValue: 'http://192.168.1.200:30088/idevops/idevops-maven-service.git', name: 'srcUrl', trim: false
gitParameter branch: '', branchFilter: '.*', defaultValue: 'origin/master', name: 'branchName', type: 'PT_BRANCH', useRepository: 'http://192.168.1.200:30088/idevops/idevops-maven-service.git'
}
stages{
stage("CheckOut"){
steps{
script{
println("${branchName}")
checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'gitlab-admin-user', url: "${srcUrl}"]]])
}
}
}
}
}Running the job once after this configuration successfully retrieves the branch information.
Summary – Git parameters simplify dynamic branch selection in Jenkins, avoiding the maintenance burden of fixed‑branch parameters. By configuring them correctly in both freestyle and pipeline jobs (including pipeline‑as‑code), teams can integrate branch selection into their DevOps workflow without extra plugins.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
