February 4, 2021
For new projects, I set up automatic deployment to an Ubuntu VPS via git. I use DigitalOcean for hosting and they have a tutorial for this. However, it is 8 years old and the commands provided now give me errors.
To make this process faster for new projects in the future, I am writing a shortened and modified version of the tutorial here. It will work with any VPS hosting provider:
Connect to the VPS via SSH (I have a shell alias for this command):
ssh [username]@[VPS IP address]
Then initialize a bare git repository:
cd /var
cd repo
mkdir [project-name].git
cd [project-name].git
git init --bare
Aside: This assumes you store repositories in /var/repo. In hindsight, I wish I had stored them in ~/repo, as they are specific to me as a user, but it would be too much work to change this for every project.
Assuming you are still in /var/repo/[project-name].git on the VPS:
cd hooks
nano post-receive
Paste in the following and save:
#!/bin/sh
git --work-tree=/home/[username]/docs/[project-name] --git-dir=/var/repo/[project-name].git checkout -f
Finally, add execution permission to the file:
chmod +x post-receive
This will update your project files on the server, assuming you store them in ~/docs.
You now have two options to restart your app automatically:
I also add these lines to the post-receive file for some projects:
cd /home/[username]/docs/[project-name]/server
npm install --silent
npm run build
pm2 reload [project-name]
This is NodeJS and PM2 specific. It does the following:
.gitignore
.This is just what I could think of for my first project after I realized this was possible. Git hooks are powerful!
Leave the VPS
exit
Initialize a repository on your local machine:
cd [root of your project]
git init
Add a remote path (the name live
can be anything):
git remote add live ssh://[username]@[VPS IP address]/var/repo/[project-name].git
git add .
git commit -m "Initial commit"
git push live master
That's it - your project is deployed. Now you can push your changes from the local machine, and they will automatically be deployed on the server.