使用 PHP 代码自动化 git pull

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23136044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:31:31  来源:igfitidea点击:

Automation of git pull using PHP code

phpgitbitbucket

提问by Abhishek Sachan

I am working to automate the task of 'git pull'from bit-bucket server to my godaddy shared hosting. I have installed Git on Godaddy server and able to 'git clone', 'git pull'etc from command line remotely. But now I want to write a PHP code to run 'git pull' directly from browser.

我正在努力将“git pull”从位桶服务器到我的godaddy共享主机的任务自动化。我已经在 Godaddy 服务器上安装了 Git,并且能够从命令行远程'git clone''git pull'等。但是现在我想编写一个 PHP 代码来直接从浏览器运行“git pull”。

PHP function exec()can be used for this but git pull from bit-bucket requires a password. I searched a lot on internet but can not find how to provide password from PHP code.

PHP 函数exec()可用于此,但 git pull from bit-bucket 需要密码。我在互联网上搜索了很多,但找不到如何从 PHP 代码中提供密码。

Note: I have tried setting up passwordless authentication between 2 server(Godaddy - Bitbucket), but it didn't work. So I am left with above method only.

注意:我尝试在 2 个服务器(Godaddy - Bitbucket)之间设置无密码身份验证,但没有奏效。所以我只剩下上面的方法了。

EDIT: I have completed the setup and now able to update the godaddy server with one click. However, the PHP code part didn't work for me due to restrictions on Godaddy's severs. So I have created a batch script for the same, a passwordless authentication to server and automated git pull command. Here are steps to do it (may be helpful for any one with similar problem): http://abhisheksachan.blogspot.in/2014/04/setting-up-godaddy-shared-hosting-with.html

编辑:我已经完成了设置,现在可以一键更新 Godaddy 服务器。但是,由于 Godaddy 服务器的限制,PHP 代码部分对我不起作用。因此,我为此创建了一个批处理脚本,对服务器进行无密码身份验证和自动 git pull 命令。以下是执行此操作的步骤(可能对遇到类似问题的任何人都有帮助):http: //abhisheksachan.blogspot.in/2014/04/setting-up-godaddy-shared-hosting-with.html

回答by sfyn

If you use https instead of ssh you can specify user/password directly in the request:

如果您使用 https 而不是 ssh,您可以直接在请求中指定用户/密码:

git clone:

git克隆:

exec("git clone https://user:[email protected]/user/repo.git");

git pull:

git拉:

exec("git pull https://user:[email protected]/user/repo.git master");

Alternatives to exposing your password:

暴露密码的替代方法:

  • Use a passwordless ssh key on the target system.
  • Use client-side certificates for https.
  • 在目标系统上使用无密码 ssh 密钥。
  • 为 https 使用客户端证书。


Update:if you need to get at the exec command output to debug or verify things, you can pass it an array argument, and then output using standard iterative techniques to the location of your choice. Here is an example that simply prints the output:

更新:如果您需要获取 exec 命令输出以进行调试或验证,您可以将数组参数传递给它,然后使用标准迭代技术将输出输出到您选择的位置。这是一个简单地打印输出的示例:

function execPrint($command) {
    $result = array();
    exec($command, $result);
    foreach ($result as $line) {
        print($line . "\n");
    }
}
// Print the exec output inside of a pre element
print("<pre>" . execPrint("git pull https://user:[email protected]/user/repo.git master") . "</pre>");

Since the $resultarray will be appended to, and not overwritten, by exec()you can also use a single array to keep a running log of the results of exec commands.

由于$result数组将被附加到而不是覆盖,因此exec()您还可以使用单个数组来保留 exec 命令结果的运行日志。