Set Up CloudBees CodeShip Basic with Node JS, Step-by-Step

Written by: Evan Glazer
7 min read

Recently, I wrote an article on how you can set up CodeShip Basic with Ruby on Rails where I focused on how you can set up a mini continuous integration (CI) pipeline. My goal for this article is to show you how to quickly set up a mini CI pipeline and to expose you to the next items that are essential to a powerful CI build.

When it comes to development - in general - we want to write fast and effective code and deploy without any defects (So we hope =D). Testing the code we write manually and then have to save the sha and then merge the commits...and then hit the deploy button using the sha again. This makes the process to production - long and defective - slow. Using some sort of development pipeline logic can make this process quick and effective communicating with providers creating continuous integration.

Let's say that you're building a company and your engineering team is expanding, and you now feel it is time to automate the process of testing and deploying with constraints built in to trigger deployment. This will make the deployment process less likely to have a bad deploy creating less defect and faster development. So you think about what needs to be done. This article will go step-by-step guiding you to easily set up your Node JS project with CloudBees CodeShip Basic.

Let's start with the essentials

There are a few things you need to do before you dive in.

  • Heroku account. Create an account here.

  • CloudBees CodeShip account. Create an account here.

  • GitHub project. Create an account here.

Follow my Github project: Fork Here

Setting up CloudBees CodeShip in your Node JS project

  What does everything mean?

  • Containers. I think it's safe to say that containers can be a confusing topic to discuss at first. Although the reality is that it makes it easier for developers to know that their software will run wherever deployed. Containers can run all kinds of applications and can be managed on AWS, Heroku, Docker, Kubernetes, etc.

  • Testing. CloudBees CodeShip supports all Node JS-based test frameworks with the main ones being Mocha, Chai, Sinon and Jasmine. In this test project, I will use Jasmine to setup my CI with my development pipeline. There are a few types of testing out there but the main two I feel are important are: Behavioral-Driven-Development and Test-Driven-Development. These two testing types lay out the best practices for writing great tests.

  • Parallel testing. Once you have your testing framework setup, you can begin to separate your tests into groups and call a group into a certain pipeline.

  • Pipelines. Pipelining is a group of apps that share the same codebase. Each app represents stages like development, staging and production, representing a part of the continuous delivery workflow so an environment can easily be promoted.

  • VCPU. This is what we call a virtual central processing unit. Normally a few vCPUs are assigned to every virtual machine within a cloud environment and represent a portion of a physical CPU.

 Basics 1-2-3

To setup CloudBees CodeShip Basic, it is quite easy as all we need to do after signup is connect our GitHub project, install CloudBees CodeShip to our GitHub and conclude by pushing a commit to the repository to trigger our first build.

Click here to install CloudBees Codeship and select the repository you want CloudBees Codeship to communicate (trigger) with.

Then connect your repository that you're adding and push a commit to the repository. You will see the project build in a container.

Setup CloudBees CodeShip services/steps files

codeship-services.yml

Here we're setting up the build to have a Postgres database and deploy to Heroku when we have a successful build.

codeship-node-js-example:
  build: .
  depends_on:
    - postgres
  environment:
    DATABASE_URL: "postgres://example@postgres/examples"
postgres:
  image: healthcheck/postgres:alpine
  environment:
    POSTGRES_USER: user
    POSTGRES_DB: pass
codeship-heroku-deployment:
  image: codeship/heroku-deployment
  encrypted_env_file: deployment.env.encrypted
  volumes:
    - ./:/deploy

codeship-steps.yml

Here we're setting up the testing to be parallel and setting up which environment it will deploy on a successful build.

- type: parallel
  steps:
  - name: tests
    service: codeship-node-js-example
    command: /bin/sh -c "npm test -- --forceExit"
- name: deploy
  tag: staging
  service: codeship-heroku-deployment
  command: codeship_heroku deploy /deploy codeship-node-js-example

DockerFile and Docker Compose

Setting up Docker Files to build the virtual container properly.

docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: npm run dev
    volumes:
    - .:/usr/app/
    - /usr/app/node_modules
    ports:
      - "3000:3000"
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://examplep@postgres/examples
  postgres:
    image: healthcheck/postgres:alpine
    environment:
      POSTGRES_USER: user
      POSTGRES_DB: pass

Dockerfile

FROM node:7.7.2-alpine
WORKDIR /usr/app
RUN apk update && apk add postgresql
COPY package.json .
RUN npm install --quiet
RUN npm install -g jasmine-node
COPY . .

Heroku Deployment

To set up Heroku, we will need to set up a .pub key to communicate with CloudBees CodeShip. Then we will need to configure our deployment information with CloudBees CodeShip by giving it our Heroku API key.

To begin, we will need to build an SSH key locally. To build a .pub (public key) file, we will need to run ssh-keygen -t rsa in the terminal. Then we will need to add the key to Heroku using the Heroku CLI in terminal heroku keys:add [Public Key Path] which is normally defaulted to this location - ~/.ssh/id_rsa.pub. If you do not have Heroku CLI installed on your machine, you can learn how to install it here.

Once we have added the key to Heroku. We will need to get our Heroku API Key.

  • Go to the Heroku Dashboard and [login] (https://dashboard.heroku.com/login).

  • Then go to Account Settings by clicking on your avatar.

  • Scroll down to API Key and click reveal to copy it.

Now that we have the key, it is time to add it to CloudBees CodeShip so we can have our deployment working off of our testing suite or we can create a file called deployment.env.protected to store the Heroku API Key in.

HEROKU_API_KEY=your_api_key_here

Or follow these steps:

  • Go to the CloudBees CodeShip Dashboard to login

  • Your screen should look something similar to the above ^^

  • Next we will need to navigate over to the deploy tab after clicking on our project's Project Settings.

  • Click "Add new deployment pipeline" and pick the deployment branch that you want CloudBees CodeShip to communicate with Heroku with. Now we want to click on Heroku and add our key.

What is my next step after setting this up?

Currently, we have one source repository other developers can work within. Every time we push a commit, it is going to build the container and run unit tests giving us a pass/fail. Then we currently have a way for passed builds to automate deployment over to Heroku. So the question is what else could be added to strengthen the process?

Well, one of the most challenging things to set up with CI is integration testing. Selenium and NightWatch JS are popular in Node JS for integration testing. You can also add forms of stress testing or security vulnerability checks to your project to make sure nothing malicious will break your app.

Wrapping up

Now we have set up the communication for our Node JS project on GitHub to communicate with CloudBees CodeShip and for CloudBees CodeShip to communicate (trigger) builds after each commit. Now if we want to test our project - we can just do git add . git commit --allow-empty -m 'testing Codeship' git push origin controller and then if all the tests pass from the test suite it will deploy in Heroku; otherwise the build will fail.

We have now set up a deployment pipeline that can strengthen your projects for rapid development and faster deploys.

Additional resources

Stay up to date

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