Integration of Git, Github and Jenkins and creating the Build Pipeline

Integration of Git, Github and Jenkins and creating the Build Pipeline

TASK 2

1. Create container image that’s has Jenkins installed using dockerfile

2. When we launch this image, it should automatically starts Jenkins service in the container.

3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins

4. Job1 : Pull the Github repo automatically when some developers push repo to Github.

5. Job2 : By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed ).

6. Job3 : Test your app if it is working or not.

7. Job4 : if app is not working , then send email to developer with error messages.

8. Create One extra job job5 for monitor : If container where app is running. fails due to any reson then this job should automatically start the container again.

Tools required-

  1. Redhat vm (I am using redhat version8)
  2. github
  3. Jenkins
  4. docker


Now let's start the task

Creating the docker container image in which we have installed jenkins

Create one folder and in that folder create one docker file. Here i mention all the required dependencies for the image that has jenkins pre installed.


No alt text provided for this image
FROM centos:latest

RUN yum install wget -y
RUN yum install net-tools -y
RUN wget O/etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo

RUN rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
RUN yum install java -y
RUN yum install jenkins -y
RUN yum install git -y

RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
CMD java  -jar /usr/lib/jenkins/jenkins.war

run command for creating the docker image

docker build -t <name of image>:<version>  <filepath>   //run this command to build   the docker image.
No alt text provided for this image
No alt text provided for this image

To verify that your image is created or not use

docker images //This cmd shows  images that are created by us.
No alt text provided for this image

As you can see above image has been created successfully.

Now we are launching one container through this image . This container have jenkins pre-installed.

docker run -it — previleged -p 3320:8080 -v /:/host task2:v1

-v /:/host — Here we are mounting the / directory of base os to this docker container because in future we require docker inside this docker container to launch some container.

No alt text provided for this image

As you can see container has been launched and jenkins services has been started. Here we have to provide the password that is mentioned at the time of launching the container.

No alt text provided for this image
No alt text provided for this image

Creating job1

No alt text provided for this image

The role of this job is to download the code from the github repository where developer push the code. Here is my github repository that havinh some HTML code.

No alt text provided for this image

We have to provide the URL of the github repository to the this jenkins of job where developer have uploaded the code . so that jenkins will go download from there.

No alt text provided for this image

Here I am using Poll SCM trigger. From this trigger jenkins will go to github in every one minute and check if developer has uploaded any new code.

No alt text provided for this image
rm -rf /host/web-repo
mkdir /host/web-repo
cp -r . /host/web-repo

Jenkins will first create one folder in this container where jenkins is running and copy the code inside this folder.

job2

No alt text provided for this image

By looking at the code or program file that jenkins have downloaded from github, Jenkins should automatically start the respective language interpreter installed image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed ).

If the code is in HTML then jenkins will start the container that has HTML interpreter already installed.

At the time of launching the container we have to expose this container to the client so that it can connect to the website running on this container.

chroot /host /bin/bash <<"EOT




export len=$(ls /web-repo | grep index | grep html | wc -1)
if [$len -gt 0]
then
if docker ps | grep prod_deploy
then 
docker rm -f prod deploy docker
docker run -dit -p 8081: 80 -v /web-repo:/usr/local/apache2/htdocs --name prod_deploy httpd:lat
docker run -dit -p 8081: 80 -v /web-repo:/usr/local/apache2/htdocs --name prod deploy httpd:lat


fi


fi 


EOT

No alt text provided for this image

Job3

Test your app if it is working or not. If app is not working , then send email to developer with error messages.

No alt text provided for this image
No alt text provided for this image

Here in email notification we have to provide following details for sending email to the developer.

SMTP Server — smtp.gmail.com

Default user e-mail suffix- @gmail.com

Smtp port — 465

user name and password of the email from which you want to send the email to the developer

No alt text provided for this image

After creating all the jobs , now we are installing one plugin “Build Pipeline” . This plugin will give one dynamic pipeline for visualizing the task and for seeing the functioning of the jobs that are dependent on each other in efficient way.

Build pipeline view:

No alt text provided for this image













To view or add a comment, sign in

More articles by Sai Jayanth Rajamahendram

Others also viewed

Explore content categories