create git hook to push code on server

Login to the server
 

ssh root@IP

 

sudo apt-get update sudo apt-get install apache2

 
Again, we should give ownership of the document root to the user we are operating as:


sudo chown -R `whoami`:`id -gn` /var/www/html

 
We need to remember to install git on this machine as well:
sudo apt-get install git
Now, we can create a directory within our user's home directory to hold the repository. We can then move into that directory and initialize a bare repository. A bare repository does not have a working directory and is better for servers that you will not be working with much directly:

mkdir ~/proj 

cd ~/proj 

git init --bare

 

Since this is a bare repository, there is no working directory and all of the files that are located in .git in a conventional setup are in the main directory itself.
We need to create another git hook. This time, we are interested in the post-receive hook, which is run on the server receiving a git push. Open this file in your editor:




nano hooks/post-receive

 
 
put below content in post-recieve file
 
 
#!/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/app --git-dir=/home/ec2-user/proj checkout -f
      # sudo  /etc/init.d/nginx restart cd /home/ec2-user/ywrstaging/ywroom-staging &&  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  
 
 
When you are finished, save and close the file.

Remember, we must make the script executable for the hook to work:

chmod +x hooks/post-receive 

 

  Now, we can set up access to this remote server on our client.


Configure the Remote Server on your Client Machine

Back on your client (development) machine, go back into the working directory of your project:

cd ~/proj


Inside, add the remote server as a remote called production. You will need to know the username that you used on your production server, as well as its IP address or domain name. You will also need to know the location of the bare repository you set up in relation to the user's home directory.
The command you type should look something like this:

git remote add production demo@server_domain_or_IP:proj

 
Let's push our current master branch to our production server:


git push production master



 

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails