How to Automate Git Using Cloudways API

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.

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

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?php

include'CloudwaysAPIClient.php';

$api_key='H95P5Yxn1IuSTEtwUdWxYAKSK1TX41';

$email='m.fayyaz@cloudways.com';

$server_id='54048';

$app_id='169021';

$git_url='git@github.com:fayyazgt/Cloudways_Mymodule.git';

$branch_name='master';

$cw_api=newCloudwaysAPIClient($email,$api_key);

$gitpull=$cw_api->git_pull($server_id,$app_id,$git_url,$branch_name);

?>

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:

Click on Webhooks:

Tap the Add webhook button:

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

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:http://phpstack-14846-54048-169021.cloudwaysapps.com/gitpull.php

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.

Secret

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.

Events

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.

Conclusion

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.

Start Creating Web Apps on Managed Cloud Servers Now!

About 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 m.fayyaz@cloudways.com