0

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.
Is this right?
Then how can I make git/hooks/pre-receive work while we use HTTP as accessing protocol?

/// ------------------------------------------------------------------------------------------------
/// @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 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
zhengfish
  • 113
  • 12
  • What server do you use to push over HTTP? – phd Sep 27 '17 at 07:57
  • We setup one server for our business which is located in the internet, and because of the security limitation in the proxy server, we have to use HTTP/HTTPS protocol instead of SSH. So could please give me any advice? – zhengfish Sep 29 '17 at 08:29
  • Yes, but what exactly is the server? Simple HTTP server certainly will not run server-side hooks, you need to configure [git-http-backend](https://www.kernel.org/pub/software/scm/git/docs/git-http-backend.html). – phd Sep 29 '17 at 11:34
  • Possible duplicate of [How to set up git over http?](https://stackoverflow.com/questions/26734933/how-to-set-up-git-over-http) – phd Sep 29 '17 at 11:35
  • Thanks in advance. I can check it ASAP and get back to you. – zhengfish Sep 30 '17 at 01:12
  • I made it with your help. Thanks a lot. @phd – zhengfish Oct 11 '17 at 08:59

1 Answers1

0

Following @phd guide, I improve my conf of apache2 and now it works.

Here is the completed apache2 conf for your reference.

# @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###
zhengfish
  • 113
  • 12