Integration of Git/GitHub, Docker and Jenkins
Deepak Sharma
3 x RedHat Certified Engineer (EX200, EX294, EX180) || DevOps Engineer || Docker || K8s || Ansible || Linux || Git || Github || Gitlab || Terraform || Jenkins || Cloud Computing || AWS
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.
Workflow of task
Job -1: If Developer push to dev branch then Jenkins will fetch from dev and deploy on dev-docker environment.
Job -2: If Developer push to master branch then Jenkins will fetch from master and deploy on master-docker environment.
Both dev-docker and master-docker environment are on different docker containers.
Job -3: Manually the QA team will check (test) for the website running in dev-docker environment. If it is running fine then Jenkins will merge the dev branch to master branch and trigger #job 2.
Step -1 Create a git local repositiory
- Create a folder and initialize it
- Create a file
- Add file to staging area
- Add file to commit area
- Now add remote and connect to github repo
- Finally push file to github
For above process ,we use the below commands.
git init notepad index.html git add index.html git commit index.html -m "1st master commit" git remote add origin https://github.com/Ds123-wq/task.git git push -u origin master
Now we can create a hook which is present in the folder .git/hooks/. You can see many files in there. We create a file "post-commit".This file is used to trigger just after commit.So we use push command inside it.So when commit success,it automatically push the code to github by this trigger.
So when commit is done ,it push the code to github in master branch.
Create a new branch for other developer "dev1" .In this branch ,developer write code and add it to staging area ,then commit to commit area.As soon as developer commit,it uses post-commit file and upload code to github in dev1 branch.
git checkout -b dev1 notepad index.html git add index.html git commit index.html -m "1st dev1 commit"
Step-2 Create job1 of jenkins
This job is used run when Developer push to dev branch then Jenkins will fetch from dev and deploy on dev-docker environment.
In this job ,we provide the GitHub Repository Link and Also, specify the branch to be dev1 to fetch those files only.
In Build trigger ,use Poll SCM which runs after every minutes .After every minute,jenkins goto github and if new code come up ,then download the new code.
In execute shell, run the above code .This code first copy the new dev1 branch code to /root/testing directory and check testing container is running or not .If not,then it start again it. After job-1 done ,we can verify by base os ip and given port no.90.We paste 192.68.43.130:90 in chrome we see code of dev1 branch.
Here we see 1st commit master code and dev1 branch code .Because dev1 branch is created after 1st commit of master branch.When feature branch is created ,it will also access all previous code of master branch .
Step-3 Create job-2 of jenkins
This job is used to is check if Developer push code by master branch ,then Jenkins will fetch code of master branch from github and deploy on master-docker environment.
In this job ,we provide the GitHub Repository Link and Also, specify the branch to be master to fetch those files only.
In Build trigger ,use Poll SCM which runs after every minutes .After every minute,jenkins goto github and if new code come up ,then download the new code.And It aslo trigger by QA team job-3.
In execute shell, run the above code .This code first copy the master branch code to /root/production directory and check production container is running or not .If not,then it start again it. After job-2 done ,we can verify by base os ip and given port no.91.We paste 192.68.43.130:91 in chrome we see code of master branch.
Step-4 Create job-3 of jenkins
Now for the next job the objective is Manually the QA team will check (test) for the website running in dev-docker environment. If it is running fine then Jenkins will merge the dev branch to master branch and trigger #job 2.
We will assume that the QA team has approved the changes to be merged and therefore, this job will merge both the branches.
In this job ,we provide the GitHub Repository Link and Also, specify the branch to be master to fetch those files only.
In SCM ,we use additional behaviour "merge before build" to merge both branches
After successful merge of branches ,post-build Action is used to upload the code to github in master branch.
After job-3 done ,it triggers to job-2 and deploy the code in production environment .We can verify by base os ip and given port no.91.We paste 192.68.43.130:91 in chrome we see code of master branch and dev1 branch.
Thus, we have successfully automated the process of SCM using Integration of Git/GIitHub with Jenkins and Docker.
Thank you,