Setting up a Node.js production environment and the continuous deployment pipeline for that is not a trivial task to do. Also, if you don't have enough DevOps personnel, it may be impossible to achieve.
In this article, I am going to show you how you can easily setup a build pipeline, a staging and a production environment. The process shouldn't take more than a couple of hours.
What We Are Going to Build
By the end of this tutorial, you will be able to set up your production Node.js environment. This will include a continuous delivery (CD) tool, a staging and a production environment where successful builds will be automatically deployed to the staging environment.
Before deploying to production, human interaction is needed to verify if everything is ok.
For this setup, we are going to use the following products:
Codeship is a continuous delivery tool that runs your tests and deploys your application to different cloud providers - like Heroku.
It makes configuring of pipelines really easy, you can think of it like Jenkins, but with a more intuitive UI - but also a more limited feature set.
Heroku is a platform as a service that runs your applications, whether it is written in PHP, Ruby or Node.js.
Heroku has a friendly pricing model; you can easily extend its functionality with third-party add-ons, like databases. They also provide lots of security features out of the box. Long story short, while you don't have / can't afford someone dealing with your infrastructure full-time, they got your back.
CloudFlare protects and accelerates your web applications by providing a CDN (Content Delivery Network), DNS, optimized delivery of web content (like minifying JS/CSS content). After you set up CloudFlare with your domain, they also provide SSL certificates for you.
Putting it Together - Your Production Node.js Environment
Get a Heroku, Codeship, GitHub and CloudFlare account.
Step 1: Integrate Codeship with GitHub
In Codeship, look for the Create new project button and select your project from GitHub:
On the next screen you have to set up your test commands:
Once you save these changes, then on every commit your tests will be run.
Step 2: Integrate Codeship with Heroku
If the build goes well, we want our application to be deployed to the staging environment right away. To do so we have to integrate Codeship with Heroku.
First, you have to create a new application on Heroku for both your staging and your production application. One thing you should pay attention to is the naming of your application. Make sure you follow some kind of a pattern, like prefix every application of yours with the name of your company, followed by the application name, then postfixed with the environment.
If your company is named Acme, Inc. and your application/service is called Users API you would call your staging application
acme-users-staging and your production
To do so just click Project Settings > Deployment and when it asks for the branch just enter master. In the next screen just select Heroku and setup your API keys.
Once you are done with it, every successful build on the master will be pushed to Heroku.
Step 3: Adding Heroku pipelines
Heroku pipeline is a feature from Heroku labs that makes releases applications easier. What will happen in the background is that Heroku builds a slug from the
staging environment to enable consistent deployments between
On how to set up pipelines, please follow the instructions to set this up: https://devcenter.heroku.com/articles/labs-pipelines
Step 4: Integrate CloudFlare
Integrating CloudFlare is pretty straightforward - after signup you simply have to enter your domain name, from where CloudFlare will read the current records. After that the only thing that you have to do is change your name servers to the CloudFlare ones.
The most important items that you should check here:
- page rules to enforce https
Step 5: Deploy to Production
Once we know that everything is fine on staging, then nothing stops us from deploying our new application to production. You can easily do so with
heroku:pipelines in your terminal.
heroku pipeline:promote --app acme-users-staging
That's it, you just deployed to production, congratulations! Also please note, that this was the only human interaction needed for pushing your application to production.
Evolve to the Next Level
Once you start paying too much for these services, then you should consider moving your infrastructure to other providers, like AWS to reduce costs. But before doing so make sure that you have all the necessary skills and availability in-house to do so.