Automate Github Deployment With cPanel
Automate Github Private Repository With CPANEL Using Git Version Control And Github Action
The Git Version Control feature enables developers to host Git repositories on their cPanel accounts with ease. Git can be used to manage any collection of files, including website files and assets, software development projects, and also basic text files. The Git version control is incredibly useful for developers and makes cloning repositories easier and more accessible to beginners. Visit cPanel Git Version Control to learn more.
I will introduce a simplified method for cloning your repository from Github:
1. Generate SSH Key
Delete the known_host file from the file manager --> .ssh folder.
and then open Terminal from your cPanel (if you don't have terminal access, ask your hosting provider to enable this)
Run the command provided below to generate a new SSH key and press enter three times.
ssh-keygen -t rsa -C "your github email"
Go to the .ssh folder from your cPanel File Root, Edit id_rsa.pub and copy the content.
2. Setup Deployment Key
Go to your git repository settings --> Deploy key --> add deploy keys --> and paste the copied content.
and then run the below command.
ssh -T git@github.com
If it returns this Hi **username/repository** You've successfully authenticated, but GitHub does not provide shell access.
You're ready to go.
3. Create Git Action to automate
In order to automate the deployment process, you must have a .cpanel.yml file in your top-level directory of your repository. Create a .cpanel.yml file and set up this code below:
---
deployment:
tasks:
- export DEPLOYPATH=/home/<username>/public_html # Add /<app_folder> if required
- /bin/cp <file_name> $DEPLOYPATH #Copy specific file to destination from root
- /bin/cp /<sub_folder>/<file_name> $DEPLOYPATH #copy specific file from source sub folder
- /bin cp * $DEPLOYPATH #copy all from root
- /bin cp /<sub_folder>/* $DEPLOYPATH #copy all from sub folder root
- /bin/cp -r * $DEPLOYPATH #copy all recursively to $DEPLOYPATH
- Replace
<username>
with your cPanel username. - Place the file at the root of your project
- The file must be in the repo before you clone it with cPanel
- Be sure to edit the file to leave only the lines you need and don't leave all the commands active
- There must be no unmerged pull requests or the deploy button will be deactivated
- There must be no errors in the file or the deploy button will be deactivated.
4. Setup Git Version Control
Now go to git version control from your cPanel.
And give the same information about your git repository as shown below:
Enter your git repository URL :
git@github.com:username/respository-name
Enter the desired path for the repository’s directory. If you enter a path that does not exist, the system will automatically create the directory when it creates or clones the repository.
This name does not impact functionality, and instead functions only as a display name.
If everything looks good, go ahead and click on "Create."
5. Manage Repositories
A list of all Git repositories currently connected to cPanel will appear. When you click Manage, you will be given the choice to Pull and deploy from the Git repository. Next, we will automate the process.
This tutorial has been tested on a private repository, and the result showed that it works.