Continuous Integration and Continuous Delivery (CICD) are crucial aspects of software development that help us build, test, and deploy software quickly and efficiently.
Project Overview
Our project is a Django-notes-app web application .To automate the deployment process of this application, we have set up a CI/CD pipeline using Jenkins. The pipeline consists of several stages, including code checkout, building Docker images, deploying services to servers using Docker Compose, updating the Docker registry, on the server.
By setting up this pipeline, we aim to streamline the deployment process of our django web application and ensure that each new change to the codebase is automatically deployed to the production/ testing/ development environment. This approach will save us time and effort while ensuring the reliability and scalability of our application.
Pre-requisites
A GitHub account to store the source code.
One server/machine :
a) Jenkins declarative
Jenkins, Docker and Docker Compose are installed.
Docker registry to store the Docker-versioned images.
Knowledge of Groovy syntax to create the Jenkins pipelines.
Let's get started--->
Step 1: Get the source code of this Django web App
Fork this repo:
https://github.com/msnehabawane/django-notes-app.git
Step 2: Make the server ready for deployment purpose
Create ec2 instance, one as the master node for deployment purposes.
Instances can be t2-micro, 1 CPU, 1 GiB Memory, and ports 8080 , 80, 22 are open to allow incoming traffic
Step 3: Setup these server to use Jenkins
Jenkins Master Node must have Jenkins installed.
Jenkins node must have Java, docker, docker-compose installed and configured.
sudo apt install docker-compose -y
for docker-compose installationJust follow the commands given below and you get it installed on your machine.
Install Docker
$ sudo apt-get update
$ sudo apt-get install
docker.io
$ sudo usermod -aG docker $USER
$ sudo reboot
$ sudo docker run hello-world
$ docker ps
Install Jenkins
Jenkins requires Java to run, so first install Java -->
$ sudo apt-get update
$ sudo apt install openjdk-11-jre
$ java -version
Long-Term Support release of Jenkins---->
$ curl -fsSL
https://pkg.jenkins.io/debian-stable/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-stable
binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
$ sudo apt-get update
$ sudo apt-get install jenkins
$ jenkins --version
Access your Jenkins on http://Localhost[EC2 instance public ip]:8080
Step 4: Create a Jenkins Pipeline project
Step 5: Set the GitHub webhook to trigger the project pipeline
Go to the GitHub repository that you forked in Step 1 -->
If you find a tick mark then you are done!
Step 6: Create a Jenkinsfile for our pipeline to run
pipeline {
agent any
stages{
stage("code"){
steps{
echo "cloning the code"
git url:"https://github.com/msnehabawane/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:"myId",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"
}
}
}
The pipeline has several stages that are executed sequentially:
"code" stage: This stage checks out the code from the GitHub repository using Git.
"Build docker images" stage: This stage builds Docker images for the different components of the multi-container application, such as worker, result, and vote.
"Deploy services" stage: This stage deploys the Docker containers for the application using docker-compose.
"Update docker images" stage: This stage updates the Docker images on DockerHub by pushing the newly built images
The pipeline is executed and sets an environment variable DOCKER_IMAGE_TAG using the build number. The pipeline uses several shell scripts to perform the Docker-related tasks . The pipeline also uses credentials to log in to DockerHub and push images.
Step 7: Run the Pipeline manually to check for any errors
Step 8: Check whether the docker image successfully upload to dockerHub or not
The build Succeeded and we can access the Application on development-server
Done!
Summary:
In this blog, we explored the significance of implementing Continuous Integration/Continuous Delivery (CI/CD) pipelines in software development. Demonstrated how to create a Declarative pipeline in Jenkins that can automatically build Docker images for updated app code and deploy them to the targeted deployment environment, notify management teams about new releases/updates, and perform clean-up for the host server.
Using CICD pipelines teams can streamline their software development processes, improve software quality, and achieve faster release cycles. By automating the repetitive and error-prone tasks of building, testing, and deployment, teams can focus on writing high-quality code and delivering value to their users.
I hope you learned something today with me!
Stay tuned for my next blog . I will keep sharing my learnings and knowledge here with you.
Let's learn together! I appreciate any comments or suggestions you may have to improve my blog content.
Thank you,
Neha Bawane