Deploy website using Git/Gitolite [post-receive hook doesn't work properly]

881 views Asked by At

I'm trying to deploy my website using git/gitolite.

I've created a remote repository, I cloned it to my local machine.

I've 2 branches Master and develop.

I've created a script of post-receive hook in order to deploy each branch in the correct web directory :

/var/www/<mysite>/ for branch Master [live version] /var/www/<mysite>/dev for branch develop [dev version]

for that I'm using this script in .gitolite/hooks/common/post-receive file :

#!/bin/bash
prodroot="/var/www/<mysite>"
devroot="/var/www/<mysite>/dev"

while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
        sudo git --work-tree=$prodroot  checkout -f
        sudo chown -R webuser:psacln $prodroot
else
        echo "Push on dev branch"
        sudo git --work-tree=$devroot  checkout -f 
        sudo chown -R webuser:psacln $devroot
fi
done

If I use it this way, it doesn't work properly, because checkout -f always get master branch, in other side if I use checkout -f $branch I'm getting an error saying :

fatal: '/home/git/repositories/<mysite>.git' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

And every thing fall down, I'm obliged to create repositories [Using gitolite].

What I want to understand is : if it's not a git directory, why it works for checkout -f [MASTER] and not for checkout -f $branch [here DEVELOP] ?

Am I missing something ? Please help me.

Thank you.

2

There are 2 answers

6
VonC On

In this type of hook, try t:

  • add unset GIT_DIR at the beginning of the script
  • always use the --git-dir=/path/to/your/repo.git in addition of the --work-tree= option.

That way, any git command will know exactly where (--work-tree) and for which git repo (--git-dir) it is supposed to be executed.

1
timmz On

Try this Coloq

if [ "master" == "$branch" ]; then
    cd $prodroot  
    sudo git pull //first time do git clone
    sudo chown -R webuser:psacln $prodroot  // what group is psacln ?