是否可以接受用户输入作为远程 git post-receive 挂钩的一部分?
我有一个接收后钩子,每当我们推送 master 时,它都会部署我们的 master 分支。
我想让部署成为可选的;该钩子要求简单的 Y/N 响应来实现此目的;下面的 bash 伪代码:
echo "Should I deploy this code now? Enter Y/N"
read deploy_code
case ${deploy_code} in
"Y") do_a_deploy ;;
"N") exit ;;
*) # ask the question again if not Y or N ;;
esac
由于 post-receive 钩子在 stdin 上获取其参数的方式, read
行不会暂停以等待用户的输入,并且脚本会不断循环以尝试获取是/否答案。
我认为从 /dev/tty
专门请求可以解决这个问题;
read deploy_code < /dev/tty
但这仍然会导致脚本无限循环,因为输入不是来自键盘。
在这种情况下真的可以得到键盘输入吗?
编辑: 啊。看起来确实是 ssh 惹的祸。现在查看输出,我添加了 < /dev/tty
我看到/dev/tty:没有这样的设备或地址
如果我在本地运行脚本,但通过 ssh,我可以模仿这个:
ssh 127.0.0.1 "echo 40913e300c8bf4ed7ea91b5ef61a522f3be2c05f e2aabfc865547d8b494b76c96947bab0c62acfec refs/heads/master | /path/to/post-receive"
编辑 2 :
所以我可以将 -t 选项设置为 ssh 以按照 在 ssh 会话中启用 tty 或者我可以在authorized_keys中启用它服务器上基于每个密钥的文件
编辑 3:
创建 ~/bin/ssh-t 之后
#!/bin/sh
ssh -tt "$@"
(双 -t 选项强制使用 tty)并设置 GIT_SSH 指向它,我现在得到了可怕的 致命:协议错误:错误的行长度字符: 后跟 005,我猜这是 .bash_profile 中的某些内容或在 git 有之前的类似回显有机会跑步
I've got a post-receive hook that deploys our master branch whenever we push master.
I'd like to make the deployment optional; the hook asks for simple Y/N response to achieve this; bash pseudo code below:
echo "Should I deploy this code now? Enter Y/N"
read deploy_code
case ${deploy_code} in
"Y") do_a_deploy ;;
"N") exit ;;
*) # ask the question again if not Y or N ;;
esac
Because of the way the post-receive hook gets its arguments on stdin the read
line doesn't pause for input from the user, and the script loops round and round trying to get a Y/N answer.
I thought that specifically requesting from /dev/tty
would fix this;
read deploy_code < /dev/tty
but that still causes the script to loop endlessly, as the input isn't coming from the keyboard.
Is it actually possible to get keyboard input in this circumstance?
Edit:
Ah. It looks like it's actually ssh to blame. Looking at the output now I've added the < /dev/tty
I'm seeing /dev/tty: No such device or address
And I can mimic this if I run the script locally, but over ssh:
ssh 127.0.0.1 "echo 40913e300c8bf4ed7ea91b5ef61a522f3be2c05f e2aabfc865547d8b494b76c96947bab0c62acfec refs/heads/master | /path/to/post-receive"
Edit 2:
So I can either set the -t option to ssh to request a tty as per enabling tty in a ssh session or I could enable it in the authorised_keys file on the server on a per-key basis
Edit 3:
After creating ~/bin/ssh-t
#!/bin/sh
ssh -tt "$@"
(the double -t option forces a tty) and setting GIT_SSH to point to it, I'm now getting the dreaded fatal: protocol error: bad line length character: followed by 005 which I guess is something in .bash_profile or similar echoing before git has a chance to run
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也可以解析提交评论。 Git 允许空提交。只是另一种选择。
You could parse the commit comment as well. Git allows empty commits. Just another option.