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

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

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.