Building CI Multibranch pipeline with Jenkins and GitHub

In this post, we’ll show how to integrate Jenkins’s multibranch pipeline project with GitHub for continuous integration.

· Prerequisites
· Overview
∘ What is a Jenkins Multibranch pipeline?
∘ How does a Multibranch pipeline?
· Getting Started
∘ Create a Jenkins Multibranch Pipeline
∘ Configure webhook
∘ Test the multi-branch pipeline
· Conclusion
· References

Prerequisites

This is the list of all the prerequisites:

  • Spring Boot 3+
  • Maven 3.6.+
  • Jenkins server is already up and running
  • A GitHub repository
  • Git

Overview

What is a Jenkins Multibranch pipeline?

The Multibranch Pipeline enables the developer to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages, and executes Pipelines for branches that contain a Jenkinsfile in source control.

https://www.jenkins.io/doc/book/pipeline/multibranch/

How does a Multibranch pipeline?

A multibranch job is simply a folder of pipeline jobs. For every branch you have, Jenkins will create a folder. So instead of creating a pipeline job for each of the branches you have in a git repository, you could use a multibranch job. This means that you’ll have to create only one job. We also need to define where the Jenkinsfile is, and it’s important that this file is at the same location in all the branches you create.

When a developer pushes his code. GitHub sends a webhook with change information to Jenkins.

Jenkins receives the change information and automatically creates a branch pipeline. Then it runs the tasks with the steps mentioned in the Jenkins file of that branch.

Getting Started

Create a Jenkins Multibranch Pipeline

Before even starting, we need to create an empty git repository on GitHub. You could use an existing one, but for this story, we’ll start from scratch with a Spring boot maven project.

The Jenkinsfile in the project root directory contains all of our continuous integration steps.

pipeline {
agent any

environment {
MAVEN_ARGS=" -B -e -U"
}

stages {

stage('Build') {
steps {
withMaven(maven: 'MAVEN_ENV') {
sh "mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true ${MAVEN_ARGS}"
}
}
}

stage('Unit tests') {
steps {
withMaven(maven: 'MAVEN_ENV') {
sh "mvn clean test-compile surefire:test ${MAVEN_ARGS}"
}
}
}

stage('Integration tests') {
steps {
withMaven(maven: 'MAVEN_ENV') {
sh "mvn clean verify -Dskip.unit.tests=true ${MAVEN_ARGS}"
}
}
post {
success {
junit allowEmptyResults: true, testResults: '**/*-reports/*.xml'
}
}
}

stage('Code quality - sonar') {
steps {
sh """
echo "Running sonar Analysis"
"""
}
}

stage('OWASP Dependency-Check Vulnerabilities') {
steps {
sh """
echo "OWASP Dependency-Check Vulnerabilities"
"""
}
}
}
}

You can check my previous story and add the sonar script at the quality stage.

  • Login to Jenkins
  • Click New Item on Jenkins home page.
  • Enter a name for your Pipeline, select Multibranch Pipeline, and click OK.
  • Add a Branch Source (for example, Git) and enter the location of the repository.

If you have a private repository, you must provide a personal access token in GitHub to use in Jenkins jobs.

The new repository link for Jenkins will look like the following syntax:

https://access_token@github.com/<Your_Org>/yourRepoName.git

  • Save the Multibranch Pipeline project.

Upon Save, Jenkins automatically scans the designated repository and creates appropriate items for each branch in the repository which contains a Jenkinsfile.

Configure webhook

In order to automate the build process in the multibranch pipeline, we will use Github Webhook. We need to install the “Multibranch Scan Webhook Trigger” plugin in Jenkins.

Then, In Scan Multibranch Pipeline Triggers, choose Scan by webhook. Here we should give a token. I have generated a random token “!HCWqf7u?8d2cN7Sfa?QnCd5pP?zMqpdIEJNtjj3d0byrzJJk=dXxtAd0vGDvmNM”.

A multi-branch scan will be triggered if a token matches the token I entered. The multi-branch scan URL : JENKINS_URL/multibranch-webhook-trigger/invoke?token=[Trigger token]

You should see a green tick mark on a successful webhook configuration as shown below.

Test the multi-branch pipeline

Let’s create two more branches in Git to see what happens in Jenkins.

I created two new branches called “develop” and “feature” and pushed the changes to the remote repository.

We see that the builds were triggered automatically on Jenkins for the two new branches.

Well done !!.💪

Conclusion

In this post, we have seen how to integrate Jenkins’s multibranch pipeline project with GitHub for continuous integration.

The complete source code is available on GitHub.

You can reach out to me and follow me on MediumTwitterGitHub

Thanks for reading!

References

👉 Link to Medium blog

Related Posts