Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Jenkins Multi-Branch Pipeline

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to use Jenkins multi-branch pipeline, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The Jenkins multi-branch pipeline is one of the best ways to design CI/CD workflows because it is entirely based on git's pipeline as code.

Multi-branch pipeline is a concept that automatically creates Jenkins Pipeline based on Git branches. This means that it can automatically discover a new Git branch when it is created in SCM (Github) and automatically create pipes for that branch. When the pipeline construction starts, Jenkins uses Jenkinsfile in the branch for the build phase.

SCM can be a Github,Bitbucket or Gitlab repository.

You can choose to exclude if you do not want the selected branch to appear in an automatic pipe with Java regular expressions. The multi-branch pipeline supports PR-based branch discovery. This means that if someone makes a PR (pull request) from a branch, the branch is automatically found in the pipeline. If this configuration is enabled, the build is triggered only when PR is mentioned. Therefore, if you are looking for a PR-based Jenkins build workflow, this is a good choice.

You can add conditional logic to Jenkinsfile to build jobs based on branch requirements. For example, if you want the feature branch to run only unit tests and Sonar analysis, you can set conditions to skip the deployment phase using the when condition, as shown below.

Therefore, whenever a developer commits PR from a functional branch to another branch, the pipeline runs the unit test and Sonar analysis phase, skipping the deployment phase. Moreover, multi-branch pipelining is not limited to continuous delivery of applications. You can also use it to manage infrastructure code.

How does a multi-branch pipeline work?

I'll guide you through the basic build and deployment workflow to understand how multi-branch pipes work.

Suppose I want the Jenkins pipeline to build and deploy the application under the following conditions.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Developers start with the feature branch by submitting code to the feature branch.

Whenever a developer brings up PR from a functional branch to develop a branch, the Jenkins pipeline should be triggered to run unit tests and static code analysis.

After successfully testing the code in the functional branch, the developer merged the PR into the development branch.

When the code is ready for release, the developer brings PR from the develop branch to master. It should trigger a build pipeline that runs unit test cases, code analysis, and deployment to the dev / QA environment.

As you can see from the above conditions, there is no case where the Jenkins job is triggered manually, and whenever there is a branch request, you need to automatically trigger the pipeline and run the required steps for that branch. This workflow establishes a good feedback loop for engineers and avoids relying on DevOps teams to build and deploy in a non-production environment. Developers can check the build status on Github and decide what to do next.

This workflow can be easily implemented through the Jenkins multi-branch pipeline. The following figure shows the appearance of the multi-branch pipeline workflow for the above example build process

This is how multi-branch pipes work.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

When a developer creates a PR from a functional branch to develop a branch, Github sends the Webhook with PR information to Jenkins.

Jenkins receives the PR, finds the relevant multi-branch pipe, and automatically creates the branch pipe. It then runs the job according to the steps mentioned in Jenkinsfile in the functional branch. During checkout, the source and destination branches in the PR are merged. The PR merge will be blocked on Github until the build status is returned from Jenkins.

After the build is complete, Jenkins updates the status to Github PR. Now you will be able to merge the code. In addition, if you want to view the Jenkins build log, you can find the link to the Jenkins build log in the PR state.

Multi-branch Pipleline Jenkinsfile

Before starting the implementation, let's take a look at the multi-branch pipeline Jenkins example Jenkinsfile that can be used in pipes.

For the multi-branch pipeline to work properly, you need to include Jenkinsfile in the SCM repository.

If you are learning / testing, you can use the multi-branch pipeline Jenkinsfile provided below. It has a checkout phase and other phases that echo messages.

In addition, you can clone and use the Github repository with this Jenkinsfile

Note: replace the agent label "master" with your Jenkins agent name. Master can also work, but it is not recommended to run in a real project environment.

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 "}"

Set up Jenkins multi-branch pipe

Here, I'll walk you through the step-by-step process of building a multi-branch pipeline on Jenkins. This setting will be based on Github and the latest version of Jenkins 2.x. You can also use Bitbucket or Gitlab as the SCM source for multi-branch pipes.

Step 1: create a "new project" on the Jenkins home page.

Step 2: select Multi-Branch Pipe from the options, and then click OK.

Step 3: click add Source, and then select Github.

Step 4: under the Authentication field, select Jenkins and create an authentication using your Github username and password.

