Skip to main content

Command Palette

Search for a command to run...

πŸš€ Stop Managing Jobs Manually: The Ultimate Guide to Jenkins Multibranch Pipelines

Published
β€’4 min read
πŸš€ Stop Managing Jobs Manually: The Ultimate Guide to Jenkins Multibranch Pipelines

In a high-velocity DevOps environment, manual work is the enemy. If you are still manually creating a new Jenkins job every time a developer creates a feature branch, you are bottlenecking your own team.

You need a CI/CD system that is as dynamic as your Git repository.

This guide will walk you through Multibranch Pipelines and Webhooksβ€”the dynamic duo that allows Jenkins to self-manage jobs, auto-detect new code, and clean up after itself.


1. Why Multibranch Pipelines?

Standard Jenkins jobs are static. They are tied to a specific branch (usually main). A Multibranch Pipeline is different: it treats your entire repository as a project.

The 3 Big Wins:

  1. Automatic Discovery: Jenkins scans your repo. If it finds a branch with a Jenkinsfile, it creates a job. If it doesn't, it ignores it.

  2. Self-Cleaning (Orphaned Item Strategy): When a Pull Request is merged and the branch is deleted, Jenkins detects this and removes the old job data automatically.

  3. Isolation: feature/login builds don't mess up main builds. Each branch has its own history, logs, and artifacts.


2. Step-by-Step Setup

Phase 1: Create the Project

  1. New Item: In Jenkins, click New Item β†’ Enter a Name β†’ Select Multibranch Pipeline.

  2. Branch Sources: Choose Git or GitHub.

  3. Credentials: Add your SSH Key or Personal Access Token (PAT).

  4. Behaviors: (Optional) Use "Filter by name" (with wildcards like feat/*) if you only want to build specific branches.

Phase 2: The "Orphaned Item Strategy"

This is the most critical setting for keeping your Jenkins clean.

  • Discard old items: Checked.

  • Days to keep old items: 2 (Gives you 48 hours to debug a deleted branch if needed).

  • Max # of old items to keep: 5.

πŸ’‘ Pro Tip: If you don't set this, your Jenkins disk space will fill up with build artifacts from branches that were deleted months ago!


3. Webhooks: The "Push" vs. "Poll"

By default, Jenkins might "poll" GitHub every 5 minutes to ask, "Any changes?"

  • Polling is bad: It hits API rate limits and adds a delay to your feedback loop.

  • Webhooks are good: GitHub sends a notification (HTTP POST) to Jenkins the millisecond code is pushed.

Configuration: Using the "Generic Webhook Trigger"

While the default GitHub plugin is fine, the Generic Webhook Trigger plugin gives you granular control (e.g., triggering builds only for specific branches or tags).

Step 1: Install the Plugin

Go to Manage Jenkins β†’ Plugins and install Generic Webhook Trigger.

Step 2: Configure the Job

In your Pipeline configuration, scroll to Build Triggers:

  1. Check: Generic Webhook Trigger.

  2. Post Parameters: Add a parameter named ref.

    • Variable: ref

    • Expression: $.ref (JSONPath to extract the branch name).

  3. Token: Create a secure token (e.g., my-project-secret-123).

Your Trigger URL is now:

http://YOUR_JENKINS_URL/generic-webhook-trigger/invoke?token=my-project-secret-123

Step 3: Configure GitHub

  1. Go to your Repo Settings β†’ Webhooks β†’ Add Webhook.

  2. Payload URL: Paste the URL from above.

  3. Content Type: application/json (Crucial!).

  4. Events: Select "Just the push event".


4. The Smart Jenkinsfile

You don't need different files for different branches. Use logical conditions in your Jenkinsfile to decide what runs where.

Groovy

pipeline {
    agent any

    stages {
        // Runs on EVERY branch
        stage('Build & Test') {
            steps {
                echo "Compiling code on branch: ${env.BRANCH_NAME}"
                sh './mvnw clean package'
            }
        }

        // Only runs on the 'main' branch
        stage('Deploy to Production') {
            when {
                branch 'main'
            }
            steps {
                echo "Deploying to Production Server..."
                // sh './deploy.sh'
            }
        }
    }

    post {
        always {
            cleanWs() // Always clean workspace to save disk space
        }
    }
}

5. Troubleshooting Checklist πŸ› οΈ

If your webhook isn't triggering the build, check these common culprits:

Symptom

Likely Cause

Solution

HTTP 403

Security/CSRF

Check "Enable Proxy Compatibility" in Jenkins Global Security.

HTTP 404

Bad URL

Did you forget the /invoke part of the URL?

Nothing happens

Token Mismatch

The token=... in GitHub must match the token in the Jenkins Job Config exactly.

Wrong Branch

Regex Error

If using filters, test your Regex at Regex101.


Conclusion

By combining Multibranch Pipelines with Webhooks, you transform Jenkins from a static tool into an automated engine. It scales with your team, keeps your environment clean, and delivers feedback instantly.

Next Steps:

Once you have this running, look into adding a Webhook Secret to verify the payload signature. This prevents unauthorized users from spamming your build server with fake webhooks!