Using Git Parameter for Dynamic Branch Builds in Jenkins (Freestyle and Pipeline)
This guide explains why and how to use the Git Parameter plugin in Jenkins to enable dynamic branch selection for both Freestyle and Pipeline jobs, covering configuration steps, parameter definitions, and example Jenkinsfile scripts for various pipeline setups.
In Jenkins projects, using a fixed branch for builds can become hard to maintain; the Git Parameter plugin allows dynamic selection of branches at build time, reducing manual errors and simplifying configuration.
1. Using Git Parameter in a Freestyle project
Enable "Parameterized Build" in the job configuration, add a Git Parameter, set the variable name and type, then fill in the repository information. After saving, the build page will display a dropdown of all branches from the repository, allowing you to select and build any branch.
2. Using Git Parameter in a Pipeline project
2.1 Ordinary Pipeline (Jenkinsfile stored inside the job)
Add a srcUrl string parameter to hold the repository URL, and a branchName Git Parameter of type PT_BRANCH. The Jenkinsfile can then reference these parameters to checkout the selected branch:
//pipeline
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}"]]])
}
}
}
}
}Save the job and run a build; the selected branch will be checked out automatically.
2.2 Popular Pipeline (Jenkinsfile stored in SCM)
Create a Jenkinsfile in a Git repository (e.g., git.jenkinsfile) with the same content as above. In the Pipeline job, add the same srcUrl and branchName parameters. In the Git Parameter's advanced settings, explicitly specify the repository that contains the Jenkinsfile; otherwise the plugin may fail to locate branches.
3. Pipeline as Code
All parameter definitions can be placed directly inside the Jenkinsfile, turning the job into a fully declarative pipeline. Use Jenkins' "Declarative Directive Generator" to create the parameter block. A complete example:
//pipeline
pipeline{
agent { node { label "build"}}
parameters {
string defaultValue: 'http://192.168.1.200:30088/idevops/idevops-maven-service.git',
description: '',
name: 'srcUrl',
trim: false
gitParameter branch: '',
branchFilter: '.*',
defaultValue: 'origin/master',
description: '',
name: 'branchName',
quickFilterEnabled: false,
selectedValue: 'NONE',
sortMode: 'NONE',
tagFilter: '*',
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 a build with this Jenkinsfile will correctly fetch the selected branch from the specified repository.
Conclusion
The Git Parameter plugin simplifies branch selection for Jenkins jobs, but careful configuration—especially specifying the correct repository in advanced settings—is essential to avoid branch‑resolution errors. Integrating the parameters into the Jenkinsfile (Pipeline as Code) provides a clean, version‑controlled solution.
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.
