当我们使用HTTP作为访问协议时,如何使git / hooks / pre-receive工作

时间:2017-09-26 08:43:40

标签: git http ssh githooks

我刚发现git hooks(pre-receive,post-receive)在通过HTTP进行git-push时无法运行,但是这些钩子可以在通过SSH进行git-push时调用。
这是对的吗? 那么当我们使用HTTP作为访问协议时,如何进行git / hooks / pre-receive工作?

/// -------------------------------------------- -------------------------------------------------- -
/// @SERVER
///这是收件后挂钩代码

hello.git $ cat hooks/post-receive
#!/bin/bash
while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        #git --work-tree=/var/www/html --git-dir=/home/demo/proj checkout -f
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done

/// -------------------------------------------- -------------------------------------------------- -
/// @ CLIENT
///这里git / hooks / post-receive在通过SSH推送git时工作。

$ git push
user01@hostxxx.net's password:
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 390 bytes | 390.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Master ref received.  Deploying master branch to production...
To hostxxx.net:/var/www/html/repo/hello.git
   a308dbc..82184b8  master -> master

1 个答案:

答案 0 :(得分:0)

按照@phd指南,我改进了apache2的配置,现在它可以正常工作。

这是完整的apache2 conf供您参考。

# @file /etc/apache2/conf-enabled/git_http_backend.conf
#
# @brief
#  This conf to enable git accessing via HTTP over apaches.
#
#  Tested on Ubuntu-14.04.5
#
# a2enmod dav dav_fs env alias proxy rewrite proxy_http
#

SetEnv GIT_PROJECT_ROOT         /var/www/html
SetEnv GIT_HTTP_EXPORT_ALL      1
SetEnv REMOTE_USER              $REDIRECT_REMOTE_USER

ScriptAliasMatch \
    "(?x)^/(.*/(HEAD | \
        info/refs | \
        objects/(info/[^/]+ | \
            [0-9a-f]{2}/[0-9a-f]{38} | \
            pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
            git-(upload|receive)-pack))$" \
    "/usr/lib/git-core/git-http-backend/$1"

<Directory "/usr/lib/git-core">
    Options +ExecCgi -MultiViews +SymLinksIfOwnerMatch
    AllowOverride none
    Order allow,deny
    Allow from all
    Require all granted
</Directory>


# disable anonymous accessing /repo/*
<LocationMatch "^/repo/.*">
    AuthType Basic
    AuthName "Git"
    AuthUserFile /etc/apache2/users.htpasswd
    Require valid-user
    Order allow,deny
</LocationMatch>


## foobar.git
<Location /repo/foobar.git>
    Options +ExecCGI
    AuthType Basic
    DAV on
    AuthName "Git"
    AuthUserFile /etc/apache2/users.htpasswd
    Require valid-user

    Order allow,deny
    Allow from all
</Location>

###END###