You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jenkins Pipeline中使用Git Parameter插件配置分支参数

How to Use the Git Parameter Plugin in Jenkins Pipeline

I totally get where you're coming from—when I first tried to use this plugin in a Pipeline job, I hit the same wall: the docs said it supported Pipeline but there were zero examples to follow. After some trial and error, here's exactly how to set it up:

First, Confirm the Plugin is Installed

Before diving into code, double-check that the Git Parameter Plugin is installed on your Jenkins instance (you can find it in Manage Jenkins > Plugins). Stick to the latest stable version to avoid compatibility hiccups with Pipeline.

Basic Pipeline Configuration

Here's a minimal working example that adds a branch selection parameter to your Pipeline, mirroring the functionality you'd get in a Freestyle job:

pipeline {
    agent any
    parameters {
        // Define the Git branch selection parameter
        gitParameter(
            name: 'TARGET_BRANCH',
            type: 'PT_BRANCH', // Specifies we want to select branches
            defaultValue: 'main', // Pre-selected branch when loading the job
            branchFilter: 'origin/(.*)', // Filter to show only branches under origin
            description: 'Pick the branch you want to build'
        )
    }
    stages {
        stage('Checkout Code') {
            steps {
                // Use the selected branch in your checkout step
                git(
                    url: 'https://your-git-repo-url.git',
                    branch: params.TARGET_BRANCH
                )
            }
        }
        // Add your build/test/deploy stages here
        stage('Build') {
            steps {
                echo "Building branch: ${params.TARGET_BRANCH}"
                // Insert your build commands here
            }
        }
    }
}

Key Parameter Breakdown

Let’s break down the critical parts of the gitParameter block:

  • name: The unique identifier for your parameter—reference it later using params.<name>.
  • type: Controls what options are available:
    • PT_BRANCH: Show only branches
    • PT_TAG: Show only tags
    • PT_BRANCH_TAG: Display both branches and tags
    • PT_MULTI_BRANCH: Allow selecting multiple branches (perfect for batch operations)
  • defaultValue: The branch/tag that’s pre-selected when you open the job page.
  • branchFilter: A regex to filter displayed branches/tags. For example, origin/release-(.*) would only show release branches.

Advanced: Multiple Branch Selection

If you need to select multiple branches, use the PT_MULTI_BRANCH type and loop through the selected values:

pipeline {
    agent any
    parameters {
        gitParameter(
            name: 'TARGET_BRANCHES',
            type: 'PT_MULTI_BRANCH',
            defaultValue: 'main',
            branchFilter: 'origin/(.*)',
            description: 'Select multiple branches to build'
        )
    }
    stages {
        stage('Build Selected Branches') {
            steps {
                script {
                    params.TARGET_BRANCHES.each { branch ->
                        echo "Building branch: ${branch}"
                        // Checkout and build each branch
                        git(url: 'https://your-git-repo-url.git', branch: branch)
                        // Add your build steps here
                    }
                }
            }
        }
    }
}

Final Tips

  • When you run the Pipeline job, you’ll see a dropdown (or multi-select list) to pick your desired branch(es)—just like in a Freestyle job.
  • If you run into issues, make sure your plugin is up to date. Older versions had limited Pipeline support.

内容的提问来源于stack exchange,提问作者DenCowboy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:14:29