Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automation of git pull using PHP code

Tags:

git

php

bitbucket

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

like image 405
Abhishek Sachan Avatar asked Apr 17 '14 14:04

Abhishek Sachan


People also ask

Can I use git with PHP?

PHP allows you to create web apps that run pretty much anywhere, including on cheap hosting. Instead, git allows you to keep track of the changes in your source code. You can use Git in your PHP projects to leverage the best of both.

How do I git pull from a branch?

In case you are using the Tower Git client, pulling from a remote is very easy: simply drag the remote branch and drop it onto your current HEAD in the sidebar - or click the "Pull" button in the toolbar.

Does git pull connect to remote?

The git pull command automatically fetches and then merges the remote data into your current branch. Pulling is an easier and comfortable workflow than fetching. Because the git clone command sets up your local master branch to track the remote master branch on the server you cloned.


1 Answers

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.

like image 186
sfyn Avatar answered Oct 05 '22 04:10

sfyn