Active Git Branches Parameter

๐Ÿ‡ฌ๐Ÿ‡ง English | ๐Ÿ‡จ๐Ÿ‡ณ ็ฎ€ไฝ“ไธญๆ–‡

๐Ÿ›‘ Stop scrolling through hundreds of stale Git branches when starting a Jenkins build.

Active Git Branches Parameter adds a build parameter that shows the branches your team is most likely to need first: recently updated branches, optionally filtered, capped, and protected from accidental selection.

It is designed for teams with busy repositories and many feature/release branches, where the normal branch dropdown quickly becomes noisy.

Important

To sort branches by recent commit activity, turn off useQuickFetch. The default (useQuickFetch: true) lists branches with ls-remote, which is fast but can only sort alphabetically. With useQuickFetch: false the plugin fetches just the latest commit of each branch into a cache repository on the controller and sorts by commit date. The first fetch takes a few seconds; after that only changed branch tips are transferred.

โ“ Why This Plugin?

Most Git branch parameter solutions can list branches. This plugin focuses on making the list useful when the repository is busy.

  • ๐Ÿš€ Find active work quickly: Put recently updated branches at the top instead of relying only on alphabetical sorting.
  • ๐Ÿ”’ Avoid dangerous branch picks: Keep protected branches visible but disabled with excludeBranches, so users understand what exists without being able to select forbidden branches.
  • โญ Keep important branches visible: Force-show branches such as main, develop, or release/.* with alwaysIncludeBranches, even when you limit the dropdown to the top N branches.
  • โœ… No Groovy scripts or script approval: Use a dedicated Java parameter instead of Active Choices scripts that run shell commands on the controller.
  • ๐Ÿ”— Works with explicit repository URLs: Useful for Pipeline jobs where the repository is checked out inside the script, not declared in the job SCM configuration.
  • ๐Ÿชถ Light on the controller and Git server: Jobs pointing at the same repository share one cached branch list, refreshed at most once per minute.
  • โšก Fast after warm-up: Uses stale-while-revalidate caching so users see the last known branch list immediately while Jenkins refreshes in the background.

๐ŸŽฏ When To Use It

Use this plugin when:

  • ๐Ÿ“Š Many branches: Your branch list is long and users usually want the most recently updated branches.
  • ๐Ÿงน Keep it simple: You want a simple branch-only parameter without maintaining Groovy scripts.
  • ๐Ÿ” Private repos: You need Jenkins credentials support for private repositories.
  • ๐Ÿ›ก๏ธ Protect main: You want to discourage or block builds from main, master, or release branches while still showing them in the UI.
  • ๐Ÿ—๏ธ Checkout in the Jenkinsfile: Your Pipeline checks out one or more repositories from inside the Jenkinsfile instead of the job SCM configuration.

๐Ÿ“‹ Not the right fit?

If you need tags, pull requests, or arbitrary revisions, the mature Git Parameter Plugin may be a better fit.
If you need GitLab-specific API-based ref loading, consider GitLab Repository Refs Parameter.

โœจ Features

๐Ÿ”„ Core Functionality

  • ๐ŸŒ Dynamic branch fetching from a configured Git repository.
  • โฐ Recent-activity sorting by branch commit date when quick fetch is disabled.
  • ๐Ÿš„ Quick fetch mode for fast remote branch listing when commit timestamps are not required.

๐ŸŽ›๏ธ Customization & Control

  • ๐Ÿ”ข Configurable Top N limit to keep the dropdown focused.
  • ๐Ÿ” Regex branch filtering with Java regular expressions.
  • โญ Always-include rules for important branches that must stay visible.
  • ๐Ÿšซ Exclude/disable rules for branches that should be visible but not selectable.

๐Ÿ” Integration & Performance

  • ๐Ÿ”‘ Jenkins Credentials integration for private repositories.
  • โšก Stale-while-revalidate cache for responsive Build with Parameters pages.

๐Ÿ“‹ Requirements

Requirement Version
๐Ÿ”ต Jenkins 2.541.3 or later
โ˜• Java 17 or later
๐Ÿ”Œ Git Plugin Latest recommended

๐Ÿ“Š Comparison

Feature Git Parameter List Git Branches Active Choices Active Git Branches
๐Ÿ“‹ Branch dropdown โœ… โœ… โœ… โœ…
๐Ÿท๏ธ Tags / PRs / revisions โœ… โœ… โœ… โŒ
๐Ÿ”— Repo in parameter โŒ โœ… โœ… โœ…
โฐ Sort by recent activity โŒ โš ๏ธ โš ๏ธ โœ…
๐Ÿ”‘ Jenkins Credentials โœ… โœ… โš ๏ธ โœ…
๐Ÿšซ No script approval needed โœ… โœ… โŒ โœ…
๐Ÿ›ก๏ธ Disable protected branches in UI โš ๏ธ โš ๏ธ โœ… โœ…

Legend: โœ… Full support ยท โš ๏ธ Limited/Partial ยท โŒ Not supported

๐Ÿ“ฆ Installation

Install Active Git Branches Parameter from Manage Jenkins โ†’ Plugins โ†’ Available plugins.

๐Ÿš€ Usage

๐Ÿ“œ Pipeline (Declarative)

pipeline {
    agent any
    parameters {
        activeGitBranches(
            name: 'BRANCH',
            repositoryUrl: 'https://github.com/your-org/your-repo.git',
            credentialsId: 'github-credentials',
            maxBranchCount: 10,
            branchFilter: 'feature/.*',
            alwaysIncludeBranches: 'main|develop',
            excludeBranches: 'main|master',
            description: 'Select a branch to build'
        )
    }
    stages {
        stage('Build') {
            steps {
                echo "Building branch: ${params.BRANCH}"
                git branch: params.BRANCH, 
                    url: 'https://github.com/your-org/your-repo.git',
                    credentialsId: 'github-credentials'
            }
        }
    }
}

