DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • A Concise Guide to DevSecOps and Their Importance in CI/CD Pipeline
  • Zero to Hero on Kubernetes With Devtron

Trending

  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • End-to-End Event Streaming With Kafka, Spring Boot and AWS SQS/SNS (Production-Ready Code Guide)
  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. 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.

By 
Samanway Dey user avatar
Samanway Dey
·
Mar. 17, 22 · Analysis
Likes (2)
Comment
Save
Tweet
Share
3.9K 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) Continuous Integration/Deployment 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.

Related

  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Implementing CI/CD Pipelines With Jenkins and Docker
  • A Concise Guide to DevSecOps and Their Importance in CI/CD Pipeline
  • Zero to Hero on Kubernetes With Devtron

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook