Automated Website Deployment using DevOps

Aim: Developing an Automated Environment so that whenever the developer makes changes in my website it is first tested and if changes are found to be stable then deploy them else notify the developer and don’t deploy changes.
Technologies used: Github, Jenkins, Docker, RHEL-8.
Pre-requisites:
- RHEL-8 with Docker, Jenkins, and Python3.6 installed.
- Github account
- Jenkins with Git plugin, Github plugin, SCM plugin.
- Download and install my docker image “rajansh87/webserver:v1” from the command: docker push rajansh87/webserver:v1
- Download any docker image from the docker hub for PHP.
Note: The link for my docker image for the webserver is: https://hub.docker.com/repository/docker/rajansh87/webserver
Initial steps:
- Created a repository on Github.
- create a docker file for creating an image for Jenkins so that whenever you launch a container of that a Jenkins server automatically starts.
The complete Automated Environment will be done by 5Jobs in Jenkins.
Job #1: Developer’s Code Fetch
Whenever Developer push to the Github repository then Jenkins will fetch the code from it and store it in the local directory.
This job would be triggered automatically whenever there is a push on the Github Dev branch.
Job #2: Launch Container and Deploy
By looking at the code or program file, this will automatically start the respective language interpreter which is installed in the image container for deploying the code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed ).
This job would be triggered automatically whenever the Job1 is completed successfully.
Job #3: Test
This job will test whether the code was correct and is deployed successfully or not.
Job #4: Notify Developer for Error
This job will notify the developer by sending mail if there was some error in code for deployment.
Job #5: Maintain Containers
This job will keep an eye over the launched containers for respective language code and if someone stopped due to some reasons it will re-launch another new container for it.
Screenshots:
- Create a Github Repository.

2. Create a list view Dev_Task2 for this task inside which all my jobs would be running, inside this a job called “Job1_download” as a freestyle project in Jenkins.

3. Give Source for the code from git of dev branch. So it will clone the repository to Jenkins workspace.

4. Build triggers are used to start this job, and here I used Poll SCM and gave that at every minute there will be a trigger for this job.

5. This job will execute this code that consists:
It will copy everything from workspace to a directory called “/task2” at location “/root/”. Also, it will copy everything to al folder called “/task2_ws/”

6. Create Job2_launchContainer as a freestyle project in Jenkins.

7. Build triggers are used to start this job, and here this job will be triggered only after stable built of Job1_download.

8. This job will execute this code that consists:
Goto workspace and check type of code i.e its extension. Then check if its container already running.
if no such container running so run a new container from the respective images and also mount the volume as the directory where our cloned data is present to its “/var/www/html/”.
I also did PATing so that to expose my container’s IP to the outside world on port 8888.

9. Create Job3_Test as a freestyle project in Jenkins.

10. Add Build triggers as “build after other projects are built” as Job2_launchContainer. that is this job will automatically be triggered and start running after a stable built of job2.

11. Here I also used another build trigger Poll SCM and gave that at every minute there will be a trigger for this job.

12. This will test if job 2 was running well else tell fail.

13. Create Job4_sendMail as a freestyle project in Jenkins.

14. It will be triggered after Job3 is completed.

15. It will send a mail to a developer using a mail.py file. This mail.py file code is downloaded from geeksforgeeks.com for sending mail using python.

16. Create Job5_maintainContainer as a freestyle project in Jenkins.

17. It will be triggered after Job2 is completed.

18. This is the same code as of job 2 and will check if all containers working fine.

19. And All is done and your Automated Environment is ready this is the final view.

20. Deployed Code Output.

21. Received mail:

So, Now Everything is ready and whenever the developer will push some code on Github repository it will start the automated Deployment system with testing and notification to the developer.