Deploy your project to CPanel using git post-receive hooks

Is there a straightforward method for deploying your project to shared hosting that does not support complex CI/CD procedures? There is, indeed. In this post, I’ll show you how to use git and post-receive hooks to deploy your application to the server.
This tutorial applies to web hosting that supports ssh and git. Yours must-have these features, given that my $30/year hosting does. If your server does not support them, another option is to use git-ftp, which I will discuss in future posts.
Setup SSH keys
In your Cpanel find the menu named SSH Keys. Add new ssh keys and activate them. Download a private key to your PC and place them in your root directory. (C:\users\username in windows,/home in Linux)
2. Connect to the server
Open the PuTTY application on your PC. Don’t have it? Download it or use git bash. I prefer PuTTY simple and quick.
Enter your hostname/IP address. You can find them on the CPanel homepage.
Enter your port.
Go to SSH/Auth section and load your private key downloaded from above.
If your credentials are correct you can now login to the server.
The server will ask for your username. From Cpanel find out your username and log in using the given credentials.

If the putty is not working, use the in-built terminal in your Cpanel to access the command interface.
If that is not available create a bare repo using the method below and upload the folder to the server using file manager.
3. Create a bare repo
Enter the given commands in the terminal at the base directory in the server.
git init —- bare ~/sabinkhanal.gitcd sabinkhanal.git/hooksnano post-receivechmod +x post-receive
4. Add this code to the post-receive file. Replace the target, branch, and git directory as per your own setup.
TARGET="/home/username/sabinkhanal.com"GIT_DIR="/home/username/sabinkhanal.git"BRANCH="master"while read oldrev newrev refdo# only checking out the master (or whatever branch you would like to deploy)if [[ $ref = refs/heads/$BRANCH ]];thenecho "Ref $ref received. Deploying ${BRANCH} branch to production..."git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -felseecho "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."fidone
5. Create a bash script in your project folder
You can name it to deploy.sh or any name you wish.
Fill your bash script with the given script.
The given code does the following,
- Run ssh-agent and load the private key.
- Add production target to your local repo. The format should be as below. Replace where it is necessary.
- Add the code for adding, committing, and pushing to the server. Customize as per necessity.
eval `ssh-agent` && ssh-add ~/privatekey
&& git remote add production hostname/home/username/sabinkhanal.git || :
&&
--your code here eg: npm run build --
&& git add . && git commit -m "$1"
&& git push production master
6. Run the bash script
Run your bash script as ./deploy.sh with the commit message. (Remember args/$1) . If everything goes well you’ll get a message like this.

Use this method to simplify your deployment to shared hosting. It’s simple and quick. You don’t need a complicated CI/CD setup. You can add your own scripts to your project to further automate or verify it. Thanks.