[go: up one dir, main page]

0% found this document useful (0 votes)
125 views41 pages

Jenkins Declarative Pipeline

This document discusses setting up continuous integration and continuous deployment (CI/CD) of a Django notes application using Jenkins, Docker, and Docker Compose. Key steps include: 1. Launching an Ubuntu EC2 instance and configuring Docker permissions. 2. Creating a Dockerfile and building a Docker image for the application. 3. Installing and configuring Jenkins on the EC2 instance. 4. Creating a Jenkinsfile to define the CI/CD pipeline with stages for code, build, push to Docker Hub, and deploy. 5. Configuring Docker credentials in Jenkins and pushing the image to Docker Hub. 6. Deploying the application using Docker Compose to

Uploaded by

Rakesh Mahta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views41 pages

Jenkins Declarative Pipeline

This document discusses setting up continuous integration and continuous deployment (CI/CD) of a Django notes application using Jenkins, Docker, and Docker Compose. Key steps include: 1. Launching an Ubuntu EC2 instance and configuring Docker permissions. 2. Creating a Dockerfile and building a Docker image for the application. 3. Installing and configuring Jenkins on the EC2 instance. 4. Creating a Jenkinsfile to define the CI/CD pipeline with stages for code, build, push to Docker Hub, and deploy. 5. Configuring Docker credentials in Jenkins and pushing the image to Docker Hub. 6. Deploying the application using Docker Compose to

Uploaded by

Rakesh Mahta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

CI/CD: Note-App Deployment using Declarative Pipeline

LINKEDIN: https://www.linkedin.com/in/ritikktiwari/

by: Ritik Kumar Tiwari


AWS EC2 Instance

• Go to AWS Console
• Select ubuntu Operating system
• Instances(running)
• Launch instances

GitHub URL: https://github.com/RitikPyCode/django-notes-app


Ubuntu has not docker permission:

We need to modify the user permission and append the ubuntu user in docker group:

Then reboot your system once and wait for few minutes.
Docker file:
FROM python:3.9

WORKDIR /app/backend

COPY requirements.txt /app/backend

RUN pip install -r requirements.txt

COPY . /app/backend

EXPOSE 8000

CMD python /app/backend/manage.py runserver 0.0.0.0:8000

We have successfully built.


Jenkins Installation Pre-requisites.
sudo apt update
sudo apt install openjdk-17-jre
java -version
sudo apt-get update

Installation 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

service Jenkins status

Now you need to go to your Brower and paste the public IP address of your server
eg: http://PublicI:8080/

Jenkins runs on 8080 port.

If port is not enabled, then you need to enable that port: 3000 on your aws security group:
Steps:
Paste it on your browser: http://PublicI:8080/

Copy and Cat this command with sudo permission:


/var/lib/jenkins/secrets/initialAdminPassword, you will get initial password.
Select suggested.
Create your username and Password:
Save and Finish

Start using Jenkins


Dashboard layout
This is pipeline structure, where all steps are there. Code, Build, push to Docker Hub, Deploy

