简体   繁体   English

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

[英]how can I make git/hooks/pre-receive work while we use HTTP as accessing protocol

Guru 古鲁

I just found that git hooks (pre-receive, post-receive) cannot be run while doing git-push via HTTP, however these hooks can be called while doing git-push via SSH. 我只是发现在通过HTTP进行git-push时无法运行git钩子(接收前,接收后),但是可以在通过SSH进行git-push时调用这些钩子。
Is this right? 这是正确的吗?
Then how can I make git/hooks/pre-receive work while we use HTTP as accessing protocol? 那么当我们使用HTTP作为访问协议时,如何使git / hooks / pre-receive工作呢?

/// ------------------------------------------------------------------------------------------------ /// ----------------------------------------------- -------------------------------------------------
/// @SERVER /// @服务器
/// This is the post-receive hook code ///这是接收后挂钩代码

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 /// @客户
/// Here git/hooks/post-receive works while git push via SSH. ///此处git / hooks / post-receive在git通过SSH推送时起作用。

$ 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

Following @phd guide, I improve my conf of apache2 and now it works. 按照@phd指南,我改进了apache2的conf,现在可以使用了。

Here is the completed apache2 conf for your reference. 这是完整的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###

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM