Over a million developers have joined DZone.

Continuous Deployment for Node.js Apps from Bitbucket to Heroku

DZone's Guide to

Continuous Deployment for Node.js Apps from Bitbucket to Heroku

· DevOps Zone
Free Resource

“Automated Testing: The Glue That Holds DevOps Together” to learn about the key role automated testing plays in a DevOps workflow, brought to you in partnership with Sauce Labs.

In this blog post we’re going to deploy a Node.js application from a Bitbucket repository to Heroku using Codeship.

We’ve set up a simple Node.js application called codefish which contains some Jasmine specs. We’ll use screenshots of this application in this blog post. If you haven’t got your own project to set up but you want to follow along on your computer, just fork the repository.

Set up Continuous Integration and Deployment for your node.js projects. The Codeship supports GitHub and BitBucket projects and about any techstack you can imagine.

codefish-node on Bitbucket

Together, we’re going to deploy this application to Heroku using Codeship.

Codeship Landing Page

First, sign in to Codeship with Bitbucket. Codeship needs access to your Bitbucket repositories to be able to set them up. Let’s allow access.

Bitbucket Access

We’re back at Codeship. Now let’s create your first project.

Let's set up our first project on Codeship

The first step of your project setup is to select Bitbucket as your repository provider.

Select your repository provider

In the list of your Bitbucket repositories

Search for your repository in the list

search for the repository you want to set up and select it. In my case I search for “codefish”.

Find your repository in the filtered list

Now your repository is connected and you can set up your test commands:

Set up your test commands

Codefish is a Node.js application. Therefore let’s choose “Node.js” as your technology. This prepopulates the setup commands and the test commands for you.

Select Node.js as your technology

Codeship suggests Node.js 0.10.25, but you can use whatever Node version you like. To find out the Node version of your project, type node -v in the Terminal.

Type ruby -v and find out your Ruby version

Just copy your Node version into the nvm install and nvm use statements.

If you want to run Jasmine specs, please install jasmine-node globally additionally to your node modules. In this case just add npm install -g jasmine-node after npm install.

You don’t need npm test for your Jasmine specs, so you can comment it out by adding a hash key before the command. Instead, add jasmine-node spec to the test commands to run your Jasmine specs.

[img src="https://raw.githubusercontent.com/codeship/screencast-storyboards/node-bitbucket-heroku/screenshots/node/test-commands.png" alt="Comment the test command and add jasmine-node spec">

Now let's finish your setup and go to the dashboard.

Finish your setup. You are on the Dashboard now

To start your first build, you need to add a push hook to your Bitbucket repository. Copy the hook url and follow the link to the service hook settings of your repository. Add a "POST" hook there,

Bitbucket select POST hook

paste the hook url

Bitbucket paste hook URL

and save the hook.

Bitbucket hook URL

You can trigger a build for your application by pushing to your repository. Let's add the the Codeship status image to the README file. I use markdown syntax to insert the image.

Copy the code for the Codeship status badge to your README file

Now commit and push this change.

Commit and push your change

This triggered a new build on Codeship.

A new build got triggered on Codeship

You can access the build details by clicking the arrow on the right. Here you can follow the running build.

Click on the arrow to the right to access the build details

A few seconds later your build succeeded! Great!

Look at all the commands that are running

You see all the commands that were run. After a few initial preparation commands Codeship ran the commands that you specified a few moments ago.

You can inspect the output of a single command by clicking on it. For the codefish application, we can see that two Jasmine specs were run.

Look at the log of a single command by clicking on it

You've already pushed to your repository, watched your build log and got a green build. So you can finish the assistant at the top.

Finish the setup wizard by clicking on the click to finish button

Now let's deploy your application to Heroku. Go to your project settings by clicking on the settings icon in the projects dropdown.

Go to your project settings by clicking on the settings icon in the projects dropdown

You are on the Testing Setup screen now

Then navigate to the "Deployment" section.

You are on the Deployment Setup screen now

As we want to deploy to Heroku we click on the "Heroku" button.

Click on the Heroku button

Now you are asked to enter the name of your Heroku application and your API key. If you haven't already, now is the time to go to Heroku and create an application.

You are on the Heroku page now

I named my application "codefish-app", but please choose whatever name you like

Create your app and name it codefish-app

and create your app.

You successfully created your Heroku App

Back at your deployment configuration on Codeship enter the application name.

Back on Codeship we enter the application name codefish-app

To retrieve your Heroku API key, follow the link to your Heroku account and click "Show API key".

Click on the link to get your Heroku API key

Copy it and insert it into your deployment configuration at Codeship.

Copy and paste the Heroku API key to Codeship

Now save your deployment by clicking on the green checkmark on the right.

Save your deployment configuration by clicking on the green checkmark

From now on Codeship will deploy your application to Heroku everytime you push to your Bitbucket repository. Let's go back to our Dashboard.

Let's push a change and see if it gets deployed. Change something in your application first,

Add a new paragraph you can then commit and push

then commit and push the change.

Commit and push the change

And immediately another build will start running on Codeship.

Go back to the project overview to see a new running build

After the commands we already know from your first build, your application also gets deployed to Heroku now.

After some initial commands were run your application gets deployed

And about 2 minutes later your application is online.

After about 2 minutes your application is online

When you open the URL of your Heroku app now, your deployed application appears. You can find mine on codefish-app.herokuapp.com.

Have a look at the app you just deployed

If you need help with setting up your own application, please use the support link in the top-right corner or please tweet us @codeship!

If you need help please click the support link in the top-right corner or tweet us @codeship

Learn about the importance of automated testing as part of a healthy DevOps practice, brought to you in partnership with Sauce Labs.


Published at DZone with permission of Florian Motlik, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}