首页 > 解决方案 > Git post-receive hook实时输出流

问题描述

我正在使用 nginx 和 fastCGI (fcgiwrap) 托管一个 git-http-backend。当我推送具有接收后挂钩的应用程序时,似乎只有在挂钩终止后,输出才会发送到我的 git 客户端。

#!/bin/sh

echo "foo"
sleep 5
echo "bar"

它在终止后在某处缓冲输出并立即发送。所以实际看到“foo”需要 5 秒。我不太确定这种缓冲发生在哪里。

我认为我想要的是可能的,例如 Heroku 也能够流式传输他们的 post-receive 钩子的输出。

这是我的nginx.conf

user                    nginx;
worker_processes        4;
pid                     /run/nginx.pid;

events {
    worker_connections    1024;
}

http {
    sendfile              on;
    tcp_nopush            on;
    tcp_nodelay           on;
    keepalive_timeout     65;
    types_hash_max_size   2048;

    include               /etc/nginx/mime.types;
    default_type          application/octet-stream;

    access_log            /var/log/nginx/access.log;
    error_log             /var/log/nginx/error.log;

    gzip                  on;
    gzip_disable          "msie6";

    server {
        server_name       _;
        listen 80         default_server;
        listen [::]:80    default_server;

    location /ping {
        add_header Content-Type text/plain;
        return 200 'pong';
    }

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root            /var/lib/git;
    }

    location ~ ^.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        client_max_body_size            0;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
        fastcgi_param   GIT_HTTP_EXPORT_ALL "";
        fastcgi_param   GIT_PROJECT_ROOT /var/lib/git;
        fastcgi_param   PATH_INFO $uri;
        fastcgi_param   REMOTE_USER $remote_user;
        fastcgi_pass    unix:/var/run/fcgiwrap.socket;
        fastcgi_buffering off;
        fastcgi_keep_conn on;
    }

        location / {
            try_files       $uri $uri/ =404;
        }
    }


}


daemon                  off;

标签: gitnginxfastcgi

解决方案


原因既不是git也不是nginx,而是fastcgi。我发现这篇文章很有帮助:https ://serverfault.com/questions/852564/nginx-and-fcgiwrap-incremental-output-problems 。

然而,提供的补丁仅适用于 Debian。当我使用 Alpine 时,我最终基于https://github.com/petercolberg/fcgiwrap/tree/unbuffered编译了我自己的二进制文件

之后,我所要做的就是将以下行添加到我的 nginx 配置中:

fastcgi_param NO_BUFFERING 1;


推荐阅读