git:在任何客户端推送后自动在服务器上运行 bash 脚本

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

我查看并找到了各种 git hook 指南,但似乎没有一个适合这个特定的、简单的场景。

我有自己的服务器,我将代码更改推送到(使用 git Push origin master)。完成此操作后,我必须 ssh 进入服务器并运行我设置的函数“update-code”,该函数克隆存储库、运行 ant 并重新启动 tomcat 等。我希望 git 运行此“update-”在任何用户运行“git Push”(从他们的机器,而不是服务器)后为我执行“code”命令。

回顾一下,任何用户(从他们的机器)推送到我的存储库后,都会在服务器上自动执行 shell 命令,就像我登录并输入“更新代码”一样。让 git 执行此操作的具体步骤是什么?

linux git centos hook
3个回答
2
投票

您可以在这里找到教程:使用 Git 自动部署

您需要在 git 存储库(服务器端)的

post-receive
目录中创建一个名为
hooks
的脚本。

不要忘记设置脚本的可执行权限

chmod +x post-receive


1
投票

您想使用

post-receive
钩子(有关详细信息,请参阅
man githooks
):

post-receive

   This hook is invoked by git-receive-pack on the remote repository,
   which happens when a git push is done on a local repository. It
   executes on the remote repository once after all the refs have been
   updated.

   This hook executes once for the receive operation. It takes no
   arguments, but gets the same information as the pre-receive hook does
   on its standard input.

请注意,如果在同一次推送中更新了多个引用,则仅调用一次。如果您需要被调用并对不同的参考采取行动(即为

master
做一件事,为
release/
做另一件事),那么您应该查看
post-update
钩子。

这些将存储在远程服务器上的

foo.git/hooks/
目录中。确保这些脚本可执行。


0
投票

我发现这无法正常工作,直到我将接收后文件的第一行设置为

#!/bin/sh

想告诉别人我花了很长时间才弄清楚这就是它不起作用的原因。

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