Question

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): http://abhisheksachan.blogspot.in/2014/04/setting-up-godaddy-shared-hosting-with.html

Was it helpful?

Solution

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

git clone:

exec("git clone https://user:password@bitbucket.org/user/repo.git");

git pull:

exec("git pull https://user:password@bitbucket.org/user/repo.git 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);
    print("<pre>");
    foreach ($result as $line) {
        print($line . "\n");
    }
    print("</pre>");
}
// Print the exec output inside of a pre element
execPrint("git pull https://user:password@bitbucket.org/user/repo.git master");
execPrint("git status");

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top