Create a hook for deploying code

1. Login to the server.

2. Create a directory name like "rails_hook".

3. Now go in this directory, (means cd rails_hook).

4. git init --bare

5. Open the file with this command -  sudo nano hooks/post-receive

6. Now copy below code in this 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=/var/app/current --git-dir=/var/app/proj_hook checkout -f
        cd /var/app/current && bundle install && bundle exec rake db:migrate
        sudo service passenger restart
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done

7. chmod -R 777 hooks/post-receive

7. Now come in the local host(on local console).

8. cd ~/proj

9. git remote add production username@server_domain_or_IP:rails_hook  

Comments