Merge Git Branches Using Jenkins Job

Govind Bhardwaj
3 min readJun 13, 2020

In this blog I will explain how to merge GitHub branches.

I will create three jobs for this 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

Solution For Job 1 -

I create a code in dev branch and push it on GitHub . Then I fetch this code in Jenkins with GitHub plugin (Source Code Management)

Fetching Code by jenkins

Then I create a execute shell and run these following commands -

#sudo docker run -dit — name devos vimal13/apache-webserver-php

#sudo docker cp /var/lib/jenkins/workspace/mltaskjob1/. devos:/var/www/html/

Run these commands to launch container

Console output-

Solution For Job 2 -

I develop program in master branch in pudh it on GitHub. These files are fetch by jenkins . This job will build after job 3

Then run these following commands -

#sudo docker run -dit — name masteros vimal13/apache-webserver-php

#sudo docker cp /var/lib/jenkins/workspace/mltaskjob2/. masteros:/var/www/html/

Solution For Job 3-

For job 3 I run following commands on execute shell in jenkins. It automatically fetch file type then launch respective container(In my case php file).

#if sudo docker ps | grep devos
then
echo “dev-docker is already running and now jenkins will merge master and dev branches”
cd /root/mltasks/task1/
sudo git checkout master
sudo git merge dev
fi

Console output -

then trigger job —

Check on browser

*For dev branch

  • For master branch

Whole task have completed now.

Thank you for giving your time to this article.

--

--