
Automate Node.js Deployment with Git and cPanel Hooks
Automate Node.js Deployment with Git and cPanel Hooks
Manual deployment of Node.js applications can be time-consuming and error-prone. Automating deployment using Git version control and cPanel deployment hooks can streamline updates, reduce downtime, and improve your development workflow.
What is Deployment Automation?
Deployment automation refers to the process of automatically updating your production server with new code changes pushed to your Git repository. This reduces manual intervention and ensures your Node.js app is always running the latest version.
Prerequisites
- Active cPanel hosting account on Hiverift
- Node.js application already set up in cPanel
- Git repository (GitHub, GitLab, Bitbucket, or self-hosted)
- SSH access to your cPanel server
- Basic knowledge of Git and shell scripting
Step 1: Set Up Your Git Repository in cPanel
- Login to your cPanel dashboard.
- Navigate to Git Version Control under the Files section.
- Click Create to clone your remote Git repository or create a new repository.
- Set the deployment directory where your Node.js app will reside.
- Clone the repository to your server.
Step 2: Configure cPanel Deployment Hooks
Deployment hooks are scripts triggered automatically after Git operations like pull or push. To configure them:
-
- Navigate to your repository folder in
~/.cpanel/hooks/
or create the folder if it doesn’t exist. - Create a post-receive hook script named
post-receive
inside.git/hooks/
directory of your repository. - Make sure the script is executable:
- Navigate to your repository folder in
chmod +x post-receive
Step 3: Write the Deployment Script
The deployment script automates installing dependencies, building your app, and restarting the Node.js process. Here is a sample post-receive
script:
#!/bin/bash
# Navigate to the app directory
cd /home/your-cpanel-user/path-to-your-node-app || exit
# Pull latest changes (if needed)
git pull origin main
# Install npm dependencies
npm install
# Build the app if necessary (uncomment if your app requires build step)
# npm run build
# Restart the app using PM2 or cPanel's Node.js app manager
pm2 restart your-app-name || pm2 start app.js --name "your-app-name"
Note: Adjust paths, branch names, and app names to match your environment.
Step 4: Integrate Git Push with Deployment
When you push changes to your remote Git repository, cPanel can automatically deploy your updates:
- Set up a webhook on your Git hosting service to trigger a pull on your server.
- Alternatively, manually SSH into your server and run
git pull
and the deployment script.
Step 5: Testing the Automated Deployment
- Make a small change in your local Node.js project.
- Commit and push the change to the remote Git repository.
- Verify that the post-receive hook executes automatically.
- Check your app to confirm the changes are live.
Troubleshooting Tips
- Ensure the post-receive hook has executable permissions.
- Check logs for errors (
~/.pm2/logs/
for PM2 or cPanel error logs). - Test each command in the script manually to verify it works.
- Confirm SSH keys are configured properly for Git authentication.
Benefits of Using Git and cPanel Hooks for Deployment
- Faster and reliable deployment with minimal downtime
- Version control integration for traceability
- Reduced manual errors during deployment
- Easy rollback to previous versions if needed
Conclusion
Automating your Node.js deployment using Git and cPanel hooks simplifies the update process and makes managing your applications more efficient. With this setup, pushing code changes triggers an automatic deployment, freeing you to focus on building great features instead of managing servers.