Skip to content

Automate Deployment to VPS/Cloud Using a Shell Script

Updated: at 02:00 PM

Deploying side projects doesn’t have to be complicated. For those times when you want to get your Node.js application live without dealing with complex CI/CD pipelines, here’s a straightforward shell script that handles the deployment process. It connects to your server via SSH, pulls the latest changes, installs dependencies, builds the project, and restarts the application using PM2. This approach is simple, effective, and ideal for small projects.

Note: This guide is specifically tailored for Node.js environments. While the core concepts can be adapted for other technologies, the script utilizes Node.js-specific tools like npm and PM2. If you’re using a different stack, you’ll need to modify the commands accordingly.

Why Use PM2?

If you’re not already using PM2, it’s a powerful tool worth considering. PM2 is a production process manager for Node.js applications that helps keep your apps running reliably, even in the event of unexpected crashes. It provides features for process management, automatic restarts, load balancing, and monitoring.

Benefits of PM2:

Setting Up PM2

To install PM2 globally on your VPS, run:

sudo npm install pm2@latest -g

Start your application with PM2:

pm2 start app.js --name "your-app-name"

Replace app.js with the entry point of your application, and "your-app-name" with the name you want to assign to your app.

The Shell Script

Save the following script as deploy.sh on your local machine:

#!/bin/bash

# Your VPS user and IP address
VPS_USER="your_user"
VPS_IP="your_vps_ip"

# Your project directory on the VPS
PROJECT_DIR="/path/to/your/project"

# Your Git branch
GIT_BRANCH="main"

# Connect to the VPS and execute commands
ssh -T $VPS_USER@$VPS_IP << EOF
  echo "Pulling the latest changes..."

  cd $PROJECT_DIR || exit

  # Pull the latest changes from the repository
  output=\$(git pull origin $GIT_BRANCH)

  # Check if the branch is already up to date
  if [[ "\$output" == *"Already up to date."* ]]; then
    echo "Branch is already up to date. No deployment necessary."
    exit 0
  fi

  echo "New changes detected. Preparing for deployment..."

  # Install dependencies
  npm install

  # Build the project
  npm run build

  # Restart the application with PM2
  pm2 restart your-app-name
EOF

echo "Deployment complete."

How to Use

  1. Update the Variables: Replace the placeholders with your actual VPS username, IP address, project directory, Git branch, and PM2 app name.

  2. Make the Script Executable:

    chmod +x deploy.sh
    
  3. Run the Script:

    ./deploy.sh
    

Your latest code changes will now be live on your server.

Why This Approach?

While tools like GitHub Actions and other CI/CD solutions are powerful, they can be overkill for small projects. This script provides a simple way to automate deployments without the need for complex setups, allowing you to focus on development.

Applicability to Other Environments

Although this guide is designed for Node.js applications using PM2, the core concept of SSH-ing into your server and running deployment commands can be adapted to other environments:

However, note that PM2 is specifically for Node.js applications. For other environments, you’ll need to use equivalent process managers such as:

Customization

You can adjust the script to suit your needs:


Note: This approach is suitable for personal and small projects. For larger, enterprise-level applications, consider implementing more robust deployment strategies.


Next Post
Cloud Hosting for Side-Projects - Best Budget-Friendly Providers