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 Clone Github Repository Using Cloudways API

Updated on October 19, 2016

8 Min Read
Reading Time: 8 minutes

Cloudways API has been released to all developers and re-sellers, with some excellent third-party solutions. In order to educate the developers, I have written several tutorials that highlight how to purge Varnish using the API and carry out API based CRUD operations for servers and applications.


In this article, I am going to explain how you can clone your GitHub application from your Git repository to the Cloudways application. The process has the following five steps that systematically cover all aspects of  cloning the GitHub app onto the Cloudways platform.

  1. Create server and application on Cloudways.
  2. Get the API Secret Key.
  3. Get the SSH key and deploy in GitHub.
  4. Get branches of GitHub Repository.
  5. Clone the application.

Create Server and Application on Cloudways

The first step is to launch the server and then the application on Cloudways platform. To illustrate the process, I have created the following GIF.

php server stack

Simply follow the steps in the GIF and the server and the application is ready for the next step.

Get the API Secret Key

This is an important step for the process. API key is an important of the cloning process. The API key could be found in the application tab after the signing into the platform.


Get the SSH Key and Deploy in GitHub

Getting the SSH requires a bit of code.

First I will make a class to connect with Cloudways API using authenticated access token and then pass email, and the API key to prepare access token. The function prepareacesstoken() will then send the request to Cloudways API for access token. Once received, it will be saved in  $accesstoken.

I have used Guzzle for making HTTP requests. If you  have not installed Guzzle, you should refer  How to create and delete applications via cloudways api . Now create a file CloudwaysAPI.class.php and copy-paste the class code from the above mentioned tutorial. I will next extend this class with new methods.

get_servers() > to get your launched server,

get_applications() > for launched applications,

GenerateKey() > for SSH key generation,

GetKey() > for fetching key,

Since you have copied the class from above article, add the following methods to CloudwaysAPIClient class.

Notice that I have passed two parameters in GenerateKey() and GetKey() methods. These two parameters,server ID and application ID are required to create the SSH key.

Now create a file gkey.php and add the following PHP code to it.

I included the parent class file and then passed the API key and email address. These are required for the access tokens. After that, I fetched the server’s list, which will be shown in the select box.

I will now create a simple form to show servers and applications. I will use bootstrap for UI design so the bootstrap CDN should be added to the file. Now in the body tag, add the following code.

This code will generate a basic form with servers’ name in the server select box. By using the foreach loop, I populated the server select box. Now, every time you select a server name, it will automatically fetch the applications of that server. To achieve this, add the following script before closing the body tag.

This concludes the process of getting the SSH key using Cloudways API. When the form is submitted, the POST variables catch the values and pass them over to GenerateKey() method as arguments. The response will be returned, collected in the variable $success and will be shown in the textarea.


Copy the SSH key and deploy it in your GitHub repository. I have created a repo with demo code. See the following screenshot for adding the key to the repository.


Once successful, the Cloudways SSH key has been deployed to the GitHub repo. This also finishes the main part of the application.

Get Branches of GitHub Repo

After deploying the key,  the next step is fetching GitHub branches in the application that needs to be cloned. At this point, I will add the GetBranches() method in CloudwaysAPI.class.php

Now create another file cloneapp.php. Next, I will create a form in the bootstrap Modal that will be shown upon page load. In the form, select the server and the application and input your GitHub SSH URL. To fetch the branches, I will pass three arguments, the Server ID, Application ID and the Git URL. Add the following code. To the file’

To show the applications available on a specific server, add the following JavaScript code.

The Submit button sends the arguments to GetBranches() method and fetch branches from Github repo. You must also add the SSH URL of your GitHub repo. To send the response, add the following PHP code.

Finally, $options has the branches from the GitHub repository.


Read more: Enhanced Cloudways Staging Environment Is Now Available for All Users

Clone Application in the Cloudways App

The final step is the actual cloning of the GitHub application. Add the last method, GetClone().  This method will need five parameters: server ID, application ID, git url, git branch and the deploy path.

Now add the following HTML code in the body tag of clone app.php.

Add the PHP code that will deploy your GitHub app in Cloudways

Add JavaScript to auto select application for the servers.



This is a lengthy tutorial with several inter-dependent steps. However, if you follow the order of the steps, you will see that  the code works without any hassle. If you still have a question about the code or wish to add to the conversation, do leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

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