push only last committed files to the server from git remote repo using github webhooks

295 Views Asked by At

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&[email protected]: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 protected]";




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

1

There are 1 best solutions below

6
VonC On

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.