Skip to content

Node

This guide provides a walkthrough of .razorops.yaml for a typical Node.js based application. Our CI process is going to consist of following steps -

  • Install dependencies (npm install or npm ci)
  • Caching dependencies for faster builds
  • Execute test suite (npm test)

Choosing Node versions

Usually the first step before writing CI spec is to decide the language runtime and support for the version compatible with your application. We support most active versions of Node. You have the following options if you want to pin-down a version of Node -

  • For Docker runner environment, you can specify a container image (razorci/node or official ruby) with tag.
  • For Linux-VM environment, you can change the version (or install new version if not available) using nvm.

Selecting Node version on Linux-VM environment

In Linux-VM build environment, we use nvm to manage the various Ruby versions. You can find currently supported versions in toolbox reference. You can also install a different version using nvm install <version> command if it's not supported by us.

Here are few commands to use, switch and install a version -

    # part of .razorops.yaml file
    steps:
    - commands:
      ## NOTE: source NVM script, needs to be done before using nvm command
      - source  $NVM_DIR/nvm.sh

      # list installed versions

      - nvm ls
      - node --version

      - nvm use v14
      - node --version

      # install node 16
      - nvm ls-remote ## list stable versions
      - nvm install v16

      - nvm use v16
      - node --version
      # v16.14.0

Installing dependencies

You can use razorci/node community docker image managed by us:

tasks:
  build-deps:
    runner: razorci/node # or node from official library/_node
    steps:
    - checkout
    - run: npm install --package-lock-only
tasks:
  build-deps:
    runner: razorci/node
    steps:
    - checkout
    - run: yarn ci

Read more how to use this image on dockerhub docs.

Tip

You can further speedup the workflow by caching npm dependencies so that every subsequent build then performs just incremental changes on top of that during npm install which may lead to even faster builds.

Caching dependencies

Downloading node third-party packages/dependencies from the internet can be time-consuming, you can greatly speed up this process by caching it using an unique key. In future workflow runs, the cache will be restored so that dependencies don't need to be downloaded from remote (Yarn/Npm etc) repositories. For more information, see Caching dependencies.

tasks:
  build-deps:
    runner: razorci/node
    steps:
    - checkout
    # try to restore the cache by an unique key
    - cache/pull: npm-cache-{{ checksum "package-lock.json" }}

    - run: npm install --package-lock-only
    ...

    # update the cache with an unique key
    - cache/push:
        key: npm-cache-{{ checksum "package-lock.json" }}
        paths:
        - node_modules
    ...

Execute test suite

This example shows two tasks running in parallel to execute unit-tests and e2e tests.

# Assuming you have `lint` and `test:e2e` defined in package.json

global:
  variables:
  - NODE_ENV=test
  runner: razorci/node

tasks:
  run-tests:
    steps:
    - checkout
    - commands:
      - npm run lint
      - npm test

  e2e-test:
    steps:
    - checkout
    - run: npm run test:e2e

You can additionally provide environment variables and provide service dependencies to run your test suite.