Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

How to Automate Git Using Cloudways API

Updated on  24th November

5 Min Read
Reading Time: 5 minutes

If you develop websites and attempt to run them on your unmanaged servers, you must be aware of the laborious process involved in deploying the application and pushing its future updates. Cloudways Platform, however, has made it easy to deploy web applications without going through the process of specific server configuration, making the process cost-efficient and flexible.

Automate Git Using Cloudways API

Cloudways facilitates Developers and Web agencies to deploy their code from the git repository as well. Git is the most popular and widely used Source Code Management (SCM) system. It’s also open source and free. Cloudways supports Git over SSH which identifies and authorizes server’s connectivity with your git repo.

Git offers you many well-implemented features, and one of the most useful features is its code deployment flexibility. Through the use of a “Webhooks” system, git allows developers to extend functionality by specifying scripts that git will call based on different events and actions.

In this guide, we will explore the idea of git hooks and demonstrate how to automate git using Cloudways API. We will focus on the code that can assist you in automating your web deployment task from git to Cloudways server.

Automate Git Using Cloudways API

First of all, deploy code to your Cloudways application using git. You should be familiar with how to use git in a general sense.

Let’s take a look at the scenario here. Imagine you are working on a project, and you are managing it via git. Your project is on Cloudways, and what you need is this: whenever you push something to your repository, your Cloudways server automatically pulls it and updates the code.

After successful code deployment on Cloudways, we are going to write some code which will automate the git pull request using Cloudways API. To manage API codes and requests, we are going to install and use Guzzle HTTP Client.

Create a new file CloudwaysAPIClient.php in the directory where you had installed Guzzle and also deployed your repository code. Add the following code to it:

In the code above, we have defined the Guzzle built-in classes, a constructor that loads the Guzzle and generates the Cloudways API access token using the email address & API key and a function for handling most of the status codes of Cloudways API.

Lastly, there is a function git_pull($serverid,$appid,$giturl,$branchname) which helps pull the code from git using Cloudways API. This function requires 4 necessary parameters which are the requirements for a pull request.

Now create a new file gitpull.php in the same directory. It will help us use the CloudwaysAPIClient class to manage a pull request. The code for the gitpull.php is as follows:

In the code from gitpull.php file above, we first included CloudwaysAPIClient.php using include ‘CloudwaysAPIClient.php’; statement. It’s the file where we created our class.

After that, we defined some string variables with the necessary information required by Cloudways API.

Then, we created an object of the CloudwaysAPIClient class and called the git_pull($server_id,$app_id,$git_url,$branch_name) function to run the pull request.

Creating Webhook

We have finished the code. Now, we are going to set up the Webhook. Webhooks allow you to build integrations which support individual events on git. When one of those events is triggered, Git sends a POST call to the URL, defined in Webhooks’ configurations. Repository webhook can be created using few clicks:

Head over to the repository’s Settings page:

Git Repository Settings

Click on Webhooks:

Git Webhooks

Tap the Add webhook button:

Git Add Webhook

You’ll have to fill out a few settings before you can start using webhooks. Let’s go through each of these settings now:

Git Add Webhook Form

Payload URL

A URL is required at which the payloads will be delivered. Set the URL of gitpull.php file in this box. As I created this file in my server root, the URL should look something like this:

Content Type

This is the media type used to serialize the payloads. Supported values include JSON and HTML form. Select application/json in Content type.


This is a string, passes with the HTTP requests as an X-Hub-Signature header. Its value is computed as the HMAC hexdigest of the body, using the secret as the key. It is optional, so we are going to leave it blank.


These are the core of webhooks. The webhook fires whenever an individual event is taken on the repository on which your server’s payload URL loads and acts upon.

From radio checks, we will select Just the push event because whenever we push something to our repository, this webhook will take action and run the payload URL. At the backend, Cloudways API will handle the rest and run the pull request.


From our experience, we can tell that automating git using Cloudways API is quite useful for Cloudways users. If you have followed this far, you should be able to see the exact change in your code whenever you push something to your repository. It can help you save your time, and maintain quality standards. Just code from wherever you want, test it, and push it to your repository. Webhook and Cloudways API will handle the rest.

Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.