Writing a Jenkins file for multi branch build pipeline

Samanway Dey
2 min readOct 7, 2021

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 which 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 following. It has multiple stages. Like the below one has two stage. In first stage it builds the docker image. (Yes, the dockerfile should be available in the same hierarchy, where Jenkinsfile resides.) In second stage, it pushed the docker file into the container registry used by the organization.

Currently the above file build only if we are building the master branch. However, we would want to dedicate different branch for different deployment setups. Let’s say, 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.

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

--

--

Samanway Dey

Develops, maintains and talks about software products