1

I am new to GitHub, i m doing the following

  1. local file push to GitHub remote repo (local -> git remote repo)
  2. and then using web-hooks (once local files push to git remote repo) automatically send all files to my server / web hosting (git remote repo->web hosting)

this is my webhook url

http://domainname.com/ghautodeploy.php?server_id=625349&app_id=2031109&git_url=git@github.com:xxxx/yyyy.git&branch_name=main&deploy_path=it

its working perfectly, but its push all remote repo to my hosting, instead only last committed files.

my webhook code

 <?php
const API_KEY = "xxxxxx";
const API_URL = "yyyy";
const EMAIL = "email@domainame.com";




function callAPI($method, $url, $accessToken, $post = [])
{
    $baseURL = API_URL;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $baseURL . $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Set Authorization Header
    if ($accessToken) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $accessToken]);
    }
  
    //Set Post Parameters
    $encoded = '';
    if (count($post)) {
        foreach ($post as $name => $value) {
            $encoded .= urlencode($name) . '=' . urlencode($value) . '&';
        }
        $encoded = substr($encoded, 0, strlen($encoded) - 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
        curl_setopt($ch, CURLOPT_POST, 1);
    }
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpcode != '200') {
        die('An error occurred code: ' . $httpcode . ' output: ' . substr($output, 0, 10000));
    }
    curl_close($ch);
    return json_decode($output);
}

//Fetch Access Token
$tokenResponse = callAPI('POST', '/oauth/access_token', null
    , [
    'email' => EMAIL, 
    'api_key' => API_KEY
    ]);

$accessToken = $tokenResponse->access_token;
$gitPullResponse = callAPI('POST', '/git/pull', $accessToken, [
    'server_id' => $_GET['server_id'],
    'app_id' => $_GET['app_id'],
    'git_url' => $_GET['git_url'],
    'branch_name' => $_GET['branch_name'],
    /* Uncomment it if you want to use deploy path, Also add the new parameter in your link */
    'deploy_path' => $_GET['deploy_path']  
    
    ]);

echo (json_encode($gitPullResponse));
?>

my github webhook option

enter image description here

the problem is instead pushing only last committed files from (git remote repo -> web hosting) its pushing whole files.

i want to solve this issue, guide me how to do? Note: web-hook working perfectly, no issue

Udhayakumar
  • 261
  • 1
  • 10

1 Answers1

0

One approach, once your webhook has pulled the GitHub repository files, would be to call rsync, in order to send only the delta (new/changed files) to your server.

You have an example in "Copy remote file with rsync in php", which uses an rsync.php with:

exec("rsync -crahvP /path/in/local/files/foldertocopy remoteuser@remoteserveraddress:/path/in/remote/destinationfolder/", $output, $exit_code);

The other approach would involve setting up Git on your remote server, which is not always convenient/possible.
But pushing to your server to a bare repository would also send only deltas, instead of all the files.


Udhayakumar asks in the comments:

Generally, is what I am asking possible in GitHub?

Yes, but with GitHub Actions (meaning, no need for webhook and locally installed listener in PHP)

Use for instance up9cloud/action-rsync or rsync-deployments-action, assuming your remote server has a public internet IP.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • hi VonC, generally is this possible in github what i am asking? – Udhayakumar Jan 01 '22 at 10:59
  • @Udhayakumar Yes, I have edited my answer accordingly. – VonC Jan 01 '22 at 11:14
  • hi @VonC - my remote server don't have a public internet IP. is it possible just using GitHub web-hook url (what i want to achieve)? without doing rsync or GitHub actions – Udhayakumar Jan 01 '22 at 15:24
  • @Udhayakumar Not without sending *all* the files. Hence my proposal to use `rsync`, which only send delta. – VonC Jan 01 '22 at 18:12
  • hi @VonC - using this - You have an example in "Copy remote file with rsync in php". can you give me the sample code for push the (only modified files) from git repo to my web server? Thanks – Udhayakumar Jan 04 '22 at 03:51
  • @Udhayakumar I don't have much more than what you see in that link. I have edited the answer to include the code of its `rsync.php` file. – VonC Jan 04 '22 at 04:34