How to set-up a Multi-Branch Pipeline on Jenkins
Tags: jenkins, cicd, git, devops
Published: 2023-08-13 11:45:29

A multi-branch pipeline is a concept that automatically sets up Jenkins pipelines based on Git branches. It discovers new branches in GitHub (or any other SCM) and creates a pipeline for that branch. Once a new pipeline is set up, Jenkins uses the Jenkins file in that branch for build stages.

In this article we'll be going through the steps to set up a multi-branch pipeline for a basic Java/Maven App.

**Step 1: Install and Configure Jenkins**

1. If you haven't already, install Jenkins on your server or local machine following the official installation guide: https://www.jenkins.io/doc/book/installing/

2. After installation, open Jenkins in your web browser (http://localhost:8080 by default).

**Step 2: Install Required Plugins**

1. Click on "Manage Jenkins" in the left sidebar.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u9309fl10w38b259p76a.png)

2. Select "Manage Plugins."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/luf9ui4kzbk2ixa9u3d9.png)



3. Navigate to the "Available" tab.
4. Search for and install the following plugins if they're not already installed:
   - **GitHub Integration**: Allows Jenkins to interact with GitHub repositories.
   - **Pipeline Maven Integration**: Allows Jenkins to work with Maven projects.

**Step 3: Create a New Multi-Branch Pipeline**

1. Click on "New Item" in the left sidebar.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qjrsamgp38j24n19ttp.png)


2. Enter a name for your pipeline (e.g., "Multi-Branch Pipeline").
3. Choose "Multibranch Pipeline" as the project type and click "OK."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0rgwd245e6mk8f8rly7.png)



**Step 4: Configure Source Code Management**

1. In the "Branch Sources" section, click "Add Source" and select "GitHub."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzvdoc877xz7a4wq3eqj.png)



2. Click on "GitHub" to configure the source details.
3. Provide your GitHub credentials or a personal access token (recommended for security).
4. Select the GitHub repository where your Java Maven project is hosted.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pb60aw8arx6ka4aa6nre.png)



5. Specify the behavior for discovering branches. You can click on 'add' then choose 'filter by name(with regular expression'. In this case, we will choose the wildcard option to discover branches from the repo. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bvjh28983agv6t74qfo.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s683apk5jxou8miwzyuq.png)

6. Click "Save" to save the configuration.

**Step 5: Configure Jenkinsfile**

1. In your GitHub repository, create a file named "Jenkinsfile" (without quotes) in the root directory. This file defines your pipeline.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bydrfnm2mvb6uhekks8n.png)


2. Here's an example Jenkinsfile for a basic Java Maven project:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekv5ovy5yhssvump4rmn.png)



This Jenkinsfile defines three stages: Checkout, Build, and Test. It checks out the code from the repository, builds the Maven project, and runs tests.

**Step 6: Run the Multi-Branch Pipeline**

1. Back in Jenkins, you should see your multi-branch pipeline project.
2. Jenkins will automatically detect branches in your GitHub repository and create pipeline instances for each branch.
3. Click on a specific branch to see the pipeline runs and their status.
4. To manually trigger a pipeline run, click "Run" on the specific branch.

**Tips:**

- Keep your Maven project's `pom.xml` file in the repository to manage project dependencies and configuration.
- Make sure your Maven project has appropriate unit tests.
- Regularly update Jenkins and its plugins for security and new features.
- Use a version control system (like Git) for your codebase.
- Secure your credentials using Jenkins' credentials plugin and avoid hardcoding sensitive information in your Jenkinsfile.

**Conclusion:**

So, there you have it we have set up a multi-branch pipeline for our Java Maven app on Jenkins, which will automatically build and test your project across different branches. Customize the pipeline stages and steps according to your project's needs, and you'll have a powerful and automated CI/CD process in place. 

Stay tuned as we'll take this further in the new future and go a little bit deeper.