GitHub +Jenkins +Docker

Anuja Kumari
5 min readJul 14, 2021

🎗️ TASK DESCRIPTION 🎗️

JOB#1: If a Developer pushes their code 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.

JOB#3: Jenkins will check (test) for the website running in the dev-docker environment. If it is running fine then Jenkins will merge the dev branch to master branch and trigger JOB#2.

⚡Let’s Begin 😀

⏩ First, create a repository in GitHub.

⏩ Uploading file in master branch

master branch

⏩ change the branch, type: git checkout dev Now, all the codes from master branch are present in the dev branch.

Uploading file in dev branch

dev branch

Now, we have set up 2 branches in GitHub successfully 😀

⏩ For tunneling, i used ngrok software.

🔰 Let’s start Jenkins in RHEL 8 : systemctl start jenkins

Type RHEL8 IP and port no. 8080 in BROWSER , login with as username and password.

🔰 NOW , Let’s Create Jobs 😀

⏩ For JOB-1

  1. Select new item option from the Jenkins menu.
  2. Assign a name to the Job ( Here, JOB-1 )and select Freestyle project.
  3. Configure Job option, set the configurations….
  4. In Source Code Management section, we select Git and mention the URL of our GitHub Repository and select the branch as dev.
  5. In the Build Triggers section, we select GitHub hook trigger for GITScm polling
  6. In the Build Section, type the following script in Execute Shell:
if sudo docker ps | grep dev_os
then
echo "dev is already Running"
else
sudo docker run -d -i -t -p 8081:80 -v /devws:/usr/local/apache2/htdocs/ --name dev_os httpd
fi
sudo cp -rvf * /devws
JOB-1

7. click on the Save option.

Console output of JOB-1

we have created JOB-1 successfully 😀

⏩ NOW For JOB-2

  1. Select new item option from the Jenkins menu.
  2. Assign a name to the Job ( Here, JOB-1 )and select Freestyle project.
  3. Configure Job option, set the configurations….
  4. In Source Code Management section, we select Git and mention the URL of our GitHub Repository and select the branch as dev.
  5. In the Build Triggers section, we select GitHub hook trigger for GITScm polling

6. In the Build Section, type the following script in Execute Shell:

if sudo docker ps | grep master_os
then
echo "master is already Running"
else
sudo docker run -d -i -t -p 8082:80 -v /masterws:/usr/local/apache2/htdocs/ --name master_os httpd
fi
sudo cp -rvf * /masterws
JOB-2

7. click on the Save option.

Console Output of J0B-2

created JOB-2 successfully 😀

⏩ For JOB-3

  1. Select new item option from the Jenkins menu.
  2. Assign a name to the Job ( Here, JOB-3 )and select a Freestyle project.
  3. In Configure Job option, set the configurations…
  4. In the Source Code Management section, select Git and mention the URL of our GitHub Repository and select the branch to build as dev.
  5. In Additional Behavior , select Merge Before Build
  6. set the Name of the Repository as origin, Set the Branch to Merge to as master
  7. In Post Build Actions ,select Git Publisher , check Push only if the build succeeds and Merge Results options.
JOB-3

8. Click on save .

Console output of JOB-3

created JOB-3 successfully 😀

✴️ Let’s cross check the merge status

Merge Status

⚡Successfully done all the Jobs ✌️

status of docker containers

✴️ Now we check the pages

Code From dev branch
Code From master branch
Webpage After merging

🔰 Successfully Completed the task 😀✌️

GitHub link : https://github.com/Anujakumari/Jenkins_Task_1.git

Thanks for Reading !! 🙌🏻😁📃

🔰 Keep Learning !! Keep Sharing !!

LinkedIn profile : https://www.linkedin.com/in/anuja-kumari-4a62581aa/

GitHub : https://github.com/Anujakumari

--

--

Anuja Kumari

Learner @ Linuxworld Informative Pvt Ltd || DevOps(Docker,K8S, Jenkins, Terraform, Git and GitHub) || AWS || ( Python | Java )