对于一个新的 Web 开发项目,我配置了一个带有更新后 Hook 的共享存储库,该 Hook 会自动将更新 pull 入 Web 服务器虚拟根。
#!/bin/sh
cd /srv/www/siteA/ || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info
但是,Web 服务器运行 suphp,它强制每个文件都由用户“www-run”拥有。但是,由于更新后脚本在发起 git Push 的用户上下文中运行,因此更新的文件归用户所有,而不是 www-run 拥有。我认为最好的解决方案是使用 sudo 以 www-run 用户身份启动 git pull,因此我用以下几行更新了 sodoers(“webmaster”是所有 git 用户都是其成员的组):
Defaults:%webmaster !requiretty
%webmaster ALL=(www-run) NOPASSWD: /usr/bin/git
在更新后脚本中,我将 git pull 行更改为
sudo -u www-run /usr/bin/git pull hub master
但是我在执行 git Push 的客户端上收到以下错误
remote: sudo: no tty present and no askpass program specified
由于我已经指定在默认情况下不需要 tty 并且不需要密码,所以我无法弄清楚为什么这不起作用。
最佳答案
问题在于 sudoers 文件中用户规范的排序。从手册页
When multiple entries match for a user, they are applied in order. Where there are multiple matches, the last match is used (which is not necessarily the most specific match).
我将下面的行移到了最后并且它起作用了。
%webmaster ALL=(www-run) NOPASSWD: /usr/bin/git
关于Git 更新后 Hook 无法使用 sudo 执行 'git pull',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758815/