Operations 7 min read

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, code examples, and best practices for managing branch builds efficiently.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Git Parameter for Dynamic Branch Builds in Jenkins (Freestyle and Pipeline)

1.1 Why Use Git Parameter?

Projects often have multiple branches, and a single Jenkins pipeline can support releases from several branches. Fixed branch parameters become hard to maintain, while a character parameter forces developers to type branch names manually, leading to errors. Using Git Parameter enables dynamic branch selection.

1.2 Using Git Parameter in Freestyle Projects

When a project does not use a pipeline, enable "Parameterized Build" in the job configuration, add a Git Parameter, and specify the variable name and type. After filling in the repository information, the job will list all branches for selection.

1.3 Using Git Parameter in Pipeline Projects

1.3.1 Traditional Pipeline (Jenkinsfile stored in the job)

Add a srcUrl string parameter for the repository URL and a branchName Git Parameter of type PT_BRANCH. Then write a Jenkinsfile that checks out 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 configuration and run a build to verify that the branch list is retrieved.

1.3.2 Modern Pipeline (Jenkinsfile stored in Git)

Create a Jenkinsfile (e.g., git.jenkinsfile) in a Git repository with the same content as above. In the job, add a srcUrl parameter for the Jenkinsfile repository and configure the Git Parameter to point to this repository in the advanced settings.

1.3.3 Pipeline as Code

All parameters can be defined directly inside the Jenkinsfile, eliminating the need for separate job configuration. Use the Declarative Directive Generator (Pipeline Syntax → Declarative Directive Generator) to generate the parameter block.

//pipeline
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}"]]])
                }
            }
        }
    }
}

After saving and building, the job will correctly fetch the branch information from the specified repository.

1.4 Summary

Using Git Parameter simplifies branch selection in Jenkins jobs, reduces manual errors, and integrates well with DevOps platforms that handle parameter selection externally. While the plugin has some limitations, it remains a useful tool for dynamic CI/CD workflows.

CI/CDDevOpsPipelineJenkinsFreestyleGit Parameter
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.