Skip to main content

How to Assembling a Continuous Integration Service for a Django project on Jenkins

Project installation

First of all, let’s clone the project:
git clone https://bitbucket.org/suporte_concrete/cs-django-jenkins.git
Afterward, create a new Virtualenv* for our project:
virtualenv ~/projetos/virtualenvs/blog
Activate the Virtualenv:
source ~/projetos/virtualenvs/blog/bin/activate
*But what is a Virtualenv?
It is a tool used to create python isolated environments. That allows us to install the project’s dependencies without interfering on the dependencies from the OS’s global scope. To know more, visit this site.
Install the project’s dependencies::
pip install -r requirements.txt
Now that we have our Virtualenv with the needed packages installed, let’s run a command to run our database migrations:
python manage.py migrate
Once this is done, we can now run our project.

Running the project locally

To run the project, execute:
python manage.py runserver
Warning: this command should only be executed in a development environment. If you wish to run this project in Production, you must be relying on services Apache + modwsgi, gunicorn + Nginx, etc.

Executing the tests

To run the tests, execute:
python manage.py test
The result must be like this:
All tests passing! :D
Now that we already know how to prepare the project and execute the tests, we are going to install Jenkins on the Continous Integration server.

Preparing the server

Installing the dependencies
We must install some packages for Jenkins to be able to create the environment and build the project. Install the following packages using apt-get:
sudo apt-get install -y git python-pip python-virtualenv
The next step is to install Jenkins on the server. As for this post we are using a Ubuntu instance, we can easily install using apt-get:
# Add the Jenkins repository key
wget -q -O — https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c ‘echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list’
sudo apt-get update
sudo apt-get install jenkins
After the installation, check if the server is running with the command:
sudo service jenkins status
If the server is running correctly, you will receive this message:
Jenkins Continuous Integration Server is running with the pid XXX
To start the service, run:
sudo service jenkins start
Now we can access Jenkins on port 8080 from the server. Go to your browser and go to localhost:8080 (where localhost is the name or the IP of your server).

Dependencies

Jenkins by default do not access git repositories, so we must install a Git plugin. Go to the Jenkins server, select the “Manage Jenkins” option and on the next page click on “Manage Plugins”:
On the plugins page, click on the “Available” tab and then filter by “git plugin”:
Select the plugin and click on “Download now and install after restart”
Installing plugins
Warning: If the page stops responding after a while, go back to the Jenkins home manually because the installation might be already done. ☹

Configuring our Job

Now with our Jenkins server installed with the Git plugin, we must configure our job for Jenkins to be able to poll our repository for changes.

Initial configuration

On Jenkins homepage, click on “New Item” to create a new Job. Write the job name and select the “Freestyle project” option:
On the next page, select the “Discard Old Builds” option so the older builds to be discarded automatically.
Accessing the repository:
This step is vital. Here we are going to select the type of the repository (Git, Subversion, CVS), address, which branch to pick up and so on. As our project is a git repository, select the Git option and type the Repository URL:
Warning: the URL from this repository is public. In a private repository, you will probably need to configure a ssh key on the server so Jenkins can have access to the repository.

Build Triggers

Here we can schedule the Job according to our needs. In this case, I chose to poll the repository on every 15 minutes. If there is a change, Jenkins will download it and do the tests.

Build

After downloading the newer version, Jenkins must run the tests to finally validate our project. Let’s add a step on Build. In order to configure we will add a small shell script.
Our shell script for the Job:
#!/bin/bash
export WORKSPACE=`pwd`
# Create/Activate virtualenv
virtualenv venv
source venv/bin/activate
# Install Requirements
pip install -r requirements.txt
# Run tests
python manage.py test
The Job configuration is now done, so click on save! ☺

Is it working?

As you can see, Jenkins polled our Bitbucket repository, downloaded the latest version and ran the tests:

Popular posts from this blog

How to read or extract text data from passport using python utility.

Hi ,  Lets get start with some utility which can be really helpful in extracting the text data from passport documents which can be images, pdf.  So instead of jumping to code directly lets understand the MRZ, & how it works basically. MRZ Parser :                 A machine-readable passport (MRP) is a machine-readable travel document (MRTD) with the data on the identity page encoded in optical character recognition format Most travel passports worldwide are MRPs.  It can have 2 lines or 3 lines of machine-readable data. This method allows to process MRZ written in accordance with ICAO Document 9303 (endorsed by the International Organization for Standardization and the International Electrotechnical Commission as ISO/IEC 7501-1)). Some applications will need to be able to scan such data of someway, so one of the easiest methods is to recognize it from an image file. I 'll show you how to retrieve the MRZ infor...

How to generate class diagrams pictures in a Django/Open-edX project from console

A class diagram in the Unified Modeling Language ( UML ) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects. https://github.com/django-extensions/django-extensions Step 1:   Install django extensions Command:  pip install django-extensions Step 2:  Add to installed apps INSTALLED_APPS = ( ... 'django_extensions' , ... ) Step 3:  Install diagrams generators You have to choose between two diagram generators: Graphviz or Dotplus before using the command or you will get: python manage.py graph_models -a -o myapp_models.png Note:  I prefer to use   pydotplus   as it easier to install than Graphviz and its dependencies so we use   pip install pydotplus . Command:  pip install pydotplus Step 4:  Generate diagrams Now we have everything installed...

How to Remove course from Open-edX

Go to vagrant  => 1. In the edx-platform directory:  - cd /edx/app/edxapp/edx-platform 2. Run the following Django management command:   - sudo -u www-data /edx/bin/python.edxapp /edx/bin/manage.edxapp lms dump_course_ids --settings aws    - sudo -u www-data /edx/bin/python.edxapp /edx/bin/manage.edxapp lms dump_course_ids --settings=devstack 3. Find the course ID which you'd like to delete in the resulting list of course IDs. 4. Copy the course ID into the following command and run it:  - sudo -u www-data /edx/bin/python.edxapp /edx/bin/manage.edxapp cms delete_course <COURSE_ID> --settings aws  -   sudo -u www-data /edx/bin/python.edxapp /edx/bin/manage.edxapp cms delete_course <COURSE_ID> --settings=devstack  - You'll be asked to verify the deletion . To verify the deletion, run the command from step 2 above and ensure that the course ID is not in the list. Help reference : ...