How to Automatically deploy from GitHub (Git) to cPanel
Tags: github, cpanel, deploy, git
Published: 2022-03-10 05:07:23

## How to Automatically deploy from GitHub (Git) to cPanel

This tutorial provides a quick way of deploying files from GitHub to cPanel.  We are only deploying from Git(Hub); thus making GitHub our single source of truth. In other words, we will be *pulling* from GitHub.

> [cPanel](https://cpanel.net) is  an online Linux-based graphical user interface (GUI) used as a control panel to simplify website and server management. cPanel allows you to publish websites, manage domains, organize web files, create email accounts, and even more. cPanel is one of the popular control panels.

In this tutorial, I shall walk you through configuring and setting up your Git repo and have it auto deploy to cPanel. Let's get started. First, you need to login into cPanel.

## Login in into cPanel

To login to cPanel, on your browser, navigate to `https://yoursite.com/cpanel` or `https://yoursite.com:2083`.

## 1. Navigate to Git Version Control

To navigate to Git Version Control, follow **cPanel** > **Home** > **File** > **Git Version Control**. The GVC allows you to configure deployment for your cPanel-managed repositories.

![Navigate to Git Version Control](https://user-images.githubusercontent.com/24845008/157582596-4513c8bc-201e-40f0-9fe0-6ae19596834f.png)

## 2. See or Link your Repositories

The next screen that you will see will enable you manage your repositories. I have a repository already linked from GitHub to cPanel. If you currently do not have any repo linked, then go ahead and click on "**Create**" .

> If you already have a repo from Git link to your cPanel, please skip to **Step 4**

![See or Link your Repositories](https://user-images.githubusercontent.com/24845008/157582599-f0269e80-edfa-4c89-982f-096aae45a615.png)

## 3. Clone your Repository

This step will enable you to clone your remote repo to cPanel. You should fill in the fields such as **clone URL**, **Repository Path**.

1. The *Clone URL* refers to your remote repository URL. It must begin with either *`http://`*, *`https://`*, *`ssh://`* or *`git://`* __Example__: [https://github.com/Eunit99/eunit99.git](https://github.com/Eunit99/eunit99)

2. *The Repository Path* refers to directory where your repo will live on cPanel. The path is usually `home/YOUR-USERNAME/REPOSITORY-NAME`
If everything looks good, go ahead and click on "**Create**"

![Clone your Repository](https://user-images.githubusercontent.com/24845008/157582604-99b7df8d-7346-417f-a903-be360cb329d6.png)

## 4. Manage Repositories

This step will allow us manage our repositories which we have on cPanel. To navigate to Git Version Control, follow **cPanel** > **Home** > **File** > **Git Version Control**. The GVC allows you to configure deployment for your cPanel-managed repositories.

You will see a list of all repositories currently linked to cPanel from Git. Click on **Manage** to be taken to the next step.

![Manage Repositories](https://user-images.githubusercontent.com/24845008/157582602-e6286cea-a5ea-4968-b196-66c5b6785fd0.png)

## 5. Pull or Deploy

After having filled the fields for information about your repos in step 4 above, now is the time to clone your remote repo to cPanel just like how you would on your local machine 😊

> Pull deployment pulls changes from a remote repository to your cPanel and deploys your new changes live.

1. Click on **Pull or Deploy** tab
2. Click on **Update from Remote**
3. Click on **Deploy HEAD Commit**

![Pull or Deploy](https://user-images.githubusercontent.com/24845008/157582589-2ad7e0cb-647b-4c78-a4bb-57bc8bcc052c.png)

## 🎆✨Congratulations! You have successfully deployed a remote repo to cPanel

😲 But we wanted an auto deployment of new changes from GitHub to cPanel, isn't it? If yes, then keep reading.

## 6. Setting up auto-deployment

In order to deploy changes from a cPanel-managed repository, you must have a `.cpanel.yml` file in your top-level directory of your repository. Create a `.cpanel.yml` file and add this code below:

![Setting up auto-deployment](https://user-images.githubusercontent.com/24845008/157591349-e4926a7f-07fb-40d0-92d0-e34e020fb795.png)

``` YML
##### THIS ALLOWS US TO DEPLOY THE SITE FROM A GIT REPO ######
---
deployment:
  tasks:
    - export DEPLOYPATH=/home/your-username/public_html/
    - /bin/cp -R .vscode $DEPLOYPATH
    - /bin/cp -R assets $DEPLOYPATH
    - /bin/cp -R css $DEPLOYPATH
    - /bin/cp -R fonts $DEPLOYPATH
    - /bin/cp -R img $DEPLOYPATH
    - /bin/cp -R js $DEPLOYPATH
    /bin/cp index.html $DEPLOYPATH

```

- Replace `your-username` with your actual cPanel username
- Save and push to your remote repo and repeat **step 5**.
- **NOTE**: My code above tells cPanel the folders and files to automatically pull. Your repo file structure might differ.

## Further reading

1. [Guide to Git™ - How to Set Up Deployment](https://docs.cpanel.net/knowledge-base/web-services/guide-to-git-how-to-set-up-deployment)


> This tutorial has been tested and proven to work on **public repositories** .


### Found this article helpful? Follow me on [Twitter](https://twitter.com/eunit99)