PROJECT-1: Django application deployment using Jenkins

PROJECT-1: Django application deployment using Jenkins

Creation of EC2:

  1. Create an instance,

  2. Open the below ports as it is required to run our applications.

    Port 8000 - Django Application

    Port 8080 - Jenkins

  3. SSH to EC2/VM,

First, we'll clone and understand the dockerfile in the code directory.

  1. Update the ubuntu

     sudo apt update
    
  2. Clone the code from github

    Copy the code URL

    Paste with git clone command in the VM,

     git clone https://github.com/prasadtara09/django-notes-app.git
    

  3. Go inside the django-notes-app directory and cat the dockerfile

  4. Understand the Docker file

Installing Jenkins in the VM

  1. Become super user.

     sudo apt update
     sudo su
    
  2. Write below commands in the VM's terminal to install Java which is pre-requite to install Jenkins.

     apt install openjdk-17-jre
     java -version
    
  3. Write below commands in the VM's terminal to install Jenkins.

     curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
       /usr/share/keyrings/jenkins-keyring.asc > /dev/null
    
     echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
       https://pkg.jenkins.io/debian binary/ | sudo tee \
       /etc/apt/sources.list.d/jenkins.list > /dev/null
    
     apt-get update
    
     apt-get install jenkins
    
  4. Now check the jenkins is running or now.

     systemctl status jenkins
    
     usermod -aG jenkins $USER   # to give permission to access jenkins
    

  5. Now install docker which will be running with jenkins

     apt install docker.io 
     usermod -aG docker $USER
    
  6. We need to add docker in jenkins group since we need to permit docker to run.

     usermod -aG docker jenkins
    
  7. Now copy the VM's public url and paste it in a new tab followed by 8080 port to open jenkins.

    https://<your_VM's_Public_IP>:8080

  8. Noe copy the path and cat the path in VM terminal to see the password.

  9. Jenkins is ready to us now.

Setting up DockerHub credentials in jenkins

  1. Go to Manage Jenkins > Credentials > Under "Store scoped to Jenkins" - System > Global credentials > Add Credentials.

  2. Now here we need to store our DockerHub username and password, under Credential ID. So whenever we need dockerhub credentials, we'll include Credential ID with environment verialble.

Deploying the application using declarative pipeline

  1. We will create a new job since this is our first app

  2. We'll create a declarative pipeline

  3. Fill the asked github location where the code is located

  4. Write the pipeline script and save

     pipeline {
         agent any
    
         stages{
             stage("Code"){
                 steps{
                     echo "Clone the code"
                     git url: "https://github.com/prasadtara09/django-notes-app.git", branch:"main"
                 }
    
             }
    
             stage("build"){
                 steps{
                     echo "Build the Docker Image"
                     sh "docker build -t django-notes ."
                 }
    
             }
             stage("Push to DockerHub"){
                 steps{
                     echo "Push images to docker hub"
                     withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubPass",usernameVariable:"dockerhubUser")])
                     {
                         sh "docker tag django-notes ${env.dockerhubUser}/django-notes:latest"
                         sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}"
                         sh "docker push ${env.dockerhubUser}/django-notes:latest"
                     }
    
                 }
    
             }
    
             stage("Deploy"){
                 steps{
                     echo "Deploy"
                     sh "docker run -d -p 8000:8000 tara0674/django-notes:latest"
                 }
    
             }
         }
     }
    
  5. My docker hub don't have any django images

  6. Now click on build and it will create an image in my DockerHub and run the application that we can access.

  7. Now we have my django-note app image in my DockerHub and application is also running on Port 8000 .