In this blog post, I will walk you through the process of setting up a CI/CD pipeline using Jenkins, hosted on an AWS EC2 instance, to automate the deployment of a Django application. The pipeline will handle the steps from cloning the latest code from Git, building the application, and finally deploying it on Docker Hub. Let's dive in!
Prerequisites
AWS Account: Ensure you have an AWS account.
EC2 Instance: Set up an EC2 instance with Jenkins installed.
GitHub Repository: Have a Django application repository on GitHub.
Docker Hub Account: Set up an account on Docker Hub.
Step 1: Setting Up Jenkins on AWS EC2
Launch an EC2 Instance:
Go to the EC2 dashboard and launch a new instance.
Choose the appropriate AMI (Amazon Machine Image) – preferably an Ubuntu Server.
Select the instance type (t2.micro should be sufficient for this tutorial).
Configure the instance, add storage, and configure the security group to allow SSH and HTTP access.
Review and launch the instance.
Make sure that you allow traffic for port 8000 and port 8080 for Jenkins and the application to run in the inbound rules of the security groups
SSH into your EC2 instance.
Update your system and install Jenkins using the following commands (Note: Jenkins is a Java based application hence we need to have JDK in order for Jenkins to run):
sudo apt update sudo apt install openjdk-21-jdk -y curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins
Whilst we are here we will also install docker as well as docker compose and give the required access to them:
sudo apt install docker.io sudo usermod -aG docker $USER sudo usermod -ag docker jenkins
Step 2: Access Jenkins
Open your browser and navigate to `http://<your EC2 public IP>:8080`
Retrieve the initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Complete the setup wizard and install the recommended plugins.
Step 3: Configure Jenkins for CI/CD
Create a New Pipeline Job:
From the Jenkins dashboard, click on
New Item
.Enter the job name and select
Pipeline
, then clickOK
.
Configure the Pipeline:
In the pipeline configuration, scroll down to the pipeline section.
Select
Pipeline script
and use the following declarative pipeline script:pipeline { agent any stages{ stage("Clone Code"){ steps { echo "Cloning the code" git url:"https://github.com/LondheShubham153/django-notes-app.git", branch: "main" } } stage("Build"){ steps { echo "Building the image" sh "docker build -t my-note-app ." } } stage("Push to Docker Hub"){ steps { echo "Pushing the image to docker hub" 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 ${env.dockerHubPass}" sh "docker push ${env.dockerHubUser}/my-note-app:latest" } } } stage("Deploy"){ steps { echo "Deploying the container" sh "docker-compose down && docker-compose up -d" } } } }
Step 4: Setting Up Docker Hub Credentials in Jenkins
Add Credentials:
Go to
Manage Jenkins
>Manage Credentials
.Select the appropriate domain (
global
if unsure) and clickAdd Credentials
.Select
Username with password
, enter your Docker Hub username and password, and add an ID (e.g.,dockerHub
).
Step 5: Test the Pipeline
Trigger a Build:
Go to your pipeline job and click on
Build Now
.Monitor the console output to ensure each stage completes successfully.
Verify Deployment:
Log in to your Docker Hub account and check if the new image has been pushed.
Conclusion
Congratulations! You've successfully set up a CI/CD pipeline with Jenkins on an AWS EC2 instance, automating the process from code commit to Docker Hub deployment. This setup ensures that your application is always up-to-date and can be easily deployed. It was quite an eventful project that I made that really tests your overall DevOps knowledge and troubleshooting capabilities.
I hope my journey inspires you to tackle your projects with confidence. Remember, every challenge is an opportunity to learn and grow. For more awesome projects about DevOps technologies and want to know more about how DevOps makes this easier, consider following me on LinkedIn. Want to know more about me!! follow me on Instagram!!