Learn How to Create a CI Pipeline for Your Django Application

Learn How to Create a CI Pipeline for Your Django Application

Table of contents

Continuous Integration (CI) has become an integral part of the software development process, allowing developers to automate testing, building, and deployment of their applications. CI helps to catch bugs and errors early in the development process, reducing the likelihood of issues arising in production. In this tutorial, we will explore how to create a CI pipeline for a Django application. We will set up a pipeline and define stages and steps. By the end of this tutorial, you will have a clear understanding of how to set up a CI pipeline for your Django application, enabling you to streamline your development process and improve the quality of your code.

Prerequisites

Tutorial

First, sign in to your Harness account. Harness is a continuous delivery platform.

signup Harness

Get started with creating your first pipeline

first pipeline

Authenticate with your GitHub account as your application code is present there.

add github

authenticate with GitHub

Select your application repository. The sample Django application repo you forked above.

django forked

Continue to configure your pipeline. Next, we need to select the language of the application.

configure pipe

For our Django application, we need to select Python. Let's do it:)

As soon as you select the language, on the right-hand side, you should see the default yaml created for your pipeline.

python program

Let's continue to create our pipeline. After you click on 'create pipeline' from the previous step, you will land on this pipeline studio.

pipeline studio

Click on the 'Build Python App' stage and you should see the step 'build python app' under execution.

build python project

[Don't worry about the overview and infrastructure. In the Harness cloud accounts, Harness will take care of the infrastructure.]

Click on the 'Build Python App' step under the execution tab, and you should see the settings and default commands configured already.

commands configured

Let's replace the commands with our new ones shown below,

pip install Django
pip install virtualenv
python manage.py test

django commands

Apply changes, save and run the pipeline.

run pipeline

In just no time, you should see a successful pipeline execution.

django successful

Let's push our Django application to a container registry; we will use the Docker Hub registry in this tutorial.

To push our application as an image to Docker Hub, we need to have a Dockerfile with all the instructions. Our sample Django application has the Dockerfile in it.

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY myapp myapp/

EXPOSE 8000

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

Let's go back to the pipeline studio and add another step under the 'Execution' tab.

docker hub step

From the step library, select 'Build and Push an image to Docker Registry'.

build and push to dockerhub

At this point, this is how your pipeline looks.

pipeline looks

Edit the 'Build and Push an image to Docker Registry' step by clicking on it. Add the details required.

push details

One of the settings we need to add is our Docker Hub connector. Let's add it.

new connector

connector name

Add the registry link, and your Docker Hub username and make sure to add the Docker Hub password as a secret to keep it as a secret:)

details of registry

Make the connection with Harness so both Harness platform and your Docker Hub talk to each other.

connection

Make sure the connection is successful.

connection is successful

Now, in the docker Connector list, you can see our recently added Docker Hub connector.

docker reg connector

Now, go back to the 'Build and Push an image to Docker Registry' step and add the details such as the Docker Hub connector that we added, Docker Hub repo (your username/name of the image you would like to give), and tag.

build details

Apply changes and save the pipeline.

save pipeline

Now we are all set. Let's run the pipeline to see it executing successfully.

pipeline executing

pipeline steps complete

Let's verify if our image has been pushed by going to our Docker Hub account.

image pushed

Congratulations! We successfully created a CI pipeline for our sample Django application.

The pipeline automation is already configured by default. You can see it by going to the 'Triggers' tab in the pipeline studio.

Triggers added

Now, whenever a developer pushes any code to the main branch, the build gets automatically triggered and the pipeline starts to run, carrying out all the steps involved.

Well, if you like to learn how to set up a complete CI/CD pipeline, I have a tutorial. {% embed dev.to/pavanbelagatti/learn-how-to-setup-a-.. %}