This is Groovy syntax, and it’s the structure of Declarative Pipeline , Let me
show you the output of every stages.
pipeline {

agent any

stages{

stage("Code"){

steps{

echo "Cloning the code from github"

stage("Build"){

steps{

echo "Building the Images"

}
stage("Push to Docker Hub"){

steps{

echo "Pushing to the code on docker HUB"

stage("Deploy"){

steps{

echo "Deploying the container"

Click on Build Now,


Code, Build, Push to Docker HUB, Deploy

Finally, Demo has been completed.


Check now if your pipeline running fine. Because we have to check every stage. If we get any
error, we will do troubleshooting no problem.

SAVE and BULD NOW

Cloning the Code

pipeline {

agent any

stages{

stage("Code"){

steps{

echo "Cloning the code from github"

git url:"https://github.com/RitikPyCode/django-notes-app.git", branch:"main"

stage("Build"){
steps{

echo "Building the Images"

sh "docker build -t my-note-app ."

stage("Push to Docker Hub"){

steps{

echo "Pushing to the code on docker HUB"

stage("Deploy"){

steps{

echo "Deploying the container"

First Stage has been completed successfully.


/var/lib/jenkins/workspace/notes-aap-cicd

Go to this path /var/lib/jenkins/workspace/notes-aap-cicd . all files are here.

sudo usermod -aG docker jenkins


sudo reboot
Please refresh the Jenkins software then build now again, you will pass all the stages
successfully.
Now Pushing the code from DOCKER HUB.
First you have to sign In or Signup yourself on docker hub: https://hub.docker.com/
Draw-back related credentials, issue has been resolved below:
sh “docker login”, then creds but if we type all the creds then it will be publicly, then we have devops
concept: environment variable, it will not show you your creds.

First go to Jenkins Dashboard > Manage Jenkins > Security: Credentials > click on system
We need to assign ID for username and password, because if anyone ask then we can give our ID no.
which contains username and password.
Now back to dashboard and come in configuration

pipeline {

agent any

stages{

stage("Code"){

steps{

echo "Cloning the code from github"

git url:"https://github.com/RitikPyCode/django-notes-app.git", branch:"main"


}

stage("Build"){

steps{

echo "Building the Images"

sh "docker build -t my-note-app ."

stage("Push to Docker Hub"){

steps{

echo "Pushing to the code on docker HUB"

withCredentials([usernamePassword(credentialsId:"dockerhub123", passwordVariable:
"dockerhubPass", usernameVariable:"dockerhubUser")]){

sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}"

stage("Deploy"){

steps{

echo "Deploying the container"

}
Now we are going to push our image on docker hub using declarative pipeline.
pipeline {

agent any

stages{

stage("Code"){

steps{

echo "Cloning the code from github"

git url:"https://github.com/RitikPyCode/django-notes-app.git", branch:"main"

stage("Build"){

steps{

echo "Building the Images"

sh "docker build -t my-note-app ."

stage("Push to Docker Hub"){


steps{

echo "Pushing to the code on docker HUB"

withCredentials([usernamePassword(credentialsId:"dockerhub123", passwordVariable:
"dockerhubPass", usernameVariable:"dockerhubUser")]){

sh "docker tag my-note-app ${env.dockerhubUser}/my-note-app:latest" (we have to tag our old


image then add usename)

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"

Now refresh your docker hub website:


Stage Deploy:

sh "docker run -d -p 8000:8000 ritikt2023/my-note-app:latest"

Add 8000 port where your application will run.


Application has been deployed. You can check with your public Ip address with 8000 ports. Eg:
http://18.117.216.230:8000/
If you are going to build now again then pipeline will get failed. The reason is Port is already allocated
Docker Compose is a tool that was developed to help define and share multi-container applications.
With Compose, we can create a YAML file to define the services and with a single command, can spin
everything up or tear it all down.

Install the DOCKER-COMPOSE

sudo apt-get install docker-compose

It’s type of yml file where are the things are explained.

Container or image ko down kiya aur phir up kiya inhi code se.
If we are going to run that application with the help of docker-compose what will happen, it will throw
error,

for this error we need to kill running container.

Now run docker-compose up -d, this will decide automatically, image or container name.

Now there is no container after down

This is the solution of above error. Let me add this code in groovy syntax.
Lets change the little bit docker-compose file: build replace with image, now we are taking images
from docker hub.

We have committed few things, now you can run as much as possible, never getting error.

This is called robust pipeline.


A Robust Pipeline = Predictability + Power + Profit. The foundation of
building a pipeline is about blocking out the time and being committed
to focused prospecting.
Now let’s try our whole code will come from GitHub. Through adding file Jenkins files

Copy whole pipeline code, go to GitHub> add new file> paste same code as Jenkins file> commit it.

Then:
Application has been deployed successfully.
Webhook concept:
A webhook is a mechanism by which GitHub notifies Jenkins of changes to a repository.
Jenkins can then automatically build and test the code in response to the webhook
notification.

First edit your inbound rule port no. 8080 , My Ip to Anywhere.


Jenkins URL in Payload URL section:
Now webhook is configured. If you change any think in code and commit it…
that will be triggered automatically, it’s called continuous deployment. With out
clicking on build now.
Author: Ritik Kumar Tiwari

LinkedIn: https://www.linkedin.com/in/ritikktiwari/

You might also like