Bryan Lee

CircleCI 2.0 and Rails Gotchas

· Web Dev

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:

docker:
- image: circleci/ruby:2.4.1-node-browsers
environment:
PGHOST: 127.0.0.1
PGUSER: postgres
RAILS_ENV: test
- image: circleci/postgres:9.4
environment:
POSTGRES_USER: postgres
POSTGRES_DB: snupped_test
POSTGRES_PASSWORD: ""

Webpacker and Yarn

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

steps:
- restore_cache:
name: Restore yarn cache
key: snuppedcp-yarn-

- run:
name: Install yarn
command: yarn install

- save_cache:
name: Store yarn cache
key: snuppedcp-yarn-
paths:
- ~/.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.

# GEMFILE
group :test do
gem 'rspec_junit_formatter'
end

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:

steps:
- 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