Integrating Jenkins | Git | Docker to create an Automated Environment

Integrating Jenkins | Git | Docker to create an Automated Environment

Jenkins is an open source automation tool written in Java with plugins built for Continuous Integration purpose.

PROBLEM STATEMENT:

We need to create an automated deplyoment environment using Jenkins such as:

  1. When our master pushes the code from Git, our Jenkins job 1 would pull the GitHub code and run a production environment using the Docker container and deploy the code over there.
  2. But when our developer would push the code, Our Jenkins job 2 would create a testing environment, again on the docker container, and deploy the code over there after pulling it from the GitHub .
  3. When the code is in the testing environment, the QA team would manually go and test the code. IF it is running fine, Jenkins will merge the developer branch and trigger our job2 , which would at the end push it to the production environment.

SOLUTION:

We start by creating a code from the master branch . we automate the part of pushing over the GitHub repository by creating a post-commit file. The following is depicted below:

No alt text provided for this image

For the first time we need to set the upstream but from the next time our post commit file would do all the required for us.

#!/bin/bash
if [ `git rev-parse --abbrev-ref HEAD` == "master" ]; then 
echo "push in master branch"
git push
curl --user "admin:khushi" https://192.168.99.101:8080/job/job%201/build?token=master
elif [ `git rev-parse --abbrev-ref HEAD` == "developer" ]; then
echo "push in dev branch"
git push
curl --user "admin:khushi" https://192.168.99.101:8080/job/job2/build?token=developer
fi

This is the file written to push the code automatically when the developer commits it. Also this code specifies to run two jobs at different times ie. when the developer pushes run the job 1 whereas when the developer pushes run the job2. Since first master pushes, it automatically starts running our first job.

No alt text provided for this image
No alt text provided for this image
sudo cp -rvf index.html /prodenv
if sudo docker ps |grep prodenv
then 
echo "already running"
else 
sudo docker rm -f prodenv
sudo docker run -dit --name prodenv -p 8085:80 -v /prodenv:/usr/local/apache2/htdocs httpd:latest
fi

This is the code written to create the docker environment as well as deploy the file pulled from github.

No alt text provided for this image


OUTPUT ::

Now, when the developer pushes the same file or code by making some changes , we want our job2 to push it in another container that is created by the Jenkins itself for the testing environment.

No alt text provided for this image
No alt text provided for this image

In the similar way, we specify the github repository and the remote build token.


sudo cp -rvf  index.html /testenv
if sudo docker ps| grep testenv
then
echo "already running "
else
sudo docker run -dit -p 8086:80 -v /testenv:/usr/local/apache2/htdocs --name testenv httpd
fi
No alt text provided for this image

OUTPUT ::

And finally it triggers our job3 , which dipicts that the code is being tested by the QA team and after it is tested , it is deployed to the GitHub repository from where our job 1 again picks up the code and merges it to the master branch.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

FINAL OUTPUT ::


Do comment you feedbacks about this article :) For any queries or correction feel free to contact.

要查看或添加评论,请登录

Khushi Thareja的更多文章

社区洞察

其他会员也浏览了