GitHub-Jenkins-Docker integration
This project is based on continuous development, continuous integration and continuous deployment. Using DevOps tools, a completely automated setup has been made in which the developer only needs to commit the source code and the changes will get displayed on website running on a web server.
A step-by step process for the project is described below:
- Create a git repository locally as well as a repository in the GitHub. By default both Git and GitHub will give you a master branch. So let's create another branch in the local repository and push this change in the GitHub repository. So your framework will look something like this:
And for making a new branch you can directly use a command:
- Now we can create a hook which is present in the folder .git/hooks/. You can see many files in there. We can also create a file and in this case let's create a file "post-commit" (the name of the file should be as per the requirement of activity you want to do after a particular event) and since it is using bash shell we can use shell scripting.
NOTE: We don't have to create separate hooks for branches.
- Create a html file from dev1 branch and stage this file and commit changes, it will automatically get pushed to the origin(remote repository).
- Create jobs in Jenkins, here I have created three jobs in Jenkins:
- JOB1: It checks the dev1 branch in the GitHub and start a docker image (OS) which is the testing environment if it is not there and if it is there then it will remove that image and create a new image (good practice in testing) and is not exposed to the clients. It will check the changes in dev1 branch and will trigger JOB3 once the testing is successful.
2. JOB2 : It checks the master branch in the GitHub and start a docker image (OS) which is the production environment if it is not there and if it is there then it will use the existing one and is exposed to the clients. It will check the changes in master branch.
3. JOB3: It will merge the two branches only if the dev1 branch is completely tested and as soon as this job is done, master gets changed and JOB2 is triggered to deploy changes into the production environment.
- A sample is shown below:
This is the website before doing any changes in any branches.
As soon as the changes goes to GitHub the jenkins will start, first JOB1 then JOB3 if the previous job is successful and finally JOB1.
If all the jobs is successfully executed then the website will have some changes such as: (in this case I have only increased the dots at the end)
- Also, when JOB1 runs then the previous container (if running) was destroyed and a new one starts.
This project can be further extended by integrating more tools and providing a better infrastructure to the setup.
I am attaching a video of the working model below:
Check this video out in the YouTube channel.