asachanfbd asachanfbd - 2 years ago 103
PHP Question

Automation of git pull using PHP code

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.

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.

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.

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):

Answer Source

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

git clone:

exec("git clone");

git pull:

exec("git pull master");

Alternatives to exposing your password:

  • Use a passwordless ssh key on the target system.
  • Use client-side certificates for 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:

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 master") . "</pre>");

Since the $result array 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download