How to Use Branch Source Plugin in Jenkins for Multibranch Pipelines
Use the
Branch Source Plugin in Jenkins by creating a Multibranch Pipeline job and adding a branch source like Git or GitHub. This plugin automatically discovers branches and pull requests, triggering builds for each branch without manual setup.Syntax
The Branch Source Plugin is configured through Jenkins UI by creating a Multibranch Pipeline job. Key parts include:
Branch Sources: Define where Jenkins looks for branches (e.g., Git, GitHub, Bitbucket).Scan Multibranch Pipeline Triggers: Set how often Jenkins scans for new branches or changes.Build Configuration: Jenkinsfile in each branch defines the build steps.
This setup automates branch discovery and build triggering.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "Building branch ${env.BRANCH_NAME}"
}
}
}
}Example
This example shows how to create a Multibranch Pipeline job using the Branch Source Plugin with a GitHub repository.
Steps:
- Go to Jenkins dashboard, click New Item.
- Enter a name and select Multibranch Pipeline.
- Under Branch Sources, add GitHub and provide repository URL.
- Configure credentials if needed.
- Set scan triggers to periodically check for new branches.
- Each branch must have a
Jenkinsfiledefining the pipeline.
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo "Building branch ${env.BRANCH_NAME}"
}
}
}
}Output
Building branch feature-xyz
Common Pitfalls
Common mistakes when using the Branch Source Plugin include:
- Not having a
Jenkinsfilein each branch, so Jenkins cannot run the pipeline. - Incorrect repository URL or missing credentials causing branch discovery to fail.
- Not setting scan triggers, so new branches are not detected automatically.
- Using deprecated or unsupported branch source types.
Always verify repository access and Jenkinsfile presence in branches.
text
Wrong configuration example: - Branch source URL: https://github.com/user/wrong-repo.git - No credentials set Right configuration example: - Branch source URL: https://github.com/user/correct-repo.git - Credentials added for private repo access
Quick Reference
| Feature | Description |
|---|---|
| Multibranch Pipeline | Job type that scans branches and PRs automatically |
| Branch Sources | Define repositories and credentials for branch discovery |
| Scan Triggers | Schedule periodic scans for new or changed branches |
| Jenkinsfile | Pipeline script stored in each branch to define build steps |
| Automatic Builds | Builds triggered automatically for each branch or PR |
Key Takeaways
Create a Multibranch Pipeline job to use the Branch Source Plugin in Jenkins.
Add your repository as a branch source and configure credentials if needed.
Ensure each branch has a Jenkinsfile to define its pipeline steps.
Set scan triggers to detect new branches and pull requests automatically.
Verify repository access and Jenkinsfile presence to avoid build failures.