In today's rapidly evolving world of software development, implementing Continuous Integration (CI) and Continuous Deployment (CD) has become increasingly important. CI/CD pipelines ensure your applications are built, tested, and deployed efficiently and reliably. CTO.ai is a platform that enables developers to automate and optimize their workflows easily. This blog post will guide you through building a Django app and setting up a CI/CD pipeline using CTO.ai.
Prerequisites
Before we begin, ensure you have the following installed and set up on your local machine:
- Python 3.6 or later
- Django 2.2 or later
- Git
- Docker
- CTO.ai CLI, GitHub Account, and access to a CTO.ai Account
Getting Started
First, let’s create a sample Django app:
- Open your terminal and create a new directory for your project:
mkdir django-docker-example
cd django-docker-example
- Install Django an create a virtual environment
python -m venv venv
source venv/bin/activate
(venv) $ pip install django
- Create a new Django Project:
(venv) $ django-admin startproject myproject
- Move into the project directory.
(venv) $ cd myproject
- Create a new Django App
(venv) $ python manage.py startapp myapp
Now, let's create a Dockerfile and docker-compose.yml
file:
- In the root of your project directory, create a file named
Dockerfile
:
# Use an official Python runtime as a parent image
FROM python:3.9
# Set the working directory
WORKDIR /app
# Copy the requirements.txt file
COPY requirements.txt .
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Copy the rest of the application
COPY . .
# Make port 8000 available to the world outside this container
EXPOSE 8000
# Define the command to run the application using Django's development server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
- Create a
requirements.txt
file in the root of your project directory with the following content:
Django==3.2.9
- Create a
docker-compose.yml
file in the root of your project directory
version: "3.9"
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
Configure your Ops.yml file
To configure your ops.yaml
file, open a new file called ops.yml
and configure your Pipeline steps with your pipeline events and environment variables.
version: "1"
pipelines:
- name: django-workflow:0.1.5
description: django-workflow
env:
static:
- DEBIAN_FRONTEND=noninteractive
- ORG=<github_username>
- REPO=<github_repo>
- REF=main
events:
- "github:<github_username>/<github_repo>:pull_request.opened"
- "github:<github_username>/<github_repo>:pull_request.merged"
jobs:
- name: django-workflow-build
description: django workflow
packages:
- git
steps:
- echo "django workflow"
- curl -O https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
- python3 -m pip install --upgrade pip
- pip install -r requirements.txt
- python3 manage.py test
- docker build -t <image_name> .
Trigger Pipelines with GitHub Events
In my configuration, I’m using the pull_request:opened
and the pull_request:merged
event triggers. In my GitHub repo, I'll open a pull request and build my application using the PR comments workflow.
- In the comment section, your pipeline runs with all the checks passing.
View Pipelines in CTO.ai Dashboard
Your CTO.ai dashboard shows your Pipeline workflow passing with the logs from your Django Application. Your Pipeline dashboard shows the VERSION, DESCRIPTION, PIPELINE STATUS, and the ACTION that triggered it.
Conclusion
In conclusion, incorporating a CI/CD pipeline in your Django projects can significantly streamline the development process, improve code quality, and accelerate deployment. Following this blog's steps and best practices ensures your team always works with the latest, most reliable codebase version.
By embracing CI/CD in your Django projects today, you'll enhance your development workflow and foster a culture of collaboration and continuous improvement within your team.
Comments