V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
tabris17
V2EX  ›  问与答

git 的钩子问题

  •  
  •   tabris17 · 2016-06-14 10:11:42 +08:00 · 2676 次点击
    这是一个创建于 2864 天前的主题,其中的信息可能已经有所发展或是发生改变。
    创建了一个 bare 仓库,开发把代码推送到该 bare 仓库,服务器从 bare 仓库 pull 代码更新。
    现在想实现代码自动部署到服务器。在 bare 仓库上使用 post-receive 钩子,到服务器上执行 pull 操作。
    钩子的代码能正常执行,但是服务器并不能 pull 到最新 push 的代码。是不是用错钩子了?
    8 条回复    2016-06-15 14:22:40 +08:00
    SpicyCat
        1
    SpicyCat  
       2016-06-14 12:50:59 +08:00   ❤️ 1
    先确认下 post-receive 有没有正常触发,然后确认触发后有没有报错。
    话说这种部署的任务应该用 CI 来做吧。
    wzxjohn
        2
    wzxjohn  
       2016-06-14 12:52:33 +08:00   ❤️ 1
    钩子没错,如果可以的话建议把钩子代码发出来看下,去掉敏感信息
    tabris17
        3
    tabris17  
    OP
       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
        4
    wzxjohn  
       2016-06-14 14:42:03 +08:00
    @tabris17 post-update 确实是在 post-receive 之后的,但是应该不会带来你说的这个问题才对。
    wzxjohn
        5
    wzxjohn  
       2016-06-14 14:49:23 +08:00   ❤️ 1
    @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
        6
    tabris17  
    OP
       2016-06-14 15:16:54 +08:00
    搞定了,之前确实是 git update-server-info 和部署代码脚本的执行顺序问题。

    然后把 exec git update-server-info 的 exec 去掉,后面的部署代码就能正确执行了
    julyclyde
        7
    julyclyde  
       2016-06-15 13:46:05 +08:00
    那看来你是根本不知道 exec “是什么”,就随便用
    tabris17
        8
    tabris17  
    OP
       2016-06-15 14:22:40 +08:00
    @julyclyde 是的 ^_^
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3692 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:15 · PVG 08:15 · LAX 17:15 · JFK 20:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.