Operations 7 min read

Using Git Parameter Plugin 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‑practice considerations for reliable CI/CD workflows.

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

The article begins by discussing the need for Git parameters in Jenkins, highlighting the challenges of fixed‑branch configurations and the benefits of allowing developers to select branches dynamically during builds.

It then describes how to enable Git parameters in a Freestyle job: enable "Parameterized Build", add a Git parameter, fill in repository details, and trigger builds to select the desired branch.

Next, the guide covers three Pipeline scenarios. First, an ordinary Pipeline where the Jenkinsfile is stored inside the job; it shows adding a srcUrl string parameter and a branchName Git parameter, followed by a sample 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}"]]])
                }
            }
        }
    }
}

The second scenario uses a "Pipeline as Code" approach where the Jenkinsfile resides in a Git repository. It demonstrates adding the same srcUrl and branchName parameters, configuring the Git parameter to point to the Jenkinsfile repository, and then running the same checkout logic.

The third scenario, "Pipeline as Code", stores all parameter definitions directly in the Jenkinsfile. It provides a complete declarative pipeline example that defines the srcUrl string parameter and the gitParameter for branch selection, followed by the checkout stage.

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

Finally, the article summarizes that using Git parameters simplifies branch selection but may require careful maintenance, and notes that the organization’s DevOps platform now centralizes parameter selection, reducing reliance on Jenkins‑specific parameter types.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CI/CDAutomationdevopsJenkinsGit 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.