Git 挂钩不运行

问题描述 投票:0回答:1

我想运行

post-receive
钩子来通知 Jenkins 开始新的构建,但我无法让 Git 钩子自动运行。

  1. 我通过删除
    .git/hooks
    尝试了
    .sample
    存储库中的示例挂钩,但它们都没有在它们应该执行的操作之后运行。
  2. 我使用
    .git/hooks
    命令设置了指向
    git config --local core.hooksPath .git/hooks
    的钩子路径,但没有帮助。
  3. 我确保钩子脚本是可执行的(
    chmod ug+x <script>
    )。

自动运行 Git hook 是否需要一些特殊设置?

git githooks
1个回答
0
投票

post-receive
钩子是服务器端钩子。当数据推送到存储库时会调用它。您可以使用两个本地存储库来模拟该过程,一个作为客户端,另一个作为服务器。

git init --bare server
# copy post-receive to server/hooks and make sure it's executable

git init client
cd client
touch a.txt
git add a.txt
git commit
git push ../server master

在实践中,服务器端存储库通常位于另一台机器上,我们使用 http/https 协议或 ssh 协议远程与其通信。该钩子应该安装在服务器端存储库中。

那么还有一个问题。在大多数情况下,服务器端存储库由 Github、Gitlab、Gitee 等托管服务或其他服务管理。他们可能支持标准 git hook,比如

post-receive
,也可能不支持。他们有自己的钩子,比如 Github webhooks,与 git hooks 不同。对于这些钩子,请参考文档以了解如何配置和使用它们。

© www.soinside.com 2019 - 2024. All rights reserved.