Declarative Pipeline Project Using Jenkins

I am a DevOps Engineer with an aim to learn and contribute to the Tech World!!
📝 Blogging and Sharing: On this platform, I'll be sharing insightful articles, tutorials, and tips on all things DevOps. Expect deep dives into CI/CD best practices, IaC patterns, containerization strategies, cloud optimization, and much more. Let's learn and grow together!
Project Overview:
To clone the code from Github, build a docker image using the docker file and then deploy the docker container into the EC2 server of AWS
For automating the above flow so that code can be automatically get cloned and built and then deployed so for that we are going to build a JENKINS declarative pipeline
So basically we can understand that JENKINS is a tool that helps to automate the CI/CD process by creating the automation pipeline
StartFragmentDevops Tools Used for the Project:
Git and Github
Docker
Jenkins
AWS EC2
Project Steps:
Step1)git clone <url of GitHub code>
Step2)we need to build our GitHub code using docker so first of all we need to have docker installed in our system by using the below command and before that update your system
$sudo apt-get update
$sudo apt install docker.io
Step3)we will check some basic commands to see the list of pre-existing docker containers if any by executing the below command
$docker ps
But you may get an error as permission denied because your user is not available inside the docker group so we need to add our current logged-in user into the docker group in order to access the docker commands
So for that, we will use the below command
$sudo usermod -aG docker ubuntu
- And once you have added a user to the docker group so reboot system via below command
$sudo reboot
Step4) Now we have our docker file which will be used to build the docker Image
#This is the base Image
FROM python:3.9
#This is the working directory
WORKDIR /app/backend
#To copy our all requirements for this project to run like Django dependency and version etc.
COPY requirements.txt /app/backend
#To run the python package downloader to install all our dependency library
RUN pip install -r requirements.txt
#To copy all our current data into working directory
COPY . /app/backend
#This expose has the port number where we can run our application to view in browser
EXPOSE 8000
CMD python /app/backend/manage.py runserver 0.0.0.0:8000
- Now we know that once we have written our docker file we need to build it to create a docker image
Step 5) Installation of JAVA AND Jenkins
Now Before installing the Jenkins we need to install java in our system by using below commands
$sudo apt update
$sudo apt install openjdk-17-jre
Now to install Jenkins execute below commands
curl -fsSLhttps://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
Step 6) Now to check whether Jenkins is installed or not and what is the current status of Jenkins use below command
$systemctl status jenkins

So you can see that Jenkins is running usually jenkins run on port number 8080
So go to your public address of the ec2 instance and copy and paste it into the browser and at the end please add:8080 as the port number to the Jenkins login form

So for the first-time user, you might need to create a user
Now you will get a Jenkins login form page like below

Now we will create a new pipeline project will also give a name to it notes-app-CI_CD
As shown in the screenshot below

Then give a small description of your Pipeline Objective
Click the checkbox of Github because our code is there

Inside the build trigger also check the checkbox for GitHub Hook Trigger for scm Polling
This will make sure as soon as the developer commits the code the pipeline will get deployed

Now we will write the declarative pipeline script
Agent is the machine to which Jenkins assign the tasks to be done for now we are giving it as any
pipeline{
agent any
stages{
stage("Clone Code"){
steps{
git url:"https://github.com/LondheShubham153/django-notes-app.git", branch:"main"
echo "Clonning the code !!"
}
}
stage("Build"){
steps{
echo "Building the Image!!"
sh "docker build -t django-notes-app ."
}
}
stage("Deploy to Server"){
steps{
sh "docker run -d -p 8000:8000 django-notes-app:latest"
echo "Deploying the container!!"
}
}
}
}
Now after writing the script save the job and click on Build Now there you can see that each stage is getting executed that you have written in your pipeline script

Now if you click on the number 7 that is showing on the left side in above screenshot that is actually the build number and then you can see the console output also for your pipelines as below

Now we can see our app running in port Number 8000 inside a docker container
Also, we need to make sure that in our EC2 instance, we have added an inbound rule for the port 8000 enabled
Otherwise our application we won't able to be see in the browser

Thank you
Keep up the Happy Learning 😊😊
Regards,
Amrit Manash

