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:
Access your Jenkins master server and navigate to "Manage Jenkins" -> "Manage Nodes and Clouds."
Click on "New Node" to add the agent EC2 instance.
Name the node, select "Permanent Agent," and click "OK."
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:
Access Jenkins and click on "New Item."
Choose "Pipeline" and provide a name for your project.
In the pipeline configuration, select "Pipeline script from SCM."
Choose "Git" as the SCM, and enter your GitHub repository URL.
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"
}
}
}
}
Section 4: Configuring DockerHub Credentials
4.1 Jenkins Master:
Navigate to "Manage Jenkins" -> "Manage Credentials."
Add your DockerHub credentials.
Section 5: Creating Docker Compose File
Include a comprehensive Docker Compose file in your GitHub repository.
Configure services, networks, and volumes based on your Django app and any related services.
Section 6: Configuring GitHub Webhook
In your GitHub repository, navigate to "Settings" -> "Webhooks" -> "Add webhook."
Set the Payload URL to your Jenkins master server's webhook URL.
Configure the webhook to trigger on push events.
Section 7: Testing the Pipeline
Click on build now. It should deploy the app.
-
Make a small change in your Django app code.
-
Commit and push the changes to your GitHub repository.
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!