DEV Community

Arjun Menon
Arjun Menon

Posted on • Originally published at arjunmenon.hashnode.dev on

Declarative CICD Pipeline with GitHub Integration, Jenkins, Docker, and Agent-Based Automation

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/ArjunMnn/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"
           }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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. Observe Jenkins triggering the pipeline on the master server, with deployment occurring on the agent server.

Conclusion

You've successfully established a robust CICD pipeline for your Django web application, incorporating GitHub integration, Jenkins, Docker, and an agent-based approach. This comprehensive guide allows for easy customization to suit your specific development needs. Explore additional optimizations and enhancements to further enhance your deployment workflow. Happy coding!

Let's connect on LinkedIn.

Checkout my GitHub profile.

Top comments (0)