Setting Up CI/CD Pipeline for Django Todo App on AWS EC2 with Jenkins
In this comprehensive guide, we will explore the step-by-step process of establishing a robust Continuous Integration and Continuous Deployment (CI/CD) pipeline for a Django Todo app. The pipeline is hosted on an AWS EC2 instance and leverages Jenkins to automate the build and deployment process whenever changes are pushed to the associated GitHub repository.
Table of Contents
Prerequisites
Before diving into the CI/CD setup, ensure the following prerequisites are met:
AWS EC2 Instance: Set up an EC2 instance on AWS to host the Jenkins server.
Jenkins Installation: Install Jenkins on the EC2 instance to facilitate CI/CD.
GitHub Webhook: Configure a GitHub webhook to trigger the Jenkins job on code changes.
Django Todo App Repository: Have a GitHub repository containing the Django Todo app code along with the Docker Compose file.
AWS EC2 Instance Setup
Create an EC2 Instance:
Log in to the AWS Management Console.
Launch a new EC2 instance with the desired specifications.
Security Group Configuration:
- Configure security groups to allow inbound traffic on port 8080 for Jenkins and any other necessary ports.
Key Pair:
- Create or use an existing key pair for secure access to the EC2 instance.
Jenkins Installation
Connect to EC2 Instance:
- Connect to the EC2 instance using SSH.
Install Jenkins:
Update package lists:
sudo apt update
Install Jenkins:
sudo apt install jenkins
Start Jenkins Service:
- Start the Jenkins service:
sudo systemctl start jenkins
- Start the Jenkins service:
Enable Jenkins on Boot:
- Enable Jenkins to start on boot:
sudo systemctl enable jenkins
- Enable Jenkins to start on boot:
Retrieve Initial Admin Password:
- Retrieve the initial admin password from
/var/lib/jenkins/secrets/initialAdminPassword
.
- Retrieve the initial admin password from
Jenkins Setup:
Access Jenkins in a web browser at
http://<your-ec2-instance-public-ip>:8080
.Complete the Jenkins setup process using the initial admin password.
GitHub Webhook Configuration
GitHub Repository Settings:
In your GitHub repository, navigate to "Settings" -> "Webhooks" -> "Add webhook."
Payload URL:
Set the Payload URL to your Jenkins server's URL with
/github-webhook/
at the end (e.g.,http://<your-ec2-instance-public-ip>:8080/github-webhook/
).
Content Type:
- Set the Content type to "application/json."
Webhook Events:
- Set the webhook to trigger on push and pull request events.
Django Todo App Repository
Code and Docker Compose:
- Ensure your GitHub repository contains the Django Todo app code along with the Docker Compose file. My docker-compose.yml file looks something like:
version : "3.3"
services :
web :
build : .
ports :
- "8000:8000"
volumes:
- ./volume/store_data/db.sqlite3:/data/db.sqlite3
Jenkins Job Configuration
Job Setup
Create a New Jenkins Job:
In the Jenkins dashboard, click "New Item" and select "Freestyle project."
Configure the job with a meaningful name.
Configure Source Code Management:
Choose your version control system (e.g., Git) and provide the repository URL.
Configure GitHub Webhook:
In the Jenkins job configuration, go to the "Build Triggers" section.
Check "GitHub hook trigger for GITScm polling."
Build Steps:
Add build steps with the following shell commands:
# Jenkins Job Shell Commands # Stop existing containers docker-compose down # Build Docker image docker-compose build # Start Docker containers docker-compose up -d
Verifying Changes
After the Jenkins job is triggered and completes successfully, verify the changes by accessing the hosted Docker container on the EC2 instance at port 8000.
Conclusion
Setting up a CI/CD pipeline for the Django Todo app on AWS EC2 with Jenkins streamlines the development and deployment process. This comprehensive guide outlines the steps taken to achieve this setup, providing insights into the CI/CD workflow.
Feel free to adapt and customize this pipeline according to your project's requirements. Happy coding!
My GitHub repository: https://github.com/anandamideShakyan/django-todo-cicd
Credits for the code repository: https://github.com/shreys7/django-todo