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

```plaintext
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" -&gt; "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.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700638538017/30e28dcc-9f31-4354-a504-d30b38d1a929.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")

### Section 6: Configuring GitHub Webhook

1. In your GitHub repository, navigate to "Settings" -&gt; "Webhooks" -&gt; "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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700638671657/c916bae6-5170-4717-851d-35ee1bab8807.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")
    
3. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700638609439/e9673364-d867-4d32-84a0-4bffa026d76b.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")
    
    Make a small change in your Django app code.
    
4. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700641993157/80561264-27c7-4f8f-a3e5-d930fdbc6235.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")
    
    Commit and push the changes to your GitHub repository.
    
5. Observe Jenkins triggering the pipeline on the master server, with deployment occurring on the agent server.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707888740297/8e528845-7159-421c-a9c5-fda31622f6d3.avif align="left")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700642104697/d8f265e4-0444-4deb-a7d1-1689a38af9cd.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")
