Automate git pull using PHP code

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

The PHP function exec () can be used for this, but Git requires a password to transfer from the bit bucket. I searched a lot on the Internet but cannot find how to provide a password from PHP code.

Note. I tried setting up passwordless authentication between two servers (Godaddy - Bitbucket), but that didn't work. Therefore, I am left only with the method described above.

EDIT: I completed the setup and now I can update the godaddy server with one click. However, part of the PHP code did not work for me due to limitations on Godaddy severs. Therefore, I created a batch script package for the same, without authentication on the server and an automatic Git pull command. Here are the steps for this (it may be useful for anyone with a similar problem): http://abhisheksachan.blogspot.in/2014/04/setting-up-godaddy-shared-hosting-with.html

+6
source share
1 answer

If you use https instead of ssh, you can specify the 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 disclosing your password:

  • Use ssh key without password in the target system.
  • Use client side certificates for https.

Update: if you need to get the output of the exec command for debugging or checking things, you can pass an array argument to it and then output it using standard iterative methods to the location of your choice. Here is an example that just 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 https://user: password@bitbucket.org /user/repo.git master") . "</pre>"); 

Since the $result array will be added rather than overwritten by exec() , you can also use a single array to log the execution of the results of the exec commands.

+10
source

All Articles