๐Ÿ“ Pipeline (Scripted)

properties([
    parameters([
        activeGitBranches(
            name: 'BRANCH',
            repositoryUrl: 'https://github.com/your-org/your-repo.git',
            maxBranchCount: 15,
            description: 'Select a branch'
        )
    ])
])

node {
    stage('Build') {
        echo "Selected branch: ${params.BRANCH}"
    }
}

๐Ÿ’ผ Freestyle Job

  1. ๐Ÿ”ง Go to your job's configuration page
  2. โ˜‘๏ธ Check "This project is parameterized"
  3. โž• Click "Add Parameter" and select "Active Git Branches Parameter"
  4. โš™๏ธ Configure the following options:
    • ๐Ÿท๏ธ Parameter Name: The name of the parameter (e.g., BRANCH)
    • ๐Ÿ”— Git Repository URL: The URL of your Git repository
    • ๐Ÿ”‘ Credentials: Select credentials for private repositories (optional)
    • ๐Ÿ”ข Max Branch Count: Maximum number of branches to display (default: 10)
    • ๐Ÿ” Branch Filter: Regular expression to filter branches (optional)
    • โญ Always Include Branches: Regex for branches that must remain visible (optional)
    • ๐Ÿšซ Exclude Branches: Regex for branches to show but disable (optional)
    • ๐Ÿš„ Use Quick Fetch: Fast remote branch listing without commit-time sorting (default: enabled)
    • โœ๏ธ Allow Custom Branch: Allow users to type a branch name manually (optional)
    • ๐Ÿ“Œ Default Value: Pre-selected branch (optional)

โš™๏ธ Configuration Options

Option Required Description
name โœ… Parameter name
repositoryUrl โœ… Git repository URL (HTTPS or SSH)
credentialsId โš ๏ธ Jenkins credentials ID for private repositories
maxBranchCount โœ… Maximum number of branches to display (1-100)
branchFilter โš ๏ธ Regular expression to filter branch names
alwaysIncludeBranches โš ๏ธ Regex for branches that must always be included
excludeBranches โš ๏ธ Regex to disable/prohibit selection of specific branches
useQuickFetch โš ๏ธ true: fast ls-remote, alphabetical. false: fetch branch tips into a cache repository on the controller, sorted by commit date (default: true)
defaultValue โš ๏ธ Default selected branch
allowCustomBranch โš ๏ธ Allow users to type arbitrary branch name (default: false)
description โš ๏ธ Parameter description

Legend: โœ… Required ยท โš ๏ธ Optional

๐Ÿ” Branch Filter Examples

Pattern Description
.* โœ… Match all branches
feature/.* ๐ŸŽฏ Only feature branches
release/.* ๐Ÿ”– Only release branches
(main|master|develop) ๐ŸŒณ Only main, master, or develop
(?!release/).* โ›” Exclude release branches
hotfix-.*|bugfix-.* ๐Ÿ”ง Hotfix or bugfix branches

๐Ÿ”ฌ How It Works

  1. ๐Ÿ“„ Page Load: When the Build with Parameters page loads, the plugin resolves branches for the configured repository. All Git operations run on the controller and never touch job workspaces.
  2. ๐Ÿš„ Quick Fetch (default): Lists remote branches with ls-remote. Fast, but sorted alphabetically.
  3. โฐ Time-sorted Fetch (quick fetch disabled): Fetches only the latest commit of each branch into a cache repository under $JENKINS_HOME/caches/active-git-branches, then sorts by commit date. After the first fetch, only changed branch tips are downloaded.
  4. ๐Ÿ”ค Apply Filters: The branch filter is applied, then alwaysIncludeBranches are preserved while maxBranchCount limits the rest.
  5. ๐Ÿ›ก๏ธ Disable Rules: Branches matching excludeBranches stay visible but are disabled, and server-side validation prevents bypassing the rule.
  6. โšก Smart Caching: Cached results are returned immediately. When the cached list is older than one minute, it is refreshed in the background. Jobs using the same repository, credentials and fetch mode share one cache entry.

๐Ÿ”ง Troubleshooting

โŒ No branches displayed

  • โœ”๏ธ Verify the repository URL is correct
  • โœ”๏ธ Check that credentials are configured for private repositories
  • โœ”๏ธ Use the "Test Connection" button in the configuration

๐Ÿ’ก Tip: Check Jenkins logs for detailed error messages

๐ŸŒ Performance issues

  • โœ”๏ธ Enable useQuickFetch to skip commit-date sorting
  • โ„น๏ธ With quick fetch disabled, the first load is slower because the branch tips are fetched into the cache repository

๐Ÿ’ก Tip: To change how often branch lists are refreshed, set the system property io.jenkins.plugins.activegitbranches.ActiveGitBranchesParameterDefinition.refreshIntervalSeconds (default 60)

๐Ÿ” Authentication errors

  • โœ”๏ธ Ensure credentials have read access to the repository
  • โœ”๏ธ For GitHub, use a Personal Access Token instead of password
  • โœ”๏ธ For SSH, ensure the SSH key is properly configured in Jenkins

๐Ÿ’ก Tip: Test credentials separately to isolate auth issues

๐Ÿค Contributing

Contributions are welcome! See CONTRIBUTING.md for how to build and test the plugin.

๐Ÿ“„ License

MIT License - see LICENSE for details.