Integration of Jenkins with git hub and docker.

Skabhi
3 min readJun 20, 2020

--

Task :

  1. If Developer push to dev branch then Jenkins will fetch from dev and deploy on dev-docker environment and test it . If it runs successfully then it will auto trigger Job3.
  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.
  3. After successfully testing of “dev” code it will auto merge “dev” branch to “master” branch and then it will succesfully deploy to production system.

Now we are going to create 1 job:

  • First give your github url in source code management.
  • Build a remote trigger which will auto starts the job j1 as developer commit.
  • Go to Build shell and run the following commands :

sudo rm -rf /root/task1
sudo mkdir /root/task1
sudo cp * /root/task1
sudo rm -rf *

if sudo docker ps -a | grep web
then
sudo docker rm -f web
sudo docker run -dit -p 81:80 -v /root/task1/:/usr/local/apache2/htdocs/ — name web httpd:latest
else
sudo docker run -dit -p 81:80 -v /root/task1/:/usr/local/apache2/htdocs/ — name web httpd:latest
fi

  • Now , In your post build action configure as :

Now going to job2:

  • This job is similar to the Job1, but this job will be triggered when the code is pushed on the master branch.

sudo rm -rf /root/task1
sudo mkdir /root/task1
sudo cp * /root/task1
sudo rm -rf *

if sudo docker ps -a | grep prod
then
sudo docker rm -f prod
sudo docker run -dit -p 82:80 -v /root/Desktop/tasks/task1/:/usr/local/apache2/htdocs/ — — name prod httpd:latest
else
sudo docker run -dit -p 82:80 -v /root/task1/:/usr/local/apache2/htdocs/ — — name prod httpd:latest
fi

Now going to job3:

  • This job will be the last and the important one. JOB3 will trigger when JOB1 is successfully tested the “dev” code. This job will auto merge “dev” branch to “master” branch if it works fine.
  • Here we will set a trigger which will auto deploy the merge code to the production system.

Github url :

My output screens :

  • Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #devops #integration

--

--