How to automatically deploy from GitHub Private Repository to cPanel using Github Action

| 3 min read
Auto-deployement cPanel to GitHub

Hello there! If you're here, it means you're looking to set up auto-deployment in your cPanel from a GitHub repository. I've been in the same boat, starting with manual uploads to cPanel via File Manager or FTP clients like FileZilla. I then explored cPanel's GitHub feature, which allowed pulling from GitHub, but it still involved manual steps. So, here's a simple solution to lift that burden off your shoulders – using GitHub Actions to auto-deploy a private repository to cPanel on each push. Before starting steps lets me introduce GitHub Actions.

What is GitHub Actions?

GitHub Actions is a CI/CD tool for the GitHub flow. You can use it to integrate and deploy code changes to a third-party application platform as well as test, track, and manage code changes.

At the most basic level, GitHub Actions brings automation directly into the software development lifecycle on GitHub via event-driven triggers. These triggers are specified events that can range from creating a pull request to building a new brand in a repository. All GitHub Actions automations are handled via workflows, which are YAML files placed under the .github/workflows directory in a repository that define automated processes.

How to use GitHub Actions for Deployment?

First you have to create a repository in you local machine and push the latest changes to your private github repository. Another thing you need is cpanel account. In cpanel you have to create a ftp user account, which we will need later.

Let's get started

STEPS:

  1. Log in to your cPanel account and create an FTP account. After creation, copy the FTP username, password, hostname, and port – we'll need these for GitHub Actions. Test the FTP account using an FTP client to ensure everything is set up correctly.
Cpanel Create FTP Account
Cpanel Create FTP Account
  1. Log in to your GitHub account and navigate to your repository.
  2. Click on the "Actions" tab, and you'll find the "set up a workflow yourself" link. Click on it, and it will prompt you to create a .yml file.
Navigate to actions and click create workflow
  1. Copy and paste the provided code into your main.yml file and commit the changes.
on:
  push:
    branches:
      - master
name: 🚀 Deploy website on push
jobs:
  web-deploy:
    name: 🎉 Deploy
    runs-on: ubuntu-latest
    steps:
    - name: 🚚 Get latest code
      uses: actions/checkout@v3
    
    - name: 📂 Sync fisles
      uses: SamKirkland/FTP-Deploy-Action@v4.3.4
      with:
        server: yourdomain.com
        username: ${{ secrets.ftp_username }}
        password: ${{ secrets.ftp_password }}
action code for .yml file
  1. Go to "Settings."
  2. Navigate to "Secrets and variables" from left menu and from dropdown click on "Actions" menu.
Create secrets for gtihub actions
  1. Create repository secrets that has been used in username and password in above .yml file
Create secrets for gtihub actions

Congratulations! You have successfully completed the configuration. Now, pull the changes from GitHub that we made by creating the main.yml file. Make some modifications in your local repository and push those changes to your GitHub repository.

Navigate to your GitHub Actions, and you will find a list of actions that were executed during the push. A successful deployment will be indicated by a green icon, while an unsuccessful one will display a red icon. If green icon shown go to your site and see the changes, if red then click the red icon link and see the deployement error message and fix the issue.

Copy paste yml code in .yml file

With these steps, you've now set up GitHub Actions for auto-deployment. Pushing updates to your GitHub repository will trigger an automatic deployment to cPanel, making your development workflow smoother.

You can check this https://github.com/SamKirkland/FTP-Deploy-Action to view more actions. Also check official documentation of GitHub Actions

If your are unable to deploy inbox me at [contact@rawbinn.com]