Deploying site with Git: post-update hook works fi

2019-03-06 02:15发布

问题:

Having a strange issue with a git post-update hook. I created repository on my server (/var/www/vhosts/git/master.git) and in this repository added a post-update hook with the following code:

#!/bin/sh

echo $1
echo "*UPDATE*"

case " $1 " in
*'refs/heads/master'*)
    GIT_WORK_TREE=/var/www/vhosts/website.com/sandbox.website.com git checkout -f
    echo
    echo "Master was updated!"
    echo
    ;;
esac

case " $1 " in
*'refs/heads/sandbox'*)
GIT_WORK_TREE=/var/www/vhosts/website.com/sandbox.website.com git checkout -f
    echo
    echo "Sandbox was updated!"
    echo
    ;;
esac

I made sure this file is executable. Then I created a local repository on my machine using:

$ mkdir website && cd website
$ git init
$ echo 'Testing.' > index.html
$ git add index.html
$ git commit -q -m "Initial commit"
$ git remote add web ssh://username@website.com/var/www/vhosts/website.com/git/master.git
$ git push web +master:refs/heads/master

For whatever reason that first push works just fine—/var/www/vhosts/website.com/sandbox.website.com gets updated with the index file—but then none of the following pushes work. I get the response back from the post-update hook that says "Master was updated!" but the directory doesn't actually get updated.

Suggestions?

回答1:

You seem to have have a permission issue. Take a look on the permissions on the/var/www/* path and ensure that your git user has permission to read/write/delete on the files.

I have used a much simpler technique (here is a detailed script) that allowed me to use my server as git server with gitolite and deploy on push without having to clone the repository on the /var/www/myproject path. It is working for me on git 1.7.9 and Ubuntu 12.04.1 LTS with Apache/2.2.22.

  1. Create your rpository on the server:

    mkdir myproject.git && cd myproject.git
    git init --bare
    
  2. Configure the repository:

    git config core.worktree /var/www/myproject
    git config core.bare false
    git config receive.denycurrentbranch ignore
    
  3. Edit or create the hooks/post-receive and make it runnable

    touch hooks/post-receive
    chmod u+x hooks/post-receive
    

Here is the file content:

#!/bin/sh
git checkout -f

You are ready to do a push and have it working.

My git user is called git and my apache used the ubuntu user. I had to edit the apache configuration on /etc/apache2/conf.d/security (you can do it on /etc/apache2/http.conf also) to include:

User git
Group git

Now my /var/www/myproject deploys all the files on push, creating the files with git:git user/group and my apache uses the same user/group to run.

Now you just have to restart your server or do a service apache2 restart