All Collections
Administering Server and Website
Deployment and Staging Management
Automatically Deploy From Git to Server Using Webhooks
Automatically Deploy From Git to Server Using Webhooks

3 easy steps to automatically deploy from Git to Server using Webhooks [with code example].

Cloudways Product avatar
Written by Cloudways Product
Updated over a week ago

Table of Contents

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 the Cloudways server and your git repo. You can continue once you have set up your SSH connection in your repository.

How to Automatically Deploy From Git to Server Using Webhooks

Step 1: Generate API Key

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

Classic Interface

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:

<?php
const API_KEY = "YOUR API KEY HERE";
const API_URL = "https://api.cloudways.com/api/v1";
const EMAIL = "YOUR EMAIL GOES HERE";

/* examples
const BranchName = "master";
const GitUrl = "[email protected]:user22/repo_name.git";
*/

//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));
}
curl_close($ch);
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));
?>

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 [email protected]:username122/phptest.git)

  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:

example.com/gitautodeploy.php?server_id=12345&app_id=678910&git_url=[email protected]:username123/phptest.git&branch_name=master.

Here, example.com needs to be replaced with your Cloudways application URL.

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.

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.

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.

That’s it! We hope this tutorial was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.

Did this answer your question?