Task1: Deploy Website using GitHub, Docker, Jenkins

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.
Technologies used: Github, Jenkins, Docker, RHEL-8.
Pre-requisites:
- RHEL-8 with docker and Jenkins 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
Initial steps:
- Created a repository on Github.
- Created two branches in the repository as “master” and “Dev” branch.
- Master branch will only consist of data/code that is being already tested and found to be stable and appropriate. We (or Developer) would not push anything directly to the Master branch.
- Dev branch will consist of data/code that is created by the developer and pushed to the repository for deployment. If code found to be appropriate then only it would be deployed else no changes would be made.
- If the Code is Found appropriate we’ll be merging master and Dev branch.
The complete Automated Environment will be done by 3 Jobs in Jenkins.
Job #1: Developer’s Code Fetch and Testing
Whenever Developer push to Dev branch of the repository then Jenkins will fetch the code from Dev branch and deploy it on the Development environment or testing environment which is created using Docker.
If the Job executes successfully means the developer’s code is good for deployment.
This job would be triggered automatically whenever there is a push on the Github Dev branch.
Job #2: Fetch and Deployment
Whenever Developer push to master branch of the repository then Jenkins will fetch the code from master branch and deploy on Deployment environment created using Docker.
After Job successfully executed a Website would be hosted by the docker Deployment environment.
This job would be triggered automatically whenever there is a push on the Github master branch or when triggered by Job3.
Job #3: Merge and Deployment
When the code of Dev branch is found to be appropriate for deployment, then this Job will merge Dev branch to master branch and trigger Job2 to deploy the changed website.
Screenshots:
- Create a Github Repository with two branches, master and Dev.

2. Create a list view Dev_Task1 for this task inside which all my jobs would be running, inside this a job called “Job1_devEnv” 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 “/task1” at location “/root/”, then check if there is any container already running named “myweb1".
if no such container running so run a new container named “myweb1” from image “rajansh87/webserver:v1” and also mount the volume as the directory where our cloned data is present to its “/var/www/html/”.

6. Create Job2_deployEnv as a freestyle project in Jenkins.

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

8. 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.

9. This job will execute this code that consists:
It will copy everything from workspace to a directory called “/task1” at location “/root/”, then check if there is any container already running or stopped named “myweb2”.
if no such container running so run a new container named “myweb2” from image “rajansh87/webserver:v1” 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.

10. Create Job3_mergeEnv as a freestyle project in Jenkins.

11. Give Source for the code from the git of the dev branch. So it will clone the repository to Jenkins workspace. Also, provide credentials of your GitHub account since in this job we’ll be merging jobs that require authentication.

12. Add Additional Behaviours to “Merge before build” and provide information as that of given in the screenshot below.

13. Add Build triggers as “build after other projects are built” as Job1_devEnv. that is this job will automatically be triggered and start running after stable built of job1.

14. Add Post-Build actions that are what to do after this job is done. So after the stable build of this job, trigger job2. Also, add Another action of “Git Publisher” with the following things done as shown on the screenshot below.

15. And All is done and your Automated Environment is ready.

So, Now Everything is ready and whenever the developer will push some code on dev branch it will start the automated Deployment system with testing.