Automate Node.js Deployment with Git and cPanel Hooks

Automate Node.js Deployment with Git and cPanel Hooks

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

  1. Login to your cPanel dashboard.
  2. Navigate to Git Version Control under the Files section.
  3. Click Create to clone your remote Git repository or create a new repository.
  4. Set the deployment directory where your Node.js app will reside.
  5. 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:

    1. Navigate to your repository folder in ~/.cpanel/hooks/ or create the folder if it doesn’t exist.
    2. Create a post-receive hook script named post-receive inside .git/hooks/ directory of your repository.
    3. Make sure the script is executable:
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

  1. Make a small change in your local Node.js project.
  2. Commit and push the change to the remote Git repository.
  3. Verify that the post-receive hook executes automatically.
  4. 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.

© 2025 Hiverift Hosting | Node.js Deployment Automation Guide

Post Your Comment

Build Your Website with HiveRift

From professional business to enterprise, we’ve got you covered!

©2025, Hosting. All Rights Reserved by KhatuShyam Technologies

HiveRift
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.