DevOps Automation

DevOps Automation

The project I made completely automates the Continuous Integration and Continuous Deployment of a web server using Git , Jenkins and Docker. The setup uses complete Automation and runs the website on a server as soon as the developer commits it on Git.

  • For this Project 1st I made a Git repo locally and added an extra branch named dev.
git checkout -b dev 

No alt text provided for this image
  • Then I pushed the complete repo containing two branches on Github
git push --set-upstream origin master
git push --set-upstream origin dev

No alt text provided for this image
  • After that I wrote a post-commit bash script to push the code after every commit to Github and trigger build remotely. I used ngrok to tunnel the Jenkins Ip through a public IP.
No alt text provided for this image
  • If Developer push to dev branch then Jenkins will fetch from dev and deploy on dev-docker environment . If Developer push to master branch then Jenkins will fetch from master and deploy on master-docker environment.
#!/bin/bash


if git branch --show-current | grep dev
then
git push
curl --user "admin:root" http://8e99b8fe.ngrok.io/job/project/build?token=dev
else 
git push --set-upstream origin master
curl --user "admin:root" http://8e99b8fe.ngrok.io/job/project/build?token=master
fi


  • In Jenkins I made different jobs for Testing environment i.e. dev and Public site i.e. master.
sudo cp -rvf * /root/ws3 
# copied files from jenkins to my workspace
No alt text provided for this image
  •  I used Job chaining to create and run the web server as soon as the initial job of copy is successful
# for testing environment
if sudo docker ps | grep dev_docker
then
echo "Already Running"
else
sudo docker run -dit -p 8082:80 -v /root/ws3:/usr/local/apache2/htdocs/ --name 
dev_docker httpd

fi

# for public server
if sudo docker ps | grep master_docker
then
echo "Already Running"
else
sudo docker run -dit -p 8081:80 -v /root/ws3:/usr/local/apache2/htdocs/ --name master_docker httpd
fi

No alt text provided for this image
No alt text provided for this image
  • After that I made a job so that Jenkins 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 then Jenkins will fetch from master and deploy on master_docker environment.
  • For this we have to first add the credentials of our github in a new job. Then , in the additional behavior panel we have to select "merge before build".
No alt text provided for this image
  • Next, I generated a taken to trigger the build remotely using curl command
No alt text provided for this image
  • Now, if the testing container is running, it is to be deleted and if it's not there, a message is to be printed. for this i wrote a bash script.
#!/bin/bash
if sudo docker ps | grep testenv
then
sudo docker rm -f testenv
else
echo "container removed"

fi
No alt text provided for this image
  • After this I used "post-build" option to build other project i.e, master_docker.
No alt text provided for this image
No alt text provided for this image
In this project,I automated a system such that if a developer does some upgrading in a seperate branch and commits it, it will run in the test environment where it gets checked by the quality management team and if work passes all quality norms, a token will be triggered by Quality management team, that will trigger another job which merges the developer branch to master branch and update the main branch to the main environment and destroy the testing environment.

Gaurav here you not created any job 3 as sir told. means in your scenario developer do all task is he push to dev branch dev environment run viceversa for master also There is no QAT role you implement means if QAT Team pass devloper code from dev branche then it goes to prod environment. (here need to use merge )

To view or add a comment, sign in

More articles by Gaurav Yadav

Others also viewed

Explore content categories