Learn How to Create a CI Pipeline for Your Django Application
Tags: django, tutorial, python, devops
Published: 2023-03-30 13:15:15

[Continuous Integration](https://dev.to/pavanbelagatti/a-step-by-step-guide-to-continuous-integration-for-your-nodejs-application-3bhc) (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
- [Free Harness account](https://app.harness.io/auth/#/signup/?module=ci&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_django_article&utm_term=get-started) to setup CI/CD for our application.
- Sample Django application. We have already created a [sample Django application] (https://github.com/pavanbelagatti/django-app-tutorial); with a simple test case. Feel free to fork and use the repo for this tutorial.

## Tutorial
First, sign in to your Harness account. Harness is a continuous delivery platform. 
![signup Harness](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6lyk0wxigi9a2kqak2fi.png)

Get started with creating your first pipeline
![first pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6rgri1gpcbmbt8q2jx1.png)

Authenticate with your GitHub account as your application code is present there.
![add github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcdkkrm132wok2r37his.png)

![authenticate with GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1efbg84t05szpe9lh1c.png)

Select your application repository. The [sample Django application](https://github.com/pavanbelagatti/django-app-tutorial) repo you forked above.
![django forked](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wu3vl7aia583556g5kh.png)

Continue to configure your pipeline. 
Next, we need to select the language of the application. 
![configure pipe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xpz4r96wzq59gb7zj5i.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pgoswacqxjtvaw9sktwl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr8gpb7s08ep9jtr7txy.png)

Click on the 'Build Python App' stage and you should see the step 'build python app' under execution.
![build python project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi9bpji8t8yundeqzqh3.png)

[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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6j06f6okoelzz0zhmsv.png)

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

```
pip install Django
pip install virtualenv
python manage.py test
```
![django commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txeegjczoh5vyga08kmo.png)

Apply changes, save and run the pipeline.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0ahqir0yqjd5nmfeqa6.png)

In just no time, you should see a successful pipeline execution.
![django successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17qimnj80yk3mgbgtxx5.png)

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](https://github.com/pavanbelagatti/django-app-tutorial) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlznnbl2dcw9rh6crn4u.png)

From the step library, select 'Build and Push an image to Docker Registry'.
![build and push to dockerhub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nx4khi3xymd5fhoisg3n.png)

At this point, this is how your pipeline looks.
![pipeline looks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e89i29m0ud5huqfgeioi.png)

Edit the 'Build and Push an image to Docker Registry' step by clicking on it. Add the details required.
![push details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3fh2m9m2qgajwjptcwr.png)

One of the settings we need to add is our Docker Hub connector. Let's add it.
![new connector](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfu7yc7st20qerhlrfok.png)

![connector name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10n17ma2ugf081kscwxp.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdbgeoyyql39naxapt6o.png)

Make the connection with Harness so both Harness platform and your Docker Hub talk to each other.
![connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okzt8kak5ntfva8z9cep.png)

Make sure the connection is successful.
![connection is successful](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ouamkt02f0sx3vn4pn2.png)

Now, in the docker Connector list, you can see our recently added Docker Hub connector. 
![docker reg connector](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o357o7ulpnm4zqztcqp8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zroh21imcad0dez7qw9i.png)

Apply changes and save the pipeline.
![save pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3v07z8ibw3h81v7njn1.png)

Now we are all set. Let's run the pipeline to see it executing successfully. 
![pipeline executing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gts33yceyznd9t2dok9n.png)
![pipeline steps complete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n87zjuuq3fl7gbwj5gyq.png)

Let's verify if our image has been pushed by going to our Docker Hub account.
![image pushed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gb6b2qe07069fcznwru.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/anok32jwpktcr8bnpwmm.png)

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 https://dev.to/pavanbelagatti/learn-how-to-setup-a-cicd-pipeline-from-scratch-for-a-go-application-4m69 %}