Circle.ci

Circle.ci is the best CI tool I have seen so far. Super easy to get started. It is more similar to TravisCI then others (Jenkins, Drone.io).

I signed up with my GitHub account and it automatically provides a list of your GitHub projects in the 'Add Projects' menu. Just select the project you want, select 'setup project', choose target OS and language, and follow the instructions. It uses yaml and Docker, sweet.

Config

Circle CI infers a lot about your project from the code, so you may not need to customise any settings. If you do, the config.yaml file is where you do it.

There are seven primary sections, representing steps in the build/test/deploy process.

  • [docker, machine, and macos] executor environments
    • machine - Virtual Machine executor and settings e.g. time zone
      -- environment - environment variables e.g foo: bar
    • docker
    • macos
  • checkout - e.g. git clone
  • dependencies - e.g. npm install
  • database - prepare a database for tests. Default include Postgres, MySQL, Redis
  • compile - compile your project
  • test - run your tests
    • override, pre, post
  • deployment -

Each step in the above is run in its own context, so if you want an environment variables persisted you need to set that up in the Settings -> Contexts section. For SSH keys you can add in Project Settings -> Permissions -> SSH keys.

Test

By default these directories are used: test/unit, test/integration, and test/functional. With node I usually have test folders in each module/lib in my app. I would say for unit testing, use **/*.spec.js, for integration put in /test/integration. The test locations can be overridden by:

test:
  minitest_globs:
    - test/integration/**/*.test.js
    - **/*.spec.js

Deployment

If a test is green, the next (optional) step is deployment. The names below (production, staging, dev) relate to the current branch. You can also deploy based on tags.

deployment:
  production:
    branch: production
    commands:
      - ./deploy_prod.sh
  staging:
    branch: master
    commands:
      - ./deploy_staging.sh
  dev:
    branch: dev
    commands:
      - ./deploy_dev.sh

Testing locally

You can install the circle.ci CLI tool using:

curl -o /usr/local/bin/circleci https://circle-downloads.s3.amazonaws.com/releases/build_agent_wrapper/circleci && chmod +x /usr/local/bin/circleci

Then run circleci config validate -c .circleci/config.yaml to validate your config file. Or circleci build to execute your build step.

Example config

First we need to specify the latest version, which is 2.

version: 2
jobs: 
  build:
    docker:
      - image: circleci/node:9.9.0

The first image is the primary image - where you will reference. After which you can specify dependency Docker images, if any. CircleCI maintains a library of pre-built images so you can target them directly.

    working_directory: ~/repo

steps:
  - checkout

  # Download and cache dependencies
  - restore_cache:
      keys:
      - v1-dependencies-{{ checksum "package.json" }}
      # fallback to using the latest cache if no exact match is found
      - v1-dependencies-

  - run: yarn install

  - save_cache:
      paths:
        - node_modules
      key: v1-dependencies-{{ checksum "package.json" }}
    
  # run tests!
  - run: yarn test