How to Automatically Deploy From Git to Server Using Webhooks

 How to Automatically Deploy From Git to Server Using Webhooks

Git auto-deployment allows a user to get updated code deployed on their live servers as soon as the changes in the remote repositories occur. You can achieve this using Cloudways API, but it’s necessary that git repository supports git over SSH.

Before you start, learn the manual deployment of your repository using Git. This will allow you to exchange SSH keys between Cloudways server and your git repo. You can continue once you have setup your SSH connection in your repository.

Step 1: Generate API Key

Log into the Cloudways Platform,  and generate your API key using this link. Copy the Key and Email as it will be required as a part of the script in the next step.

Generate API Key

Step 2: Create Auto-deployment File

You can either use an existing application or launch a new Application on your Cloudways Server. Inside your applications public_html folder, create a file and name it gitautodeploy.php. This file will enable the auto-deployment of the code on the given server and application of your Cloudways Account. Copy and paste the code below into the file:

const API_URL = “”;
/* examples
const BranchName = “master”;
const GitUrl = “”;
//Use this function to contact CW API
function callCloudwaysAPI($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));
return json_decode($output);
//Fetch Access Token
$tokenResponse = callCloudwaysAPI(‘POST’, ‘/oauth/access_token’, null
, [
’email’ => EMAIL,
‘api_key’ => API_KEY
$accessToken = $tokenResponse->access_token;
$gitPullResponse = callCloudWaysAPI(‘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));
view rawdeployApplication.php hosted with ❤ by GitHub

Step 3: Add a Webhook in Your Git Repository

A simple webhook will be required, which will trigger the request to the app we created above and will auto-deploy the code.

To generate a webhook, copy your application URL and append these arguments at the end:

    1. filename = (auto-deployment file name)
    2. server_id = ( your server id on which you need to deploy the code)
    3. app_id = ( your app id where you need to deploy the code )
    4. git_url = ( it should be something like
    5. branch_name = ( branch name in your repo eg. master )
    6. deploy_path = ( leaving it empty will deploy code at public_html, optional parameter)

Webhook URL Example:

To add a Webhook on Bitbucket:

  1. Get into your Bitbucket repo and click Settings
  2. Now under WORKFLOW, select Webhooks and Click on Add Webhook
  3. Name your webhook and add the webhook URL to your app, then Click Save.

Bitbucket Add Webhook for git deploy to server

You can add a webhook in your Github account by:

  1. Get into your Github repo and click Settings
  2. Now click Webhooks and Click on Add Webhook
  3. Name your webhook and add the webhook URL to your app, then Click Save.

Github Add Webhook for automatic git deployment

You can test your webhook while making a small change to your repository which will automatically trigger your application, to generate a pull request using Cloudways API. For complete documentation of the API, visit our playground.