CI/CD Pipeline on AWS using Jenkins

CI/CD Pipeline on AWS using Jenkins

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

  1. Prerequisites

  2. AWS EC2 Instance Setup

  3. Jenkins Installation

  4. GitHub Webhook Configuration

  5. Django Todo App Repository

  6. Jenkins Job Configuration

  7. Verifying Changes

  8. Conclusion

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

  1. Create an EC2 Instance:

    • Log in to the AWS Management Console.

    • Launch a new EC2 instance with the desired specifications.

  2. Security Group Configuration:

    • Configure security groups to allow inbound traffic on port 8080 for Jenkins and any other necessary ports.
  3. Key Pair:

    • Create or use an existing key pair for secure access to the EC2 instance.

Jenkins Installation

  1. Connect to EC2 Instance:

    • Connect to the EC2 instance using SSH.
  2. Install Jenkins:

    • Update package lists: sudo apt update

    • Install Jenkins: sudo apt install jenkins

  3. Start Jenkins Service:

    • Start the Jenkins service: sudo systemctl start jenkins
  4. Enable Jenkins on Boot:

    • Enable Jenkins to start on boot: sudo systemctl enable jenkins
  5. Retrieve Initial Admin Password:

    • Retrieve the initial admin password from /var/lib/jenkins/secrets/initialAdminPassword.
  6. 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

  1. GitHub Repository Settings:

    • In your GitHub repository, navigate to "Settings" -> "Webhooks" -> "Add webhook."

  2. 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/).

  3. Content Type:

    • Set the Content type to "application/json."
  4. Webhook Events:

    • Set the webhook to trigger on push and pull request events.

Django Todo App Repository

  1. 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

  1. Create a New Jenkins Job:

    • In the Jenkins dashboard, click "New Item" and select "Freestyle project."

    • Configure the job with a meaningful name.

  2. Configure Source Code Management:

    • Choose your version control system (e.g., Git) and provide the repository URL.

  3. Configure GitHub Webhook:

    • In the Jenkins job configuration, go to the "Build Triggers" section.

    • Check "GitHub hook trigger for GITScm polling."

  4. 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