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.
Join the DZone community and get the full member experience.
Join For FreeToday'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.
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.
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.
Published at DZone with permission of Samanway Dey. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments