php - 同步大学小组项目从 GitHub 仓库到个人服务器的更改

标签 php github ssh githooks git-pull

我正在尝试为一个使用 PHP 构建的大学小组项目设置一个 git 开发工作流。

在过去的一周里,我一直在帮助团队的每个成员配置他们的机器,以便我们能够将代码协作到主存储库。每个开发人员现在都能够在他们的机器和托管在 GitHub 上的远程存储库之间成功同步代码,无论他们使用的是哪种操作系统:

enter image description here

设置工作流程的下一部分是让 apache 服务器从 GitHub 存储库中提取我们的更改。这样每个人(甚至是团队中的非编码人员)都可以看到实时时间的变化并测试我们的系统:

enter image description here

我发现了 GitHub 提供的一个很酷的功能,叫做 git hooks .这应该很容易让我们将这样的功能集成到我们的工作流程中。我继续在服务器上安装 git,使用 GitHub 设置 SSH key 并创建一个新的 Hook 。

然后我将这段 PHP 代码添加到 Hook 调用的文件中:

<?php 
    $output = shell_exec('git pull origin master 2>&1');
    echo "<pre>$output</pre>";
?>

现在,如果我使用 php git_pull_post_hook.php 从 SSH 终端 session 调用此代码,最新更改(来自团队中的任何开发人员)将成功同步。但是,无论何时 GitHub Hook 或任何其他人通过 HTTP 访问 php 文件,都不会发生任何事情。

经过几个小时的谷歌搜索,每个问题都导致了另一个问题。我已经按照其他线程的建议更改了文件所有者权限,但现在这是我从脚本输出(通过 HTTP)获得的错误:

Could not create directory '/var/www/.ssh'.
Host key verification failed.
fatal: The remote end hung up unexpectedly

我猜测 www-data 没有足够的权限访问我们服务器根文件夹中的 SSH key ,所以这就是导致所有这些错误的原因......但是 /var/www/.ssh 目录实际上存在于服务器上,所以我不明白这里出了什么问题。

遗憾的是,小组中没有其他人对此类事情一无所知,也没有任何好的设置指南。任何帮助/指点将不胜感激!

更新:

VonC 发布的指南似乎是适合我们需要做的事情的解决方案。我尝试删除 .ssh 文件夹并完全按照该指南中的说明执行每个步骤,但现在我们收到以下错误,指出存在权限错误:

没有目录,用HOME=/登录

root@xxx:~# mkdir /var/www/.ssh
root@xxx:~# chmod 0700 /var/www/.ssh
root@xxx:~# chown -R www-data:www-data /var/www/.ssh
root@xxx:~# su - www-data -c "ssh-keygen -t rsa"
No directory, logging in with HOME=/
Generating public/private rsa key pair.
Enter file in which to save the key (/var/www/.ssh/id_rsa): 
Could not stat /var/www/.ssh: Permission denied
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
open /var/www/.ssh/id_rsa failed: Permission denied.
Saving the key failed: /var/www/.ssh/id_rsa.
root@xxx:~# chmod 0600 /var/www/.ssh/id_rsa
chmod: cannot access `/var/www/.ssh/id_rsa': No such file or directory
root@xxx:~# chmod 0600 /var/www/.ssh/id_rsa.pub
chmod: cannot access `/var/www/.ssh/id_rsa.pub': No such file or directory

最佳答案

作为in this guide ,您可能错过了 /var/www/.ssh/known_hosts 文件。

touch /var/www/.ssh/known_hosts
chown www-data:www-data /var/www/.ssh/known_hosts
sudo -u www-data ssh github.com

这在 webjay/gh_hook.php 中被引用, 但您在 this gist 中有一个更简单的版本

<?php

// Use in the "Post-Receive URLs" section of your GitHub repo.

if ( $_POST['payload'] ) {
shell_exec( 'cd /srv/www/git-repo/ && git reset --hard HEAD && git pull' );
}

?>hi

关于php - 同步大学小组项目从 GitHub 仓库到个人服务器的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22117489/

相关文章:

php - 如何从php list函数访问变量

php - 通过 mysqli_fetch_object 无法从该查询中获取计数

git - 对多个 github 项目使用相同的部署 key

linux - 将 ssh key 添加到 github 的问题 - 权限被拒绝

php - 在 CodeIgniter 中将数组与 Calendar 类一起使用

php - 如何调试 PDO 数据库查询?

git - 设置带有大文件的远程 Git 的最佳方法

git:未更新

git - GIT : fork from Github to Bitbucket, 的复杂设置 仅推送到 Bitbucket

amazon-web-services - 为什么Terraform SSH无法进入我的EC2实例?