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:
- Process Management: Start, stop, and restart your applications with ease.
- Automatic Restarts: Automatically restarts your application if it crashes.
- Cluster Mode: Utilize all available CPU cores for improved performance.
- Monitoring: Offers detailed insights into application performance and resource usage.
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
-
Update the Variables: Replace the placeholders with your actual VPS username, IP address, project directory, Git branch, and PM2 app name.
-
Make the Script Executable:
chmod +x deploy.sh
-
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:
- Python/Django: Replace
npm install
andnpm run build
withpip install -r requirements.txt
and any necessary build commands. - Ruby on Rails: Use
bundle install
and other Rails-specific commands. - Go, Java, etc.: Modify the script to include the build and restart commands relevant to your application.
However, note that PM2 is specifically for Node.js applications. For other environments, you’ll need to use equivalent process managers such as:
- Python: Gunicorn, uWSGI
- Ruby: Puma, Unicorn
- Java: Use the built-in server or an application server like Apache Tomcat.
Customization
You can adjust the script to suit your needs:
- Different Build Commands: If you’re not using
npm
, modify the build commands accordingly. - Additional Steps: If you need to run database migrations or other tasks, insert those commands into the script.
- Logging: To keep a record of deployments, you can redirect output to log files.
Note: This approach is suitable for personal and small projects. For larger, enterprise-level applications, consider implementing more robust deployment strategies.