Integrating Jenkins, Github, Docker- ADV

Anurag Mittal
7 min readJun 23, 2020

❗️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
  2. github
  3. Jenkins
  4. docker

Github url — https://github.com/anurag08-git/devops-task4

Let’s Start the process-

First we have to create one docker image in which jenkins have installed.

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.

run command for creating the docker image-

docker build -t “image-name:v1” .

After this docker image will start downloading all the requirements that we have listed in it. It may take some minutes for building this image depend on internet connectivity.

Here you can see image building is running successful.

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 3332:8080 -v /:/host anurag08-devops: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.

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.

We require some plugins in jenkins for this task. for downloading the code from github ,jenkins require these two plugins

After installing these plugins we are creating our first job in jenkins

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.

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.

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.

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

Now we are creating second job named as “job2-task2”

This job will run only when first job run successfully.

Main moto of this job is -

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.

Now we are creating our third job that will check that the site running on this container that has launched by jenkins is running fine or not .

Here i add one more feature to this job if the site is running fine then this job will sent mail to the developer .

I am not creating fourth job whose work is if site is not running fine , sent mail to developer.

I integrate the fourth job with third job so that single job will do the thing for us

This job will run only when job2 build successfully .

This the code for checking the site is running fine or not.

For sending mail from this job we require some plugins-

“Email Extension” and “Email Extension Template” plugin

After installing these plugins go to jenkins — -> congiguration.

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

Go to again job number — 3 . In job number 3 in Add post build action — ->Enable Email-Notification shell.

Here we have to write the mail of the developer . This mail will only sent when job3 failed. Job 3 will fail only when site is not running fine . It will notifies the developer that site is not running fine.

Now creating our last job . This job will run only when if the container that has been launched by jenkins for running the website is terminated or not running properly.

This job will run in every one min for checking that the container has been running or not

This job will again launch the container and deploy the code and expose this container to the client for connectivity to the website running on web server on this container.

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.

Giving name for the pipeline

We have to initialize our first job of the pipeline

Here is the pipeline of the task that we have created.

--

--

Anurag Mittal

Learning the new technologies like mlops, devops, hybrid multi cloud to enhance my skills.