Writing a Jenkins File for Multi Branch Build Pipeline

Writing a Jenkinsfile multi-branch pipeline will help in case of setting up multiple deployment environments.

Today's software companies maintain a lot of projects and to keep development and deployment activities keep go on for each team without any friction, CICD comes into the picture.

Generally, in software development, we create two separate environments for a product: UAT and Prod. UAT is the deployment setup, which is meant for User acceptance testing and is generally used by alpha testers, developers, product managers, and internal folks of a company. On the other hand, Prod is the production environment that is used by users.

For any software product, be it an API server, microservice, web app, this is widely followed across different companies. But often one challenge faced is, building a common CICD pipeline for both the deployment environments.

Jenkins comes as a savior in such a scenario. Often the containerized applications are always deployed by first building a docker image from a branch and deploying that image in container management services like Kubernetes.

A typical Jenkinsfile would look like the following. It has multiple stages. Like the below one has two-stage. In the first stage, it builds the docker image. (Yes, the dockerfile should be available in the same hierarchy, where Jenkinsfile resides.) In the second stage, it pushed the docker file into the container registry used by the organization.

Groovy

1

pipeline {

2

  agent any

3

  stages {

4

    stage('Docker Build') {

5

      when {

6

        branch 'master'

7

      }

8

      steps {

9

        sh 'make build -e VERSION=$(git rev-parse --short HEAD)'

10

      }

11

    }

12

13

    stage('Docker Push') {

14

      when {

15

        branch 'master'

16

      }

17

      steps {

18

        sh 'make push -e VERSION=$(git rev-parse --short HEAD)

19

      }

20

    }

21

  }

22

}

23

Currently, the above file builds only if we are building the master branch. However, we would want to dedicate different branches for different deployment setups. Let’s say, the master branch for prod and staging branch for deploying to UAT.

So, like all other programming languages, Jenkins also supports conditional branching. Let’s see the below example.

Groovy

1

pipeline {

2

  agent any

3

  stages {

4

    stage('Docker Build') {

5

      steps {

6

         script {

7

          switch(GIT_BRANCH) {

8

            case "master":

9

              sh 'make build -e VERSION=$(git rev-parse --short HEAD)'

10

              break

11

            case "staging":

12

              sh 'make build -e VERSION=$(git rev-parse --short HEAD)'

13

              break

14

          }

15

        }

16

      }

17

    }

18

19

    stage('Docker Push') {

20

      environment {

21

          PROD_ENV = ''

22

          UAT_ENV  = ''

23

      }

24

      steps {

25

         script {

26

          switch(GIT_BRANCH) {

27

            case "master":

28

              sh 'make push -e VERSION=$(git rev-parse --short HEAD) -e OPTIONAL_PARAM="$PROD_ENV"'

29

              break

30

            case "staging":

31

              sh 'make push -e VERSION=$(git rev-parse --short HEAD) -e OPTIONAL_PARAM="$UAT_ENV"'

32

              break

33

          }

34

        }

35

      }

36

    }

37

  }

}



So this Jenkinsfile would build docker image from master or staging branch, depending on the branch, Jenkins build has been triggered. Point to be noted, we can also pass additional arguments (like we sent in lines no. 28 and 31) based on the branch being built in Jenkins.

This was just a very short example of how Jenkins can be used for multi-branch CICD from a single repository. However, it is highly recommended to read the wonderful documentation provided by Jenkins itself. Thank you.



Relevant Blogs:

Testing CI/CD for Cloud-Native Applications 

How To Boost Your In-house Enterprise DevOps Talent? GitOps: What Is It and How Does It Work? 

5 Steps To Implement DataOps Within Your Organization

Recent Comments

No comments

Leave a Comment