19

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

3
  • whats wrong with using push from git its self? Commented Apr 17, 2014 at 14:58
  • 1
    it needs me to ssh into server and then git pull from there. It requires to put password twice and wait for long time to update a small code on godaddy server from bitbucket. I want to automate this part using a PHP script so that no one can see my passwords. I have many developers working on a single application. Commented Apr 18, 2014 at 0:56
  • Please see also this workaround that might be usefull for you: stackoverflow.com/questions/9978400/… Commented Jun 8, 2021 at 14:59

1 Answer 1

36

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

git clone:

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

git pull:

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.

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:[email protected]/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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for reply it worked in Command line but there is no output in PHP. I looked @ stackoverflow.com/questions/8562544/… but I am a windows user so could not get how the solution worked for them! If you know about it please help.!
Make sure that php can actually run exec - for security reasons this command is sometimes severly limited on servers.
Also, exec can be called with an array as an optional second parameter - that array receives the line-by-line output, and you can then display it using a loop and print or something. Adding this as an update.
thanks for the details.. Godaddy Shared hosting had limited the execution of git command from exec() as I am able to run whoami, ls etc but it is not woking on git as well. There is simply no output/blank output in case of exec('git').
you can do it in 1 line: echo implode("\n", $result);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.