Automation using : — -​>>> Dockerfile+Jenkins+Github

Touhib Majeed
6 min readJul 28, 2020

--

Task Description : — — —

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 install 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 reason then this job should automatically start the container again.

Dockerfile

Dockerfile is a script, composed of various commands (instructions) and arguments listed successively to automatically perform actions on a base image in order to create (or form) a new one. They are used for organizing things and greatly help with deployments by simplifying the process start-to-finish.

Dockerfiles begin with defining an image FROM which the build process starts. Followed by various other methods, commands and arguments (or conditions), in return, provide a new image which is to be used for creating docker containers.

They can be used by providing a Dockerfile’s content — in various ways — to the docker daemon to build an image.

The dockerfile I used is as follows: →

#Command to build Dockerfile 

docker build -t IMAGENAME:TAG /new/


docker run -it - P --name CONATINERNAME IMAGENAME:TAG

Setting UP Jenkins

Here as we have done patting and it will expose 8080 port.

Now our jenkins is ready to use.

JOB 1:

For Job 1 , i have created a git hook (post-commit hook) as soon as the developer commits , it is pushed to the respective github repo . Then to pull that /clone that to jenkins workspace i created a github webhook which would trigger the Jenkins and it will copy to its workspace and then all the files are copied to a directory named ‘storage’ created inside Jenkins container and then the files with same extension is copied to particular folder in Base RHEL8 using ssh and scp commands using a python script ‘check_forext.py’ that i created .

JOB 2:

In 2nd job , i have written a bash script which will first check for the folders where the files are stored in base os and if the directory is empty that particular test_server will not be launched . If that folder has files then the corresponding test server will be launched with that storage mount to it Document root folder.

JOB 3 and 4:

For testing i have used http status code as i had only made it for web servers so by checking the status code we can test the site if its working or not and i have checked it for each file in that particular storage if there’s an error the mail will be automatically sent to the developer through mailx command on gmail with the corresponding error code and the filename in which error has occurred or has failed testing phase and if there’s no error the files will be deployed to production server.

Webpage deployed in Production server.

We can observe this program flow

ALL JOBS COMPLETED DUE TO NO ERROR

SUCCESS MESSAGE TO DEVELOPER AND

A WARNING : No pages for html so Html webserver is down .

Now if some error is there in the code or page not found then a mail will be sent to dev. with the corresponding error and filename.

As there was syntax error so 500 error code send to dev with respective filename and all other jobs are cancelled. But the production environment is still running with its older version. And second error regarding all codes failed is sent when no file passed test phase, in this case there was only one file so this error is also sent to dev.

JOB 5:

To monitor the production system i have written a bash script in which i have written the case that if test server is launched and if prod_server is not launched then it will be launched by jenkins itself. And if the test server fails it continues to show the older correct passed version of code so that client can still connect to the site.

Jenkin keep on triggered every second if the change occurs then it will update or by some network issue if prod server goes down it will launch it within a second automatically and also keep on informing the developer the current status or when the prod server goes down, it launches it automatically.

Now Somehow server goes down jenkins monitor it and lanch again the production environment.

Hence another task completed :)

Thank you for Viewing my Article.

thnx to vimal daga sir

and all my frnds who help me to complete this task

thnx to @Deepanshu Chajgotra

--

--

Touhib Majeed
0 Followers

Creative and people-oriented Computer Scientist with extensive project management experience.