DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Writing a Jenkins File for Multi Branch Build Pipeline

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.

Samanway Dey user avatar by
Samanway Dey
·
Mar. 17, 22 · DevOps Zone · Analysis
Like (2)
Save
Tweet
3.21K Views

Join the DZone community and get the full member experience.

Join For Free

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
pipeline {
  agent any
  stages {
    stage('Docker Build') {
      when {
        branch 'master'
      }
      steps {
        sh 'make build -e VERSION=$(git rev-parse --short HEAD)'
      }
    }

    stage('Docker Push') {
      when {
        branch 'master'
      }
      steps {
        sh 'make push -e VERSION=$(git rev-parse --short HEAD)
      }
    }
  }
}

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
 
pipeline {
  agent any
  stages {
    stage('Docker Build') {
      steps {
         script {
          switch(GIT_BRANCH) {
            case "master": 
              sh 'make build -e VERSION=$(git rev-parse --short HEAD)'
              break
            case "staging": 
              sh 'make build -e VERSION=$(git rev-parse --short HEAD)'
              break
          }
        }
      }
    }

    stage('Docker Push') {
      environment {
          PROD_ENV = ''
          UAT_ENV  = ''
      }
      steps {
         script {
          switch(GIT_BRANCH) {
            case "master": 
              sh 'make push -e VERSION=$(git rev-parse --short HEAD) -e OPTIONAL_PARAM="$PROD_ENV"'
              break
            case "staging": 
              sh 'make push -e VERSION=$(git rev-parse --short HEAD) -e OPTIONAL_PARAM="$UAT_ENV"'
              break
          }
        }
      }
    } 
  }
}

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

Branch (computer science) CI/CD Jenkins (software) Pipeline (software) Docker (software) Kubernetes Acceptance testing master Software

Published at DZone with permission of Samanway Dey. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Now It's Time to Uncomplicate With the Not-So-New API in Java
  • 5 Benefits of Electronic Data Interchange
  • Data Mesh — Graduating Your Data to Next Level
  • 6 Reasons Cybersecurity Awareness Training is Important

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo