Day 80: Jenkins CI/CD Pipeline to Deploy a Web Application (Feb 14, 2024)

Day 80: Jenkins CI/CD Pipeline to Deploy a Web Application (Feb 14, 2024)

Efficient deployment is a cornerstone of successful software development. In this tutorial, we'll guide you through the process of setting up a Jenkins pipeline to automate the deployment of your Django web application using Docker. What makes this tutorial special is the utilization of two EC2 instances – a Jenkins master server and an agent server – to facilitate seamless deployment.

Section 1: Prerequisites and Project Setup

1.1 Prerequisites:

Before initiating the deployment process, confirm that the following prerequisites are met:

  • Two ec2 instances for master and agent servers.

  • Jenkins installed and configured on the master instance.

  • Jdk, Docker and docker-compose are installed on both instances.

  • A DockerHub account for storing Docker images.

Section 2: Configuring Jenkins Master and Agent

2.1 Setting Up Jenkins Master:

  1. Access your Jenkins master server and navigate to "Manage Jenkins" -> "Manage Nodes and Clouds."

  2. Click on "New Node" to add the agent EC2 instance.

  3. Name the node, select "Permanent Agent," and click "OK."

  4. Specify the necessary configurations, ensuring Docker is installed on the agent. Refer to this article for more details

2.2 Creating a New Pipeline in Jenkins:

  1. Access Jenkins and click on "New Item."

  2. Choose "Pipeline" and provide a name for your project.

  3. In the pipeline configuration, select "Pipeline script from SCM."

  4. Choose "Git" as the SCM, and enter your GitHub repository URL.

  5. Save the configuration.

Section 3: Building the Declarative CICD Pipeline

3.1 Modifying Jenkinsfile:

Update your Jenkinsfile with the following steps to include agent configuration and deployment:

pipeline {
    agent { label 'dev-server' }
    stages {
        stage('code clone') {
            steps{
                echo 'cloning'
                git url: "https://github.com/hassanb111/django-notes-app", branch: "main"
            }
        }
        stage('build') {
            steps{
                 echo 'building'
                 sh 'docker build --no-cache -t my-note-app .'
            }
        }
        stage('push to dockerhub') {
            steps{
                echo 'pushing'
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                    sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
                    sh "docker login -u ${env.dockerHubUser} -p ${dockerHubPass}"
                    sh "docker push ${env.dockerHubUser}/my-note-app:latest"
                }
            }
        }
        stage('deploy') {
           steps{
                echo 'deploying'
                sh "docker-compose down && docker-compose build --no-cache && docker-compose up -d --build"
           }
        }
    }
}

Section 4: Configuring DockerHub Credentials

4.1 Jenkins Master:

  1. Navigate to "Manage Jenkins" -> "Manage Credentials."

  2. Add your DockerHub credentials.

Section 5: Creating Docker Compose File

  1. Include a comprehensive Docker Compose file in your GitHub repository.

  2. Configure services, networks, and volumes based on your Django app and any related services.

Section 6: Configuring GitHub Webhook

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

  2. Set the Payload URL to your Jenkins master server's webhook URL.

  3. Configure the webhook to trigger on push events.

Section 7: Testing the Pipeline

  1. Click on build now. It should deploy the app.

  2. Make a small change in your Django app code.

  3. Commit and push the changes to your GitHub repository.

  4. Observe Jenkins triggering the pipeline on the master server, with deployment occurring on the agent server.