Integrating Django with Jenkins

In this article we will focus more on using Django and Jenkins together. Of course I will explain little things in short like installing Python or Django.

We at NonStopIO Technologies, wanted to automat unit testing, and creating the builds. After the initial round of discussion with Saurabh Gawande and Sumit M we picked up Jenkins as our continuous integration tool and following is our learning from it.

Install Jenkins:

You can install Jenkins for your OS from this link

Installing Python:

Most of the systems with OS like MacOS or Ubuntu already have python installed. You may need to update python to latest version. To do so follow below steps:

Mac:
brew udpate
brew install python3

Ubuntu:
sudo apt-get update
sudp apt-get install python3

Install Django:

Follow this link for deep instructions for getting started with Django.

Create a folder MyProject in your workspace

Create virtual environment in MyProject as:
virtualenv myenv

start virtual environment as:
source myenv/bin/activate

Install Django as:
pip install Django

create a project DjangoJenkins
django-admin startproject DjangoJenkins

create an app MyApp
django-admin startapp MyApp

Install jenkins’ plugins:

pip install django-jenkins
pip install pep8
pip install pyflakes

Added jenkins in settings.py:

In the file DjangoJenkins->DjangoJenkins->settings.py->INSTALLED_APPS add django_jenkins

in settings.py add below lines:

PROJECT_APPS = (
 ‘MyApp’
)

JENKINS_TASKS = (
 ‘django_jenkins.tasks.run_pep8’,
 ‘django_jenkins.tasks.run_pyflakes’
)

*Skipping steps to make only Django running. Follow https://www.djangoproject.com/start/ for the same*

Once you have Django and Jenkins up and running, you can start configuring Jenkins for Django now:

Start Jenkins.

Go in Manage Jenkins -> Manage Plugins -> Available Tab

search for following plugins and intstall them:
1. Bitbucket Plugin (My repository is on Bitbucket. Hence!)
2. Violations plugin
3. Cobertura Plugin

Create new item:

Give name to the item: DjangoJenkinExample
Select Freestyle project
Click Ok

In Source code Management, select Git and write as follows:

In Build Triggers, select Poll SCM
and write
H * * * *
in the text field.

It will check for new push in the repo after every hour

In Build, select exucute shell and white below lines:

#!/bin/bash

virtualenv -p python3 myenv
source myenv/bin/activate
pip3 install -r requirements.txt
cd reports
touch *.xml
touch *.report
cd ..
python3 manage.py jenkins — enable-coverage

In Post-build Actions select Publish Cobertura Coverage Report:
specify the coverage.xml file path. In your case it would be reports/coverage.xml

Again select Publish JUnit test result report:
specity junit.xml file path. In your case it would be reports/junit.xml

Again and I swear it is the last time
select Report Violations

specify pep8.report file in pep8 row:

click on save

Lets get back to django to create these report files:
These files are created by the jenkins plugin we installed in Django.

To create these files:

activate your virtuial environment
run below code:

python3 manage.py jenkins

It will start test and create report files.

Come back to the Jenkins.
open the item DjangoJenkinExample

click Build Now

DONE!!

Like what you read? Give Arnold Parge a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.