DEV Community

Cover image for How to set-up a Multi-Branch Pipeline on Jenkins
Saeed H
Saeed H

Posted on

How to set-up a Multi-Branch Pipeline on Jenkins

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

  1. Select "Manage Plugins."

Image description

  1. Navigate to the "Available" tab.
  2. 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

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

Image description

Step 4: Configure Source Code Management

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

Image description

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

Image description

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

Image description

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

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

Image description

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.

Top comments (1)

Collapse
 
robinamirbahar profile image
Robina

Nice Work