Integrating Jenkins with GitHub and Docker

Krishna Pal
5 min readSep 1, 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⚡

Make a repository on GitHub first ⏩

Now Uploading file in Master branch ⏩

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

Now upload files in dev branch

dev branch

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

Now it’s time to start Jenkins

I am using RHEL8 OS to start Jenkins

Command: systemctl start jenkins

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

Next move, Job Creation👨‍💻

⏩ 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, select GitHub hook trigger for GITScm polling
  6. In the Build Section, type the following script in Execute Shell section:
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. Now save the job by clicking Save button.

Console Output of Job-1

Bravo!!!… Job-1 run successfully 😃

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, select GitHub hook trigger for GITScm polling

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

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. Now save the job by clicking Save button.

Console Output of Job-2

Bravo!!!… Job-2 run 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

Now Save the Job-3.

Console Output of Job-3

Hurray!!! Job-3 run successfully…. 😍

😎 Now let’s cross check the merge status

Merge Status
All three jobs run successfully
docker container status
dev branch code
Master branch code
webpage after merged code

So, Our Task has been done successfully… 😉✌

Thanks for reading… 📃

GitHub Link:- https://github.com/Krishna3-cloud/Jenkins-Task1.git

--

--

Krishna Pal
Krishna Pal

Written by Krishna Pal

Engineering Student || Aspiring DevOps Engineer || Tech Enthusiast

No responses yet