Search code examples
gitubuntuservervps

Server deployment using git hooks not working


While using windows 10 in local machine and ubuntu vps server, I am trying to make auto deployment using git hooks from local to server. I configured my ubuntu server as following :

root@server:~# cd /var
root@server:~# mkdir repo && cd repo
root@server:~# mkdir site.git && cd site.git
root@server:~# git init --bare
root@server:~# cd hooks
root@server:~# touch post-receive && nano post-receive 

and modified post-receive as following

#!/bin/sh
git --work-tree=/var/www/site--git-dir=/var/repo/site.git checkout -f

After that in the local pc repo I executated the following command

git remote add production ssh://[email protected]/var/repo/site.git

now in local repo I've 3 branches like master, branch1, branch2. When I push the repo to server by git push production master it pushed the master branch to server. But when I push git push production branch1, it doesn't pushed the branch1. In /var/www/site I found the master branch.

So I have two questions:

  1. How can I push other branches(not the master) to server.
  2. If I need to delete the production branch from local machine & add new server destination how to do so ?

Thanks in advance!


Solution

  • You can try something like:

    #!/bin/bash
    TARGET="/var/www/site"
    GIT_DIR="/var/repo/site.git"
    
    while read oldrev newrev ref
    do
            echo "Ref $ref received. Deploying ${newrev} to production..."
            git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $newrev
    done
    

    Without specifying a branch or commit on the checkout command will checkout the existing head again.