-->

Auto deployment PHP script using Gitolite

2020-06-28 01:52发布

问题:

i would like to setup auto deployment script to my testing server locally.
i'm using gitolite on ubuntu desktop 11.04. i install gitolite using apt-get install gitolite, so it is create user gitolite and group gitolite.
Now i setup my testing webserver using apache which has user and group ivan:ivan,
and do git clone under that username.
so all the files and folder have the same file owner ivan:ivan.

i setup post-receive hooks under /var/lib/gitolite/repository/testrepo/hooks/ and have this script inside:

#!/bin/bash
#CONFIG
LIVE="/home/ivan/public_html/testrepo"

read oldrev newrev refname
if [ $refname = "refs/heads/master" ]; then
  echo "===== DEPLOYING TO TEST SITE ====="
  unset GIT_DIR
  cd $LIVE
  git pull origin master:
  echo "===== DONE ====="
fi  

Since post-receive hooks is executed by gitolite user, it has permission problem to access to the test server which is using user ivan.

i already add gitolite to ivan group and make it group access write, but still it cannot run.
i know this is a permission problem, but i don't know the solution.
If you have same experience or got any tips, i would really appreciate it.

Thanks
Ivan

回答1:

I finally got the solution on my own, thanks for VonC for the pointers.
My solution may not beauty but yet work.

So here are the steps that work in my solution:

  1. I create a bare repositories which will have the same user permission with the web application.

  2. In Gitolite Repositories folder (in Ubuntu: /var/lib/gitolite/repositories), i create a post-receive hook which will mirror the the repository the bare repo that i just created. Code is:

    #!/bin/bash
    echo '==== DO MIRROR ===='
    git push --mirror user@host:path/to/bare.git
    echo '==== DONE MIRROR ===='

  3. And in my web application, i will create a remote that connect to that bare repo.

  4. Then i create a post-receive hook in the mirrored bare repo, that will ask the web application to pull from it. The code:

    #!/bin/bash
    WORK_DIR="/home/ivan/public_html/test/testing"
    read oldrev newrev refname
    if [ $refname = "refs/heads/master" ]; then
    echo "===== DEPLOYING TO LIVE SITE ====="
    unset GIT_DIR
    cd $WORK_DIR
    git pull mirror-repo master
    echo "===== DONE ====="
    fi

  5. Don't forget to chmod +x for both post-receive.

As i said before, it may not beautiful but yet work, at least for my current situation. The reason why i'm not just pull it from the gitolite repositories is because of user file permissions. In Ubuntu (install from apt-get), gitolite hold user and group, gitolite:gitolite. and my web application is under my home folder (i'm using suPHP) which have user and group ivan:ivan. So when i pushed to the gitolite repositories, it will run the bash script under gitolite user, which cannot access the .git folder under my home folder.

So yes, for those who have better solutions, i'm eager to hear from you.
Thanks hope my solution may help others.
Ivan



回答2:

After a long google search, i frustrated to implement this auto pull feature when any push occurs. But my own way works with 3 simple steps.

  1. Add a status variable /var/lib/gitolite/stat/project

    vim /var/lib/gitolite/repositories/project.git/hooks/post-receive
    #!/bin/bash
    echo '==== DO MIRROR ===='
    echo '1'>/var/lib/gitolite/stat/project
    echo '==== DONE MIRROR ===='
    
  2. Prepare a script privilege by root

    vim /var/lib/gitolite/stat/autopull.sh
    #!/bin/bash
    hstat=`cat /var/lib/gitolite/stat/project
    enter code here
    if [ "$hstat" == 1 ]; then
    cd /home/suphpuser/public_html_staging/project
    git pull
    chown -R suphpuser.suphpuser /home/suphpuser/public_html_staging/project
    echo '0'>/var/lib/gitolite/stat/project
    fi
    
  3. Run a script

    /var/lib/gitolite/stat/autopull.sh
    

That's all :)



标签: git gitolite