从git钩子调用脚本 [英] Call a script from a git hook

查看:73
本文介绍了从git钩子调用脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在服务器/hooks文件夹中有两个脚本:

第一个启动日志记录并编写有关push的基本信息:(后接收)

 #!/bin/sh读取oldrev newrev refnameLOGFILE = post-receive.logecho"push-Old SHA:$ oldrev-> $ newrev>> $ LOGFILEsh ./post-receive-logic>>$ LOGFILE 

第二个实际部署:(后接收逻辑)

 #!/bin/shcd〜/项目pm2 stop〜/proj/main.jsgit --git-dir〜/proj/.git --work-tree〜/proj拉npm安装pm2重新启动〜/proj/main.js回声完成" 

当我推送一个提交时,第二个脚本永远不会被调用:工作树中没有任何变化,服务器没有被杀死和重启,第二个脚本没有特定的输出.

如果我手动调用 .//post-receive-logic ,一切正常,服务器停止,文件被拉出,服务器再次启动.

我尝试不使用 sh 来调用它,就像这样:

  ./post-receive-logic>>$ LOGFILE 

但没有运气.

我做错了什么?

解决方案

锻炼:在接收后挂钩操作期间,.(或 $ PWD )在哪里?

您运行 时,您的 $ PWD 都是这样.何时自动运行呢?(请查看接收系统的裸git存储库:您的 $ LOGFILE 输出将在该目录中.)

(发布时,钩子文本中缺少缺少的引号,因此大概是您手工复制了脚本的某些部分,也许还缺少其他内容.另外,请确保钩子具有执行权限.但是我的猜测是,您对git运行将 $ PWD 设置为 .git 目录而不是hook目录的钩子的事实感到bit恼.

(注意:您的钩子可能不完整,因为它只读取一个 oldrev newrev refname ,但是 git push 可以推送许多ref.通常,您应该循环: while读取oldrev newrev refname; do ...; done .如果您有一个pre-receive钩子拒绝推入一个以上ref的推,但是,这个特定的post-receive钩子可能是正确的.)

Let's say I have two scripts in server /hooks folder:

First one initiates logging and writes essential information about push: (post-receive)

#!/bin/sh
read oldrev newrev refname
LOGFILE=post-receive.log
echo " push - Old SHA: $oldrev -> $newrev >> $LOGFILE
sh ./post-receive-logic >> $LOGFILE

Second one does actual deploying: (post-receive-logic)

#!/bin/sh
cd ~/proj
pm2 stop ~/proj/main.js
git --git-dir ~/proj/.git --work-tree ~/proj pull
npm install
pm2 restart ~/proj/main.js
echo "finished"

When I push a commit, second script never gets called: no changes in working tree, no server being killed and restarted, no output specific to second script.

If I call ./post-receive-logic by hand, everything goes fine, server stops, files being pulled, server starts again.

I tried call it without sh, like this:

./post-receive-logic >> $LOGFILE

but no luck.

What am I doing wrong?

解决方案

Exercise: where is . (or $PWD) during the post-receive hook operation?

When you run it, it's whatever your $PWD is. What about when it's run automatically? (Have a look in the receiving system's bare git repository: your $LOGFILE output will be in that directory.)

(There's a missing close quote in the hook text in your posting, so presumably you hand-copied some part(s) of the script and perhaps there's something else missing. Also, be sure the hook has execute-permission. But my guess is that you're being bitten by the fact that git runs hooks with $PWD set to the .git directory, not the hook directory.)

(Side note: your hook is probably incomplete, as it only reads one oldrev newrev refname, but a git push can push many refs. Normally you should loop: while read oldrev newrev refname; do ...; done. If you have a pre-receive hook that rejects pushes that push more than one ref, though, this particular post-receive hook could be correct.)

这篇关于从git钩子调用脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