16

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

Marc
  • 4,661
  • 3
  • 40
  • 62
Abhishek Sachan
  • 1,976
  • 2
  • 18
  • 33
  • whats wrong with using push from git its self? – Dave Apr 17 '14 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. – Abhishek Sachan Apr 18 '14 at 00:56
  • Please see also this workaround that might be usefull for you: https://stackoverflow.com/questions/9978400/git-auto-pull-from-repository/67889529#67889529 – DrBeco Jun 08 '21 at 14:59

1 Answers1

34

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.

XDjuj
  • 103
  • 1
  • 5
sfyn
  • 686
  • 8
  • 14
  • Thanks for reply it worked in Command line but there is no output in PHP. I looked @ http://stackoverflow.com/questions/8562544/executing-git-commands-via-php but I am a windows user so could not get how the solution worked for them! If you know about it please help.! – Abhishek Sachan Apr 17 '14 at 15:12
  • Make sure that php can actually run exec - for security reasons this command is sometimes severly limited on servers. – sfyn Apr 17 '14 at 15:20
  • 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. – sfyn Apr 17 '14 at 15:21
  • 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'). – Abhishek Sachan Apr 18 '14 at 00:50
  • It seems like this won't be possible using PHP on Godaddy Server. I will try to create a batch file to automate this task as I can now use pull without password. – Abhishek Sachan Apr 18 '14 at 00:58
  • im getting this echo "19:09:04 up 6 days, 8:33, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT" but does not pull latest update – Jah Dec 03 '16 at 19:15
  • 2
    you can do it in 1 line: `echo implode("\n", $result);` – vladkras Mar 14 '17 at 09:07
  • your execPrint function returns a void rather then a string what you seem to expect on your last line of code. – luukvhoudt Jun 27 '18 at 07:37