TT20: Continuous deployment for node.js applications
Our application is a simple web app that greets visitors depending on the time of the day.
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
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.
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.
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.