通过 php 通过 http 执行 git 命令 [英] Executing git commands via php over http

查看:55
本文介绍了通过 php 通过 http 执行 git 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

操作系统 - Ubuntu 14.04

OS - Ubuntu 14.04

我正在使用 GIT webhooks 进行部署.

I am working on deployment using GIT webhooks for it.

我已将部署密钥添加到 git repo,现在我想在本地存储库中发生推送时触发 git pull origin master 命令.

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.

这是我通过浏览器调用的 test.php 文件:

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');
?>

这是git.sh shell 文件:

This is the git.sh shell file:

#!/bin/bash
cd /var/www/proj-dir/test
git pull origin master

当我使用 php test.php 在终端上运行它时,我得到了预期的正确结果:

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 后跟 git pull 输出.

ubuntu for whoami followed by the git pull output.

现在这是当我通过浏览器调用相同的 http://example.com/test.php 时出现的问题,它显示用户或 whoami 输出为 www-data,是 apache 用户,但我尝试更新 php 文件的权限以执行并将用户更改为 www-data 但没有奏效.
检查了 apache 日志,当我通过浏览器执行时,出现权限错误

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.

我需要进行哪些更新才能使文件通过浏览器请求工作?

如果我需要更新 sudoers 文件,应该更新什么?

If I need to update the sudoers file what should be the updates?

更新
我将 .ssh 键添加到 var/www/ 目录,因为这是 apache 用户的家.但我仍然得到

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

我还为 www-data 用户添加了一行,以便能够执行 sh 文件.

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

参考这里 还是没有运气
将 .git 文件夹的权限更新为 www-data 用户

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

推荐答案

这似乎不是 PHP 问题,而是 git 配置问题.为用户 ubuntu 而不是用户 www-data 正确配置了 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.

您可以尝试让 PHP 像 ubuntu 一样运行某些东西,但这似乎不是最简单也不是正确的.我建议为 www-data 用户正确配置 git.

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

在确认您确实有复制场景后,您可以尝试为 www-data 用户解决问题.

After verifying you do indeed have a reproduction scenario you can try to fix the problem for the www-data user.

当以任一用户身份运行时,git config --list 的输出可能会有所不同.有关该分数的更多帮助,请参阅 https://git-scm.com/book/en/Getting-Started-First-Time-Git-Setup

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

也可能是文件权限/所有权差异导致了问题.为了排除这种情况,我建议您使用 git clone ... 作为用户 www-data 在文件系统的其他路径中创建新的克隆和工作副本.然后再次查看 git pull 现在是否按预期工作.

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.

你可以考虑给 www-data 自己的工作副本(不管权限问题).这也将防止不得不处理工作副本中可能导致合并问题的未提交更改.从 PHP/git.sh 自动/无人值守处理这些可能很麻烦.

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.

建议的工作流程是:在您当前的工作目录中开发为 ubuntuadd/commit/push您的更改.然后让 PHP 自己执行 pull(作为 www-data).

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屋!

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