Deploy a Django image to Jenkins using a multi-branch pipeline and conditionals.

Deploy a Django image to Jenkins using a multi-branch pipeline and conditionals- a simple guide.

This is the fifth article in the series Deploy a Django app to AWS EC2 instance using Jenkins. A gentle guide to Jenkins..

In this article, you will build and configure a multi-branch Jenkins pipeline that will deploy a Django image to Dockerhub. You are required to have gone through the other articles in the series beforehand.

Now, you might wonder, what exactly is a multi-branch pipeline? The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.

This eliminates the need for manual Pipeline creation and management.

1. Create a multi-branch pipeline on Jenkins.

Head over to your Jenkins dashboard and click on the new item tab. Continue with the creation by giving the job a name and selecting the multi-branch pipeline option.

Screenshot from 2022-09-03 11-28-18.png

Upon successful creation, click on the branch sources tab and on the drop-down, select either Git or GitHub depending on the version control in use.

Screenshot from 2022-09-03 11-33-13.png

Now onto configuring the sources after selection. Attach the GitHub credentials you had earlier configured, and add your repo name in the project URL input box. On the discover branches dropdown, select the Filter by name (with regular expression) option. In the input, add the following regex

.* // dynamically discovers and loads all the branches (master, feature, hot fix etc)

Click on save and it should scan your GitHub repo branches if any Jenkins file exists and build the job.

Screenshot from 2022-09-03 11-44-14.png

You have now successfully created and configured your multi-branch pipeline.

2. Use conditionals in Jenkins.

Let's mimic a production workflow and you want to push the Django image to Dockerhub for the staging environment only. The below steps will guide you on how to do it.

Create and switch into a staging branch by using the commands.

 git switch -c "feat-staging"

Confirm that you are in the staging branch by

git branch

All good now, now shift the focus to the JenkinsFile, suppose you want to only push our docker image to Dockerhub when in the staging branch, how can that be achieved? Enter the use of the when block.

stage('build') { 
            when { 
                expression { 
                    BRANCH_NAME == 'staging' //this expression make sure that this step is only run when the branch name is staging

                script { 

You have now successfully created, and configured a multi-branch pipeline in Jenkins and learnt how to use conditionals in Jenkins. In the next article, you will learn how to install, configure and use sonarqube in Jenkins.

Happy hacking!