从 php 脚本运行 git pull

2023-10-12php开发问题
3

本文介绍了从 php 脚本运行 git pull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试 完美的工作流程,使用 Git、GitHub 和 SSH,除了从 php 运行命令 git pull 外,我已经设置好了一切.

I was trying the Perfect Workflow, with Git, GitHub, and SSH, and i have everything set up, except running the command git pull from php.

当我运行 exec('git pull') 我得到:

无法创建目录/.ssh".主机密钥验证失败.致命:远端意外挂断

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

如果我在终端(以 root 用户身份)运行它,它工作得很好,但我需要这个钩子从接收后 URL (Github) 工作.

If i run it in the terminal (as root) it works just fine, but i need this hook to work from the Post-Receive URL (Github).

如果我执行 exec('whoami') 我得到 apache.

If i do exec('whoami') i get apache.

它是来自带有 CentOS 的 mediatemple 的 (dv).

It's a (dv) from mediatemple with CentOS.

推荐答案

如果您希望 apache(用户)能够从 git 中拉取数据,您必须为apache,然后将其添加到 github 上的只读键中.

If you want apache (the user) to be able to pull from git, you'll have to create an ssh key for apache, then add that to the read only keys on github.

流程是这样的(根据您的需求进行调整)

The flow is something like this (tweak to your needs)

usermod -s /bin/bash apache
su apache
cd ~
ssh-keygen # work through the keygen dance (added a dash)

上传(这里的波浪号指的是 apache 的 homedir)~/.ssh/id_rsa.pub 到 github 并授予 apache 访问它需要从中提取的任何存储库的权限.

Upload (tilde here refers to apache's homedir) ~/.ssh/id_rsa.pub to github and give apache access to whichever repos it needs to pull from.

然后您可以通过再次su到apache并运行git pull

Then you can test on the server by again su'ing to apache and running the git pull

su apache
cd ~/working-copy
git clone my-project

一旦成功,您应该能够通过 PHP 运行 git pull.

Once that's working you should be able to run a git pull through PHP.

这篇关于从 php 脚本运行 git pull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17