git 的钩子问题

2016-06-14 10:11:42 +08:00
 tabris17
创建了一个 bare 仓库,开发把代码推送到该 bare 仓库,服务器从 bare 仓库 pull 代码更新。
现在想实现代码自动部署到服务器。在 bare 仓库上使用 post-receive 钩子,到服务器上执行 pull 操作。
钩子的代码能正常执行,但是服务器并不能 pull 到最新 push 的代码。是不是用错钩子了?
2682 次点击
所在节点    问与答
8 条回复
SpicyCat
2016-06-14 12:50:59 +08:00
先确认下 post-receive 有没有正常触发,然后确认触发后有没有报错。
话说这种部署的任务应该用 CI 来做吧。
wzxjohn
2016-06-14 12:52:33 +08:00
钩子没错,如果可以的话建议把钩子代码发出来看下,去掉敏感信息
tabris17
2016-06-14 13:02:22 +08:00
@SpicyCat
@wzxjohn
触发了, git push 的时候能看到执行结果。

可能是因为在 post-update 钩子里有个
exec git update-server-info
没更新之前, pull 不到最新代码。

那说明 post-receive 是在 post-update 之前执行的咯?

另外我把命令写到 post-update 钩子里却反而不执行了。
wzxjohn
2016-06-14 14:42:03 +08:00
@tabris17 post-update 确实是在 post-receive 之后的,但是应该不会带来你说的这个问题才对。
wzxjohn
2016-06-14 14:49:23 +08:00
@tabris17 刚才没说完就回了。。。
因为 post-update 和 post-receive 都是所有 ref 更新完了才执行,所以先后顺序应该是不会影响的,只是时间问题而已。从描述和代码里也可以看出这两个是紧挨着执行的,没有太大的区别。

post-receive Hook
-----------------
After all refs were updated (or attempted to be updated), if any
ref update was successful, and if $GIT_DIR/hooks/post-receive
file exists and is executable, it will be invoked once with no
parameters.

post-update Hook
----------------
After all other processing, if at least one ref was updated, and
if $GIT_DIR/hooks/post-update file exists and is executable, then
post-update will be called with the list of refs that have been updated.

https://github.com/git/git/blob/master/builtin/receive-pack.c#L1772
tabris17
2016-06-14 15:16:54 +08:00
搞定了,之前确实是 git update-server-info 和部署代码脚本的执行顺序问题。

然后把 exec git update-server-info 的 exec 去掉,后面的部署代码就能正确执行了
julyclyde
2016-06-15 13:46:05 +08:00
那看来你是根本不知道 exec “是什么”,就随便用
tabris17
2016-06-15 14:22:40 +08:00
@julyclyde 是的 ^_^

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/285539

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX