Bryan Lee
Bryan Lee

Musings about growth, software engineering, and tech.

Ruby on Rails

CircleCI 2.0 and Rails Gotchas

CircleCI was a great automation timesaver previously when I worked with Opswork. It ran all our tests and deployed to Opsworks after all tests turned green.

CircleCI released 2.0 earlier this year which adds docker support and allows much more flexibility in controlling how your test environment is built. The official guides are pretty easy to follow but I did encounter a few gotchas not mentioned in the official guides:

Postgres Integration

I use Postgres as my database and added these environment variables and docker images:

- image: circleci/ruby:2.4.1-node-browsers
      PGUSER: postgres
      RAILS_ENV: test
- image: circleci/postgres:9.4
      POSTGRES_USER: postgres
      POSTGRES_DB: snupped_test

Webpacker and Yarn

Use webpacker and yarn? You might also want to add this:

- restore_cache:
      name: Restore yarn cache
      key: snuppedcp-yarn-{{ checksum "yarn.lock" }}

- run:
      name: Install yarn
      command: yarn install

- save_cache:
      name: Store yarn cache
      key: snuppedcp-yarn-{{ checksum "yarn.lock" }}
      - ~/.yarn-cache

Rspec Junit Formatter

You’ll notice something in the example test runner task:

- type: shell
  command: |
    bundle exec rspec --profile 10 \
                    --format RspecJunitFormatter \
                    --out test_results/rspec.xml \
                    --format progress \
                      $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)

When you deploy, you’ll find that the test fails because of RspecJunitFormatter. The command is trying to format your test output as Junit, which is a format Jenkins (and CircleCI) reads. This problem is easy to solve with a gem.

group :test do
  gem 'rspec_junit_formatter'

Install Fails For Capybara-Webkit

Another problem I ran into, where the cause is due to missing dependencies. To solve, add this task to your config.yml:

- checkout

- run:
      name: Install capybara-webkit
      command: |
        sudo apt-get update
        sudo apt-get install -y software-properties-common
        sudo apt install -y gcc g++ make qt5-default libqt5webkit5-dev ruby-dev zlib1g-dev
    comments powered by Disqus