Auto deployment PHP script using Gitolite

情到浓时终转凉″ 提交于 2019-12-03 21:15:23

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

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 :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!