Deploying website using cPanel is boring. Zip your code, upload through cpanel file manager, unzip it — Too much manual work just for one deployment. Infact, just to update the typo you just found out! I'm a lazy man who want to automate all the manual task. So, I used github actions to automate the whole deplyment process. Just one push is all you need. 😉
To do this lets first generate RSA key pairs using the following command. (Note: whatever prompt comes after that, Just press ENTER.)
ssh-keygen -t rsa
You will find private and public key as id_rsa
and id_rsa.pub
in ~/.ssh/
folder.
Copy the public key and go to cPanel's 'SSH Access'. Click 'Manage SSH Keys' > 'Import Keys' > Paste the public key into public key text box > 'Import'.
After following the steps you will find that Authorization Status is not authorized
. To authorize the key, Click 'Manage' buttion in the same row and press 'Authorize'.
Now we are done with cPanle. Lets go to our website's github repo. To automate the deplyment process we need authentication to access the server. To store our authentication credentials and secrets we will use 'Secrets and variables' actions under Settings tab.
We will store our server domain name/IP, Username and Private Key by pressing 'New repository secret' button for each secret.
After that lets pull our website using git pull
and create .github/workflows/deploy.yml
file in the root directory of the source code. In this file, we will paste the following YAML code. (make change according to your need)
name: Deploy Website
on:
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "22.2.0"
- name: Build Project
run: |
npm ci
npm run build
- name: Setup SSH Key
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Deployment
env:
REMOTE_HOST: ${{ secrets.REMOTE_HOST }}
REMOTE_USERNAME: ${{ secrets.REMOTE_USERNAME }}
run: |
zip -r dist.zip ./dist/
# Remove existing files on remote server
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa $REMOTE_USERNAME@$REMOTE_HOST 'rm -rf ~/public_html/* || true'
# Upload new files
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa dist.zip $REMOTE_USERNAME@$REMOTE_HOST:~/public_html/
# Unzip and move files
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa $REMOTE_USERNAME@$REMOTE_HOST 'cd ~/public_html && unzip dist.zip && mv dist/* . && rm -rf dist*'
Now, everytime when you will push the source code into github on master branch, an Action will trigger and deploy you code into your server.