Step 5: select the credentials you created, and then provide your Github repository to verify the credentials, as shown below.

If you are testing the multi-branch pipeline, you can clone the demo Github repository and use it. Https://github.com/devopscube/multibranch-pipeline-demo .

Step 6: select the options you want to meet your requirements. You can choose to discover all branches in the repository, or you can select only branches with pull requests.

Pipes can also find branches with PR in bifurcated warehouses.

Choosing these options depends on the desired workflow.

You can select a different behavior from the add button.

For example, if you choose not to discover all branches from the repository, you can choose a regular expression or wildcard method to discover branches from the repository, as shown below.

This is an example of regular expressions and wildcards.

Step 7: if you choose to use a different name for Jenkinsfile, you can do so by specifying the name in the build configuration. In the script path option, you can provide the name you want. Make sure that the Jenkinsfile in the warehouse is the same as the name you provided in the pipe configuration.

In addition, enable discard the old version to keep only the required build log, as shown below.

Step 8: save all job configurations. Jenkins scans the configured Github repository to find all branches that have promoted PR.

The following figure shows the job of scanning three branches, and because I didn't make any pull requests, Jenkins won't create any branch-based pipes. I'll show you how to test automatic pipe creation after setting up Webhook.

So far, we have completed the configuration in Jenkins and can scan the branches according to the PR request. In order to have a complete workflow, we need to configure a Webhook in Github to send all events (submission, PR, etc.) to Jenkins because the pipeline can be triggered automatically.

Configure Webhook for multi-branch pipes

Follow these steps to set up Jenkins Webhook on the repository.

Step 1: go to the Github repository and click Settings.

Step 2: select the webhook option on the left, and then click the add Webhook button.

Step 3: add your Jenkins URL under the payload URL, followed by "/ github-webhook /". Select the content type as "application/json" and click "add Webhook"

Note: you can choose the type of Webhook you want to receive in Jenkins. For example, you only want to trigger the pipe during PR. You can then select only the PR event from the Let me select a single event option.

You will see a green check mark on the successful Webhook configuration, as shown below.

If you don't see a green check mark or warning sign, click the Webhook link, and then click the last Webhook. You should be able to use the status code to see why the Webhook delivery failed.

We have now completed all the necessary configurations for the multi-branch pipeline. The next step is to test the multi-branch pipeline workflow trigger.

Test multi-branch pipeline

For demonstration purposes, I selected the "use branches only as branches of PR" option. Using this option, only branches with PR requests are found.

To use a multi-branch pipeline, you can use this buyback with the sample Jenkinsfile. Https://github.com/devopscube/multibranch-pipeline-demo

This warehouse has three branches. Update something in the README file in the feature branch and improve the PR for development. It will send a Webhook to Jenkins, and Jenkins will send back Jenkins's work details, and PR will enter the check state, as shown below.

If you click details, it will take you to the Jenkins build log. You can write custom checks in your Jenkins file for use in building audits.

Now, if you select Jenkins, you will find the pipeline for the functional branch in Jenkins, as shown below.

If the build fails, you can commit the changes to the feature branch, and as soon as the PR is opened, it will trigger the feature pipeline.

In Jenkinfile, if the branch is not developed, I add a condition to skip the deployment phase. You can check in the Jenkins build log. In addition, if you check the build process in the Blue Ocean dashboard, you can clearly see the deployment phase that is skipped, as shown below.

Now merge the functional branch PR and promote the new PR from development to master branch.

Jenkins will receive a Webhook from Github to get the new PR and create the development pipeline as shown below.

For the development branch, the deployment phase is enabled, and if you examine the build process of the Blue Ocean, you can see that all phases have been successfully triggered.

Troubleshoot multi-branch pipes

I will discuss some errors that may be encountered in the multi-branch pipeline and how to resolve them.

Branch discovery problem

Sometimes, even after a new branch is created in SCM, it may not be reflected in the Jenkins pipeline. You can try running the scan Repository now option to scan the repository again. In addition, check the repository scan configuration in the pipeline.

Webhooks will not trigger the pipe

When Webhook does not trigger the pipeline, check the Webhook delivery status code and errors in Github. Also, check that the Jenkins URL is correct.

Also check the Jenkins log from the Manage Jenkins- > System Logs- > All Jenkins log. If Jenkins is able to receive Webhook, the log should show the reason why the job was not triggered.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report