通过PHP通过http执行git命令 [英] Executing git commands via php over http
问题描述
操作系统 - Ubuntu 14.04
我正在使用GIT webhooks进行部署。
将部署密钥添加到git repo,现在我想在从本地回购站发生推送时触发 git pull origin master
命令。
这是我通过浏览器调用的 test.php
文件:
<?php
// echo这是一个使用github webhooks测试部署功能的测试回放测试文件!!!!;
echo exec('whoami');
echo exec('sh -x /var/www/proj/test/git.sh');
?>
这是 git.sh
shell文件:
#!/ bin / bash
cd / var / www / proj-dir / test
git pull origin master
当我在终端上使用 php test.php 我得到了正确的结果:
ubuntu从github.com:repo/test
*分支主人 - > FETCH_HEAD
已经是最新的。
ubuntu for
whoami
然后是 git pull
输出。
现在遇到这个问题,相同的 http://example.com/test.php
通过浏览器显示用户或 whoami
输出为 www-data
,这是apache用户,但我尝试更新php文件的权限以执行并将用户更改为 www-data
但没有工作。
检查了apache日志,当我通过浏览器执行时,我得到一个许可错误
请确保您拥有正确的访问权限并存在存储库。 + cd / var / www / proj / deploy-test + git pull origin master
主机密钥验证失败。致命:无法从远程存储库读取。请确保您拥有正确的访问权限并存在存储库。
需要进行哪些更新才能使文件通过浏览器请求运行?
如果我需要更新sudoers文件,应该更新哪些内容?
Update
我将 .ssh
键添加到 var / www /
dir,因为这是apache用户的家园。但我仍然得到
git pull origin master
错误:无法打开.git / FETCH_HEAD:权限被拒绝
另外我为www-data用户添加了一行代码,以便能够执行sh文件。
www-data ALL =(安东尼)NOPASSWD:/ var / www / mysite / vendor / tmd / auto-git-pull / scripts / git-pull.sh
参考 here 仍然没有运气
将.git文件夹的权限更新为www-data用户
sudo chown www-data:www-data /var/www/proj/test/.git
这似乎不是一个PHP问题,而是一个git配置问题。对于用户 ubuntu
而不是用户 www-data
,Git配置正确。
你可以尝试让PHP运行一些Ubuntu的东西,但这似乎并不是最简单也不正确的。我建议为 www-data
用户正确配置git。
我怀疑你可以通过运行来重现问题在终端上:
#成为www-data用户
sudo su www-data
#你的git.sh文件
cd / var / www / proj-dir / test
git pull origin master
验证确实有复制情况后,您可以尝试修复 www-data
用户的问题。
当以任何用户身份运行时,
这也可能是文件权限/所有权的差异造成问题。为了排除,我建议你使用 git clone ...
作为用户 www-data
来创建一个新的克隆并在你的文件系统中的其他路径中工作。然后再看看 git pull
现在如预期的那样工作。
您可以考虑给 www-data
无论如何都是自己的工作副本(不管权限问题)。这也可以避免必须处理导致合并问题的工作副本中可能的未提交更改。从PHP / git.sh
自动处理/无人值守可能会很麻烦。
建议的工作流程将是:在当前工作目录下开发为 ubuntu
并且 commit
/ 推送
您的更改。然后让PHP自己完成 pull
(如 www-data
)。
OS - Ubuntu 14.04
I am working on deployment using GIT webhooks for it.
I have added the deployment keys to git repo and now I want to trigger the git pull origin master
command when a push happens from my local repo.
This is the test.php
file I call via browser:
<?php
//echo "THis is a test file on a test repo for testing the deploy functionality using github webhooks!!!!";
echo exec('whoami');
echo exec('sh -x /var/www/proj/test/git.sh');
?>
This is the git.sh
shell file:
#!/bin/bash
cd /var/www/proj-dir/test
git pull origin master
When I run this on terminal using php test.php
I get the correct result as expected:
ubuntu From github.com:repo/test
* branch master -> FETCH_HEAD
Already up-to-date.
ubuntu
for whoami
followed by the git pull
output.
Now here's the issue when I call the same http://example.com/test.php
via the browser it shows the user or whoami
output as www-data
, which is apache user, but I tried updating the permissions of the php file to execute and changing the user to www-data
but did not work.
checked the apache logs and when I execute through the browser I get a permission error
Please make sure you have the correct access rights and the repository exists. + cd /var/www/proj/deploy-test + git pull origin master
Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
What updates do I need to make so the file works via browser request?
If I need to update the sudoers file what should be the updates?
Update
I added the .ssh
keys to the var/www/
dir as that is the home for apache user. But I still get the
git pull origin master
error: cannot open .git/FETCH_HEAD: Permission denied
Also I added a line for the www-data user to be able to execute the sh file.
www-data ALL=(anthony) NOPASSWD: /var/www/mysite/vendor/tmd/auto-git-pull/scripts/git-pull.sh
Reference here Still no luck
Updated the permissions for .git folder to www-data user
sudo chown www-data:www-data /var/www/proj/test/.git
This does not seem to be a PHP issue but a git config issue. Git is configured correctly for user ubuntu
and not for user www-data
.
You can try to make PHP run something as ubuntu, but that does not seem the easiest nor the correct. I would suggest configuring git correctly for the www-data
user.
I suspect you can reproduce the issue by running on the terminal:
# become www-data user
sudo su www-data
# actions from your git.sh file
cd /var/www/proj-dir/test
git pull origin master
After verifying you do indeed have a reproduction scenario you can try to fix the problem for the www-data
user.
Chances are there is a difference between the output of git config --list
when run as either user. For more help on that score see https://git-scm.com/book/en/Getting-Started-First-Time-Git-Setup
It might als be that file permission / ownership differences are causing problems. To rule out, I suggest you use a git clone ...
as user www-data
to create a new clone and working copy in some other path in your filesystem. Then see again if the git pull
now works as expected.
You may consider giving www-data
its own working copy anyway (regardless of permission problems). This would also prevent having to deal with possible uncommitted changes in the working copy that result in merge problems. Handling these automatically / unattended from PHP / git.sh
might be cumbersome.
The proposed workflow would then be: develop as ubuntu
in your current working directory and add
/commit
/push
your changes. Then have PHP do the pull
as itself (as www-data
).
这篇关于通过PHP通过http执行git命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!