Deploy a Node App to Digital Ocean via Gitlab Runners

Cam White
Cam White
Jun 22, 2018 · 2 min read

M$ newest purchase got you down? Try Gitlab instead, you can’t argue with free everything and a solid interface to boot. One of my favorite things about it are the runners. Not only are the free of charge, but they are extremely useful and fairly easy to setup.

For this post I will be sharing code samples and configs for executing a deployment from a docker container inside a Gitlab shared runner. The target production server is an Ubuntu 16.04 Digital Ocean droplet.

Digital Ocean Steps

  1. Spin up and setup a droplet of whatever size you’d like.
  2. After completing initial setup, as your non root user, generate SSH keys on the remote machine with making sure to provide no password.
  3. Add the newly created public key to the authorized keys with this command .
  4. At this point, you must have node installed to run which we will use for our daemon.
  5. Next remove or comment out the lines directly below this comment in your .bashrc this allows us to source our profile later on.
  6. Finally and copy the output to clipboard, but keep in mind this is a private key and should be concealed.

Gitlab Steps

  1. Assuming you’ve already created a repository for your project navigate to settings > ci/cd > variables.
  2. Add three variables , , and where the values reflect the keys.
  3. Copy the following code into your project root with the filename this where the magic happens.

However magical it may be, this will always fail on first deploy because pm2 hasn’t been started yet. So we need to do that back on our droplet after it’s failed. Probably something along the lines of should do it. Now when we retry the deployment all should be Gucci.

Final thoughts

You may be wondering why we don’t just use pm2’s watch feature, and it’s a good question. As per their docs..

Watching system does not provide any graceful action, pm2 kills and restarts your application without sending SIGINT

Our solution, when ran in a cluster, takes down each instance one at a time, meaning zero downtime.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store