In this project, we are using different tools
Git-hub:
We are using git-hub for code, It is a platform and cloud-based service for software development and version control using Git, allowing developers to store and manage their code. GitHub is a very popular platform for developers to share and collaborate on projects, and it is also used for hosting open-source projects. The interface also offers developers access control, collaboration features, and various task-management tools.
Docker:
Docker is a build tool, Here we are using Docker to build our code.
Docker is also known as containerization. It is the concept of containerization that enables you to separate your application from your infrastructure. We can use the docker to push our application into a test environment and execute it automatically. When we use Docker, we create and use images, containers, networks, volumes, plugins, and other objects.
Docker-hub:
Docker-hub is used for an artifactory or repository.
EC2:
We are using ec2 to deploy our code from the docker-hub to the ec2 instance.
Jenkins:
We are using Jenkins for CI/CD or to automate our project.CI/CD [ continuous integration/continuous deploy or delivery] can be done by two method
Freestyle project
pipeline project: The pipeline project also has two ways
declarative pipeline:- In our project, we are using the declarative pipeline.
scripted pipeline
In Lab
We are using AWS for our project.
Login to the EC2-instance, we are using Ubuntu as our operating system for this project
Installing git in our system
apt-get install git -y
- check the version of git
git --version
sudo apt-get install docker.io
- check the version of the docker
docker --version
- add a user to the docker group
sudo uermod -aG docker $USER
- restart our system
sudo reboot
- clone code from git-hub
git clone https://github.com/thesamiksha/django-notes-app.git
- Inside directory
cd django-notes-app
- create a docker image from the dockerfile
docker build -t projectimg .
- Now, We want to push our docker image to docker-hub through Jenkins
The process to install Jenkins in our Ubuntu
sudo apt-get update
- It is mandatory to have Java in our system before installing Jenkins
sudo apt-get install openjdk-17-jre
- check the version of Java
java -version
- package to install Jenkins
curl -fsSLhttps://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
Finally, we are at our Jenkins
Create a Job with a pipeline
- put to your GitHub repository URL in the GitHub project
- if developers commit the code in GitHub and you want to automatically build your project in Jenkins then you can check on
Script:
In Jenkins, we can use Groovy scripts to perform various tasks and customize our build and deployment processes. Groovy is a versatile scripting language that is well-suited for automating tasks in Jenkins pipelines, creating custom scripts for build steps, and interacting with Jenkins' API.
Pipeline:
A pipeline in Jenkins represents a series of automated steps or stages that define the process of building, testing, and deploying software applications. Jenkins pipelines can be used for continuous integration (CI) and continuous delivery (CD), enabling the automation of software development workflows.
Agent any:
This means that the pipeline steps can run on any available agent. Jenkins will allocate a suitable agent dynamically based on availability.
- code for GitHub url to clone our code
pipeline{
agent any
stages{
stage ("clone code"){
steps{
echo "clonning the code"
git url:"https://github.com/thesamiksha/django-notes-app.git", branch: "main"
}
}
}
}
- code for building a docker image from the Dockerfile
pipeline{
agent any
stages{
stage ("clone code"){
steps{
echo "clonning the code"
git url:"https://github.com/thesamiksha/django-notes-app.git", branch: "main"
}
}
stage ("build"){
steps{
echo "building the docker image"
sh "docker build -t projectimg ."
}
}
}
}
- test whether our pipeline runs perfectly or not by clicking the build now command of our Job
- We got an error in building the pipeline to see the error go to the console output
- We got an error of permission denied while trying to connect to the docker daemon socket. This problem arises because Jenkins isn’t in the group of docker, For this
cd /var/lib/jenkins/workspace/notes-app-cicd
sudo usermod -aG docker jenkins
sudo reboot
- We solve an error and our pipeline is building perfectly.
For pushing the docker image into docker-hub, we have to login the docker
go to the manage Jenkins
credentials
on our system, create an environment variable and add credentials
- Login to the docker-hub through the pipeline
pipeline{
agent any
stages{
stage ("clone code"){
steps{
echo "clonning the code"
git url:"https://github.com/thesamiksha/django-notes-app.git", branch: "main"
}
}
stage ("build"){
steps{
echo "building the docker image"
sh "docker build -t projectimg ."
}
}
stage ("push"){
steps{
echo "pushing docker image to docker-hub"
withCredentials ([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubPass",usernameVariable:"dockerhubUser")]){
sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}"
}
}
}
}
}
- We can see our login has succeeded through the declarative pipeline
- code to push the docker image into the docker-hub
pipeline{
agent any
stages{
stage ("clone code"){
steps{
echo "clonning the code"
git url:"https://github.com/thesamiksha/django-notes-app.git", branch: "main"
}
}
stage ("build"){
steps{
echo "building the docker image"
sh "docker build -t projectimg ."
}
}
stage ("push"){
steps{
echo "pushing docker image to docker-hub"
withCredentials ([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubPass",usernameVariable:"dockerhubUser")]){
sh "docker tag projectimg ${env.dockerhubUser}/projectimg:latest"
sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}"
sh " docker push ${env.dockerhubUser}/projectimg:latest"
}
}
}
}
}
- code to deploy our container
pipeline{
agent any
stages{
stage ("clone code"){
steps{
echo "clonning the code"
git url:"https://github.com/thesamiksha/django-notes-app.git", branch: "main"
}
}
stage ("build"){
steps{
echo "building the docker image"
sh "docker build -t projectimg ."
}
}
stage ("push"){
steps{
echo "pushing docker image to docker-hub"
withCredentials ([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubPass",usernameVariable:"dockerhubUser")]){
sh "docker tag projectimg ${env.dockerhubUser}/projectimg:latest"
sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}"
sh "docker push ${env.dockerhubUser}/projectimg:latest"
}
}
}
stage ("deploy"){
steps{
echo "deploying the docker-container"
sh "docker run -d -p 8000:8000 thesamiksha/projectimg:latest"
}
}
}
}
- Finally, we create a container from our docker-hub image
- We can see our deploying app on the server
- Everything is running okay until we build the same code on port 8000, if we try to rebuild the same code then it gives us the error
- To escape this error we use docker-compose
Docker-compose:
Docker Compose is a tool for defining and running multi-container Docker applications. It allows us to define a set of services, networks, and volumes in a single file (usually named docker-compose.yml
) to describe the components of our application and their configurations.
- install docker-compose on our OS
sudo apt-get install docker-compose
- In Jenkins pipeline
pipeline{
agent any
stages{
stage ("clone code"){
steps{
echo "clonning the code"
git url:"https://github.com/thesamiksha/django-notes-app.git", branch: "main"
}
}
stage ("build"){
steps{
echo "building the docker image"
sh "docker build -t projectimg ."
}
}
stage ("push"){
steps{
echo "pushing docker image to docker-hub"
withCredentials ([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubPass",usernameVariable:"dockerhubUser")]){
sh "docker tag projectimg ${env.dockerhubUser}/projectimg:latest"
sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}"
sh "docker push ${env.dockerhubUser}/projectimg:latest"
}
}
}
stage ("deploy"){
steps{
echo "deploying the docker-container"
sh "docker-compose down && docker-compose up -d"
}
}
}
}