Will's Blog

Will's Blog

Let's get our Digital Ocean node.js app working just like Heroku

Why?

Heroku is something that I used for microapps, prototypes and crap in the past because it had a free instance that allowed you to plop stuff up on it and everything worked. Something happened at some point and it started to suck. I get that it was free so I can't exactly complain. I wanted something better and was willing to pay for it so started looking for options. Heroku itself was like 100 bucks a month. Holy crap, time to look someplace else. I looked over at Digital Ocean and there was a $5 server that would fit the bill just fine. These are apps just for me or people I know. I don't need a thicc server just something that can serve like 5 users a day (atm :). Digital Ocean's databases are still stupid expensive so if you're looking for persistence look elsewhere for something around 2-3 bucks, go for some sort of shared hosting.

My other post

A Friendly Guide To Automate Deployment For Node.js Apps With Git Hooks

Setup a git server on your droplet

Go to this link where I outline that . You can do it through a public git server, but with 10 minutes and you get all the controls over your git instance...just do that. The main thing you'll need access to is the git hooks.

Make sure pushing to your server works through git. If something doesn't work there you need to make sure that you have the correct chmod values for your git directory. If you didn't create your git server under the git user, or outside of the git home directory you might have this issue.

Get PM2 Going

If you don't have it installed, run this

npm install pm2 -g

If you already have a web app, go to your web directory and you can run

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

Then

pm2 list

If you don't have a web app written yet, come back to this after you setup the repo. PM2 at this point should show something like this

2021-05-04 18_37_56-.png

If you have anything else under the status besides online stop here and get the thing running. The command pm2 logs might help if you are having issues. I was having issues, but after trying a bunch of other things for node.js apps, and you aren't using Kubernetes or Docker, this is the way.

Still over in the web directory we need to link it up to our git repo. If you already have a web app created simply set your origin:

git remote set-url origin /home/git/your-project.git

Otherwise clone it

git clone /home/git/your-project.git your-project-name

You don't have to do everything remotely on the server, but it will help to make sure that permissions are setup correctly. You can also do all the things I wrote on your local machine as well and push them to the git server.

Finishing up

We need to create that hook file. Go back to your /home/git/your-project.git/hooks folder. From there you need to create and edit a post-receive file. I use vim. nano or whatever other text editor will work just as well.

vim post-receive

Fill post-receive out with something like this

#!/bin/bash
APP_NAME='your-app'
GREEN='\e[1;32m'
RESET='\e[0m'
echo 'post-receive: Starting deploy for '${APP_NAME}
echo 'post-receive: Changing directory to /var/www/'${APP_NAME}
cd /var/www/${APP_NAME}
echo 'post-receive: git check out'
git --git-dir=/home/git/${APP_NAME}.git --work-tree=/var/www/${APP_NAME} checkout master -f
echo 'post-receive: git check out complete!'
echo 'post-receive Installing'
# yarn install \
# && yarn build \
npm install \
&& echo 'post-receive Install complete' \
&& echo 'post-receive: Stopping '${APP_NAME}' on pm2' \
&& (pm2 delete ${APP_NAME} || true) \
&& echo 'post-receive: Restarting '${APP_NAME}' on pm2' \
&& pm2 start index.js --name ${APP_NAME} \
&& echo 'post-receive: Restarted '${APP_NAME} \
&& echo -e '${GREEN}post-receive Deployment Complete${RESET}'

Explanation of what this is doing

Change the directory into you project directory.

Checkout the files from your git repository located on the same machine.

Run npm install to install whatever latest crap you've made

Remove the pm2 project from the stuff it's running

Restart it with all the new updates

Modify permissions of post-receive

chmod ugo+x ./post-receive

Note: Should be able to leave off the o above if you are logged in with git user. I was not.

Now from your local repo push something up. You should see your echo statements showing up and showing you the status of what's up. Once it completes you should have a refresh of your app. Heck ya!

#digitalocean#continuous-deployment#deployment-automation#deployment
 
Share this