This is the 29th day of my participation in the August Wenwen Challenge.More challenges in August

Introduction to the

Jenkins Multibranch Pipeline project types can implement different Jenkinsfiles on different branches of the same project. On the multi-branch pipelining project, Jenkins automatically discovered, managed, and executed pipelining for branches containing Jenkinsfiles in source control, eliminating the need to manually create and manage pipelining.

In practical application, Git+Jenkins is often needed to realize automatic Job construction triggered by code submission. For ordinary jobs, webhook automatically triggers. However, for the multi-branch pipeline, each Webhook may trigger the build of the master, Develop or more branches, which obviously does not meet the requirements, and will generate a lot of garbage build history, which will bring great inconvenience to the later view of the build history troubleshooting.

Therefore, we need webhooks to trigger by branch, as in our application scenario:

1. The Git repository has two branches, master and Develop, corresponding to the production environment and development environment respectively.

The develop branch commits to trigger an automatic build, while the Master branch builds manually.

Automatic builds are only for the Develop branch, so building by branch will avoid junk builds from master or other branches.

Note: This article only introduces the generic Webhook trigger part. As for the Webhook of git version library, please configure it according to the actual situation.

Automated build process

  1. The generic Webhook trigger plugin implements Jenkins triggers.

  2. Regex matches refs/heads/develop and refs/heads/master.

Ref =refs/heads/develop matches refs/heads/develop to trigger the build.

Ref =refs/heads/master, refs/heads/master, refs/heads/develop

3. Enter different stages of build in the Develop branch based on Jenkinsfile;

Since the develop branch is triggered, the when condition matches the DEVELOP branch, which builds; The master branch is skipped.

For regular matching and multi-branch pipelining, please refer to the following links:

1. www.jenkins.io/zh/doc/book… 2. Plugins. Jenkins. IO/generic – web… 3. Github.com/jenkinsci/g…

Multi-branch pipeline creation

1. Click the New Item on Jenkins’ home page

2. Name the job after Test

3. The configuration of the job

4.Jenkinsfile

Each Git branch must have the same Jenkinsfile file. Otherwise, Discovery Branches cannot be detected.

pipeline {
    agent any
  options {
        ansiColor('xterm')
        timestamps()
    }
    triggers {
        GenericTrigger (
            causeString: 'Triggered by develop', 
            genericVariables: [[key: 'ref', value: '$.ref']], 
            printContributedVariables: true, 
            printPostContent: true, 
            regexpFilterExpression: 'refs/heads/' + BRANCH_NAME, 
            regexpFilterText: 'refs/heads/develop', 
            token: 'VXnNT5X/GH8Rs'
        )
    } 
    stages {
      stage("Test deployment") {
            when {
                branch 'develop'
            }
          steps {
                echo 'develop branch'
          }
      }
      stage("Production deployment") {
            when {
                branch 'master'
            }
          steps {
                echo 'master branch'
          }
      }
    }
    post {
        unstable {
            emailext (
                body: Project name: ${JOB_NAME}\n Build number: ${BUILD_NUMBER}\n Build log: ${BUILD_URL}console""",
                subject: $JOB_NAME - Build # $BUILD_NUMBER - Unstable! ',
                to: '[email protected]'.from: '[email protected]'
            )   
        }   
        success {
            emailext (
                body: Project name: ${JOB_NAME}\n Build number: ${BUILD_NUMBER}\n Build log: ${BUILD_URL}console""",
                subject: $JOB_NAME - Build # $BUILD_NUMBER - Successful! ',
                to: '[email protected]'.from: '[email protected]'
            )   
        }   
        failure {
            emailext (
                body: Project name: ${JOB_NAME}\n Build number: ${BUILD_NUMBER}\n Build log: ${BUILD_URL}console""",
                subject: $JOB_NAME - Build # $BUILD_NUMBER - Failure! ',
                to: '[email protected]'.from: '[email protected]')}}}Copy the code

The Triggers section is the key to this multi-branch pipeline. For easy explanation, we will introduce it in detail below.

Generic Webhook Trigger plugin

The Triggers section in the Jenkinsfile is the configuration of the Generic Webhook Trigger plug-in, which can be graphically generated through the Declarative Directive Generator as follows:

1. The token configuration

Token: via http://JENKINS_URL/generic-webhook-trigger/invoke? Token =VXnNT5X/GH8Rs Can trigger the job.

2. Configure filtering

Among them:

(1) Expression: This project has two branches by matching different branches through regular Expression, namely:

refs/heads/master
refs/heads/develop
Copy the code

BRANCH_NAME in ‘refs/heads’ + BRANCH_NAME gets the current branch in the build process from the environment variable.

(2) Text: the result of the match, that is, if the result of the match by re is refs/heads/develop, the build is triggered; Otherwise, the build will not be triggered.

3. Webhook triggers builds

(1) Manually trigger the build through Webhook in the browser:

http://x.x.x.x/generic-webhook-trigger/invoke?token=VXnNT5X/GH8Rs
Copy the code

As you can see from the figure above, the Develop branch triggers an automatic build, while the Master branch does not.

(2) BlueOcean check

conclusion

Webhook triggers automatic construction according to branches to make multi-branch pipeline more flexible. It is convenient for operation and maintenance to centrally manage branches of each project and effectively avoids excessive garbage construction generated by multi-branch simultaneous construction.