Jenkins Multi Branch Pipeline (MBP) - chaitanyavangalapudi/devops-scripts GitHub Wiki

The Multi Branch Pipeline (MBP) allows you to automatically create a pipeline for all branches on SCM repository. MBP works using a Jenkinsfile that is checked-in along with your source code. A Jenkinsfile is nothing but a pipeline script that defines your CI/CD pipeline. Also the MBP is designed to trigger a build whenever there is a code push on any of the branches on your Git repository.

Install below plugins which will be useful in building/executing Jenkins Declarative pipeline for MBP.

  • Pipeline: SCM Step
  • Pipeline: Declarative
  • Pipeline: Multibranch
  • Pipeline: Multibranch with defaults
  • Multibranch Job Tear Down Plugin

Follow the instructions specified at https://jenkins.io/doc/book/pipeline/multibranch/ to create Multi Branch Pipeline for your project.

MBPs expose additional information about the branch being built through the env global variable, such as:

  • BRANCH_NAME: Name of the branch for which this Pipeline is executing, for example master.
  • CHANGE_ID: An identifier corresponding to some kind of change request, such as a pull request number

Using these variables you can build complex pipeline as follows:

Jenkinsfile

pipeline {
    agent any
    environment {
        TEMP ="dist.${JOB_NAME}-${BUILD_ID}.tar.gz"
        DIST_ARCHIVE_VAR=TEMP.replaceAll("/","-")
    }
    options {
      buildDiscarder(logRotator(numToKeepStr: '15', daysToKeepStr: '15',  artifactNumToKeepStr: '5', artifactDaysToKeepStr: '15'))
      gitLabConnection('GitlabConn')
      ansiColor('xterm')
    }

    triggers {
        gitlab(
          triggerOnPush: true,
          triggerOnMergeRequest: true,
          triggerOpenMergeRequestOnPush: "never",
          triggerOnNoteRequest: true,
          noteRegex: "Jenkins please retry a build",
          skipWorkInProgressMergeRequest: true,
          ciSkip: false,
          setBuildDescription: true,
          addNoteOnMergeRequest: true,
          addCiMessage: true,
          addVoteOnMergeRequest: true,
          acceptMergeRequestOnSuccess: false,
          cancelPendingBuildsOnUpdate: false,
          branchFilterType: 'All')
    }

    stages {
       stage('Checkout Code from SCM') {
            steps {
             echo "Pulling Code from Branch: ${env.BRANCH_NAME}"
             updateGitlabCommitStatus name: 'build', state: 'running'
             checkout scm
            }
        }
        stage('Conditional on Branch') {
            when {
              branch 'release'
            }
            steps {
                echo "Demonstration to use Branch name in conditional: ${DIST_ARCHIVE_VAR}"                
            }
        }        
    }
}

Here the generated job name ${JOB_NAME} looks like my-pipeline/release. If you want to use it in your file names, replace the "/" character with "-" to avoid any issues. You can use when conditional with expression block to add any checks on branch names as shown above in the code block.


References: