Operations 10 min read

Guide to Setting Up Jenkins Multibranch Pipeline for Pull‑Request and Branch‑Based CI/CD

This tutorial explains how to configure Jenkins multibranch pipelines to automatically discover Git branches and pull‑requests, run conditional stages such as unit tests and code analysis, set up the required Jenkinsfile, configure GitHub webhooks, and troubleshoot common issues, providing a complete CI/CD workflow for developers.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Guide to Setting Up Jenkins Multibranch Pipeline for Pull‑Request and Branch‑Based CI/CD

This guide introduces Jenkins multibranch pipelines, a Git‑based "pipeline as code" approach that automatically creates pipelines for new branches and pull‑requests, making CI/CD workflows more efficient and feedback‑driven.

Multibranch pipelines can discover branches from SCMs like GitHub, Bitbucket or GitLab, support PR‑based branch discovery, and allow conditional logic in the Jenkinsfile to run specific stages (e.g., unit tests, Sonar analysis) only on certain branches.

The typical workflow includes:

Developers commit code to a feature branch.

A pull‑request from the feature branch to the develop branch triggers unit testing and static analysis.

After successful tests, the PR is merged into develop.

A PR from develop to master triggers a build that runs tests, analysis, and deploys to dev/QA environments.

An example Jenkinsfile for a multibranch pipeline is shown below. It defines an agent, cleanup, checkout, testing, analysis, and a conditional deployment stage that runs only on the develop branch.

pipeline {

    agent {
        node {
            label 'master'
        }
    }

    options {
        buildDiscarder logRotator(
            daysToKeepStr: '16',
            numToKeepStr: '10'
        )
    }

    stages {

        stage('Cleanup Workspace') {
            steps {
                cleanWs()
                sh """
                echo \"Cleaned Up Workspace For Project\"
                """
            }
        }

        stage('Code Checkout') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: '*/main']],
                    userRemoteConfigs: [[url: 'https://github.com/spring-projects/spring-petclinic.git']]
                ])
            }
        }

        stage(' Unit Testing') {
            steps {
                sh """
                echo \"Running Unit Tests\"
                """
            }
        }

        stage('Code Analysis') {
            steps {
                sh """
                echo \"Running Code Analysis\"
                """
            }
        }

        stage('Build Deploy Code') {
            when {
                branch 'develop'
            }
            steps {
                sh """
                echo \"Building Artifact\"
                """

                sh """
                echo \"Deploying Code\"
                """
            }
        }

    } 
}

To configure the pipeline in Jenkins, create a new multibranch pipeline project, add the GitHub source, provide credentials, select branch discovery (or PR discovery), optionally set a custom Jenkinsfile path, and enable options like "Discard old builds".

Configure a GitHub webhook (payload URL: http:// /github-webhook/ ) to send events such as pushes and pull‑requests to Jenkins, ensuring the webhook is active and correctly authenticated.

Testing involves creating a PR from a feature branch, verifying that Jenkins receives the webhook, runs the defined stages, and updates the PR status. Troubleshooting tips cover branch discovery issues, webhook failures, and checking Jenkins system logs for error details.

ci/cdautomationDevOpsgitJenkinspull requestMultibranch Pipeline
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

login 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.