Saturday 1 April 2017

Create git_hook on AWS OR DIGITAL OCEAN

Deploy App using git_hook

First you have to login on aws server or digital ocean any one you can use

ssh -i ~/Desktop/filename.pem ubuntu(ec2-username)@92.192.192.192(your public ip)

First you have to install
1. sudo apt-get install git

And Make director
2. mkdir ~/app

Give permission to app

3. sudo chmod 777 -R app/

Go to inside app

4. cd app

Make directory

5. mkdir git_hook

Give permission

6. sudo chmod 777 -R git_hook/

Go to inside

7. cd git_hook

run this comman

8. git init –bare
open this file after that you have to copy and paste below given code and you can customize app name and ec-2 username. By default user is ubuntu or for digital ocean root.

9. sudo nano git_hook/hooks/post-receive

10.






















#!/bin/bash
while
read oldrev newrev ref
do
   
if
[[ $ref =~ .*/master$ ]];
   
then
       
echo
"Master ref received.  Deploying master branch to
production..."
       
git
–work-tree=/home/ec2-user(ubuntu)/app/yourpojectname
–git-dir=/home/ec2-user(ubuntu)/app/git_hook
checkout -f
     
#
sudo  /etc/init.d/nginx restart cd
/home/ec2-user(ubuntu)/app/yourprojectname
&&  source ~/.rvm/scripts/rvm && bundle install
&& rake db:migrate
      
sudo 
/etc/init.d/nginx restart


   
else
       
echo
"Ref $ref successfully received.  Doing nothing: only the
master branch may be deployed on this server."
   
fi
done  

11
. give permission to sudo 
sudo
sh -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >
/etc/sudoers.d/90-ubuntu'
Back to local machine
Go to you project directory and run this command
1. git remote add production ubuntu@192.192.192.192:app/git_hook
after this you have to add you public key permission to your machine.
2. cat ~/.ssh/id_rsa.pub | ssh -i ~/Desktop/file.pem ubuntu@192.192.192.192 'cat >> .ssh/authorized_keys && echo "Key copied"'
And finally you will run
git status
git add .
git commit -m “message”
git push production master

1 comment:

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...