Automation using Dockerfile,Jenkins & Github

Automation using Dockerfile,Jenkins & Github

What is Jenkins ?

jenkins is an open source automation server which enables developers around the world to reliably build, test, and deploy their software.

What is github ?

GitHub, Inc. is a United States-based global company that provides hosting for software development and version control using Git. In 2018, it became a subsidiary of Microsoft for US$7.5 billion. It offers the distributed version control and source code management functionality of Git, plus its own features

What is 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.

Task

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.

The dockerfile I used is given below:

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

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 .

No alt text provided for this image


No alt text provided for this image

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.

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

JOB 3 and JOB 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.

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

Webpage deployed in Production server.

No alt text provided for this image

We can observe this program flow as:

No alt text provided for this image

ALL JOBS COMPLETED DUE TO NO ERROR

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

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

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

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

Hence this task is completed with lot of hardwork and patience.

Thank you for Viewing my Article!!

要查看或添加评论,请登录

Mehak Pargal的更多文章

社区洞察

其他会员也浏览了