Configure a Continuous Deployment to Digital Ocean for a Node.js App

- By Charlie Crawford on February 12, 2015

Looking to configure a continuous deployment to Digital Ocean for a Node.js app? Tired of manually trying to configure git hooks to work with you? Is SCP just a little too old fashioned and clunky. Great news! Your search is finally over as today we'll show you how to combine Shippable and Dokku to fit your deployment needs!

Dokku describes itself as a 'docker powered mini-heroku' and it lives up to the claim! With Dokku installed on your droplet, you can interact with it very similarly as to how you would a heroku app. By orchestrating Docker, Buildstep, and Gitreceive  together, Dokku forms a simple, not to mention well functioning, roll your own PaaS. More information on Dokku can be found here.

Setup Dokku on Digital Ocean

While manually installing Dokku is a fairly painless process, why not make your life even easier? Digital Ocean has streamlined the process by providing a droplet image with dokku pre-installed. When creating a new droplet within Digital Ocean, click 'Create Droplet',  select 'One-click apps' in the 'Choose an image' section, and choose 'Dokku 0.6.5 on 14.04'

Dokku on Digital Ocean

Do you prefer to provision your host machine from a different image, or want to setup Dokku on an already running instance? No problem, just follow along with Dokku's official installation instructions, and come back when you're done; the rest of the tutorial is still relevant for you, even if you installed Dokku yourself!

After you droplet has finished being created, visit its IP address, can be found under the Droplet name and should also be emailed to you. Here you will find a web console to finish setting up Dokku. We'll come back to this in a moment. Before going any further on this screen, you need to grab your deployment key from your Shippable account.

After you login to your Shippable account, click on the burger menu icon on the left, and select your subscription that is storing your app. Go to the 'Settings' tab, and under 'Options', find the 'Deployment Key' section. Copy what you see there as shown below. More about the Shippable Deployment key here.

Deployment key in Shippable

Then, paste this key in the Dokku setup console that we accessed earlier, and click 'Finish Setup'.

Dokku setup
 

Your droplet is ready to be deployed using Shippable, but we need to add some additional information to your app. Thankfully, the changes are quick and easy. For those who'd like to follow along, I'll be forking and modifying Heroku's node-js-sample for use with Shippable.

Configure Shippable to automate Deployment

Enable this project (forked repository) within Shippable. Here's instructions to enable a project in Shippable.

Now, only a shippable.yml file needs to be added to your forked repository; the actual application code can be left as is! Here are the contents of my shippable.yml file

language: node_js

node_js:
- 0.10.33

build:
ci:
- echo 'CI running'

on_success:
- if [ "$BRANCH" == "master" ]; then git remote add dokku dokku@138.68.46.6:tutorial; fi
- if [ "$BRANCH" == "master" ]; then git push dokku master; fi

You might have noticed I wrapped my git commands in conditions to check if the current branch is master. This is so my app only gets deployments triggered for pushes to my master branch: it doesn't really make sense to attempt to push to master, if I'm trying to build one of my feature branches! If you're curious to read more about branch based deployments and logic, check out our tutorial posted earlier this month here: specifying deployment targets for different git branches

For those following along, your shippable.yml should look very similar. However, you will need to use your app's IP address/URL, and specify your droplet's name instead. I named my droplet 'tutorial'. The format for the git remote should be:

 

dokku@<droplet-hostname>:<droplet-name>

as you can see above, mine comes out to be:

dokku@138.68.46.6:tutorial

Save and commit your shippable.yml file. This will trigger a CI build on Shippable.

If you setup DNS for your app, this app name section will instead specify the sub-domain at which you'll access your app. More information on app naming and sub-domains can be found in the Dokku docs , but this will suffice for our example.

After making sure that the app builds successfully on Shippable, I can find the URL and the port that the app is running on, by looking in the Console for 'build_on_success' section...

Dokku App URL and port

 ... By visiting that in my browser, I can see my app in action!

App in action through a browser

Verify the automated deployment

Now, whenever I push a commit on my main branch to GitHub, the latest version of my app will be deployed to my droplet! You can now further customize this to the CD pipeline of your dreams!

It's important to note that, with every change and subsequent build, that port number of your app will change. Make sure to check the build logs again to see your updated app.

Make a change to see Shippable automatically update your app:

Open the 'index.js' file in your GitHub repo and modify the file by adding 'I am using Shippable' as shown below:

app.get('/', function(request, response) {
  response.send('Hello World! I am using Shippable')
})

Commit the change and this will automatically trigger a build in Shippable. Once the build is successfully completed, go back to your browser and refresh. You'll see the change reflected.

One last caveat to keep in mind, is that Dokku's buildpacks based on Heroku's standard. This isn't a hard requirement to fulfill though. One way you can fulfill this is via a Procfile that states what command to start your web server with. More on that can be found on Heroku's site. Another option is to specify the command in the scripts section of your package.json file. Here is the snippet containing this information from my package.json file:

"scripts": {
"start": "node index.js"
},

In addition to the documentation listed throughout this post, I have also posted my version of the sample app on github. If you have any further questions, please let us know in the comments below. Enjoy!


Try Shippable

Topics: continuous deployment (CD), how-to, nodejs, external service integration