【问题标题】:how can I make git/hooks/pre-receive work while we use HTTP as accessing protocol当我们使用 HTTP 作为访问协议时,如何使 git/hooks/pre-receive 工作
【发布时间】:2018-03-07 09:30:27
【问题描述】:

大师

我刚刚发现通过 HTTP 执行 git-push 时无法运行 git 挂钩(接收前、接收后),但是通过 SSH 执行 git-push 时可以调用这些挂钩。
是这样吗?
那么,当我们使用 HTTP 作为访问协议时,如何使 git/hooks/pre-receive 工作?

/// -------------------------------------------- -------------------------------------------------- --
/// @服务器
/// 这是接收后的钩子代码

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

/// -------------------------------------------- -------------------------------------------------- --
/// @客户
/// 这里 git/hooks/post-receive 工作,而 git push 通过 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

【问题讨论】:

  • 你使用什么服务器来推送 HTTP?
  • 我们为我们的业务设置了一台位于互联网的服务器,由于代理服务器的安全限制,我们不得不使用 HTTP/HTTPS 协议而不是 SSH。所以可以给我任何建议吗?
  • 是的,但是服务器到底是什么?简单的HTTP服务器肯定不会运行服务器端的hook,需要配置git-http-backend
  • 提前致谢。我可以尽快检查并回复您。

标签: git http ssh githooks


【解决方案1】:

按照@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###

【讨论】:

    猜你喜欢
    • 2017-08-14
    • 2018-02-26
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多