Continuous Integration and Deployment for NodeJS Applications

Written by: Clemens Helm

https://fast.wistia.com/embed/medias/yhnd2u1b7a.jsonphttps://fast.wistia.com/assets/external/E-v1.js

This is the 20th Testing Tuesday episode. Every week we will share our insights and opinions on the software testing space. Drop by every Tuesday to learn more! Last week we started testing node.js applications with Jasmine.


How to deploy a node.js app to Heroku

In this screencast we'll deploy a very simple node.js web application to Heroku continuously with the help of the Codeship. You also may want to check out our Heroku Deployment article on our documentation.

Disclaimer: We show a lot of the Codeship in this screencast – that's because we use it and because we build it. There are certainly other ways to continuously deploy node.js applications. This is how we do it.

By Continuous Deployment we mean that every change to our application gets deployed immediately, if our application still works. To prove that our application still works, we've got tests in Jasmine, a JavaScript testing framework. Want to know more about Jasmine? There's more on it in this Testing Tuesdays episode.

After we created a Heroku application and set up a Codeship project, the Codeship will take care of the rest: Whenever we push a new version of our app to GitHub, it will pick it up, run the Jasmine tests for us and deploy the application if everything works.

This way we can safely push every change to our application. If our tests succeed, our changes go live. Otherwise the build will fail and we'll have to fix the bugs.

Special thanks go to Modulus for their excellent blog post "An absolute beginner's guide to node.js". We used the post's example web app in this screencast.

Up next Testing Tuesday: Testing synchronous and asynchronous JavaScript callbacks with QUnit

Next week will be the last Testing Tuesday episode for this season. We'll talk about QUnit and how it compares to Jasmine. If you've got any questions or suggestions, please leave a comment!

Further info:

Transcript

TT20: Continuous deployment for node.js applications

Intro

Ahoy and welcome! My name is Clemens Helm and this is Codeship Testing Tuesday episode 20. Recently we've talked a lot about testing JavaScript. Today we want to apply our knowledge to set up continuous deployment for a node.js application.

Screencast

We've got a very simple web application that we want to deploy continuously. By continuous deployment we mean that every change to our application gets immediately deployed, if our application still works. To prove that our application still works, we write tests in Jasmine, a JavaScript testing framework. If you want to learn more about Jasmine, watch the last few Testing Tuesdays. I'll link to them in the further info section.

Our application is a simple web app that greets visitors depending on the time of the day.

node app.js

So now it greets me with "Good afternoon!"

We've got an app.js file that contains the http server. I took this example from the "Absolute beginner's guide to Node.js" by Brandon Cannaday from Modulus. If you're new to Node.js, you should definitely check it out!

I extended the example with a greeting function that returns a different greeting string depending on the time you pass to it. I've got a Jasmine spec file that tests the function with several times of the day.

When we run Jasmine, it tells us that all our specs work and everything's alright.

So let's get our app online using continuous deployment. We're gonna deploy our app to Heroku using the Codeship. Our Code is stored in a Git repository on GitHub. Let's sign into the Codeship with GitHub. I choose GitHub as my repo provider, pick my repository and select "node.js" as my technology.

We are asked for some setup and test commands. The only setup we need is to install jasmine-node, so we enter

npm install jasmine-node -g

To run our tests we want to run jasmine-node on the spec directory, so our test command would be

jasmine-node spec

Ok, let's give this a try. To run our first build, we just need to push something to our Git repository. Fortunately I haven't pushed the application yet, so I will do this now:

git push -u origin master

Now our build starts running on the Codeship. And after a few seconds, it succeeded. But we also want to deploy our application everytime our build succeeds.

Let's create a heroku app "greeter". On the Codeship, all we have to do now is add Heroku to our deployment configuration and enter the name of our application and our API-Key. Mine is already in there.

Now let's go back to our project and run the build again. After running the tests, the Codeship also runs the deployment for us.

Done. Let's check out our app online: http://greeter.herokuapp.com/. It worked!

But what if we break something in our application? Let's give this a try:

We remove the else branch of our greeting. So now let's commit

git commit -am "Remove else branch"

and push our new version.

git push

On the Codeship our build already started running. But now our Jasmine specs fail. The build stopped right where it failed, so our malicious code didn't get deployed.

This way we can safely push every change to our application. If our tests succeed, our changes go live. Otherwise the build will fail and we'll have to fix the bugs.

Outro

I'm a big fan of continuous deployment, and I hope you like it as well! Next week will be the last Testing Tuesday episode for this season. We'll talk about QUnit and how it compares to Jasmine. Have a beautiful week and even when the sun ain't shining: Always stay shipping.

Stay up to date

We'll never share your email address and you can opt out at any time, we promise.