GitHub Actions: The Deployment Script I Copy to Every Project
A dead-simple, highly pragmatic GitHub Actions workflow for deploying Node.js and Next.js applications.
I used to manually SSH into the server, git pull, run npm install, and restart PM2. We've all been there. It feels fast the first time, but doing it for the 100th time at 2 AM when a bug is blowing up production is a recipe for disaster.
Over the years, I've consolidated my workflow into a single GitHub Actions script that I literally copy and paste into almost every new project. It's not over-engineered. It doesn't use massive enterprise Kubernetes orchestrations. It just works.
The Core Requirements
A good deployment script for a small-to-medium project needs to do three things:
- Run on pushes to the
mainbranch. - Install dependencies, run linters, and build the project (to catch errors before touching production).
- SSH into the server, pull the latest code, build it remotely (or push the built artifacts), and restart the process safely.
The Pragmatic Approach
Instead of worrying about Docker registries or zero-downtime rolling deployments right out of the gate, start with a simple SSH execution action.
Give GitHub Actions a securely stored SSH key via Secrets. Have it execute a bash script on your VPS that handles the pull and restart.
Is it perfectly immutable infrastructure? No. Does it save you hours of manual deployments and eliminate "I forgot to run npm install" errors? Yes.
Automate the obvious pain points first. You can always over-complicate your DevOps later.