Simple CircleCI Config for Phoenix Projects

by Vikram Ramakrishnan

CircleCI is a really nice tool that lets you to continuously run your test suite when you open up a pull request or push up a new branch. It allows you to be held accountable for a passing test suite before merging in your latest changes. Hooking your app up with CircleCI is painless. You simply need to login, go to your dashboard, click on “Add Projects” and choose the repo you want CircleCI to watch. One issue, however, is that CircleCI does not yet provide preconfigured environments for Phoenix projects so we’ll have to install and configure our environment manually, which we cover here.

At the root of your repo, you need a circle.yml file, which tells your CircleCI Linux container what it needs to do in order to run your test suite. Here’s one of our circle.yml files, which we used on a project recently:

machine:
environment:
PATH: "$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH"
services:
- redis
dependencies:
cache_directories:
- ~/.asdf
pre:
- if ! asdf | grep version; then git clone https://github.com/HashNuke/asdf.git ~/.asdf; fi
- if ! asdf plugin-list | grep erlang; then asdf plugin-add erlang https://github.com/HashNuke/asdf-erlang.git; fi
- if ! asdf plugin-list | grep elixir; then asdf plugin-add elixir https://github.com/HashNuke/asdf-elixir.git; fi
- erlang_version=$(awk '/erlang/ { print $2 }' .tool-versions) && asdf install erlang ${erlang_version}
- elixir_version=$(awk '/elixir/ { print $2 }' .tool-versions) && asdf install elixir ${elixir_version}
- yes | mix deps.get
- yes | mix local.rebar
test:
override:
- mix test

High level, we set the machine’s environment and install the required dependencies to run the test suite.

Btw, what is thisasdfmentioned a few times in the config? It’s a great package manager written by HashNuke, which I’ve found to be very easy to use. We can use it to manage which versions of Elixir and Erlang our project requires with a .tool-versions file at the root of the app repo:

erlang 19.1
elixir 1.3.4

Here’s what we’re doing step by step incircle.yml:

  1. We update our PATH for asdf, so once we install Elixir, we can successfully use mix.
  2. CircleCI also supports other databases and services, like Redis, which we can add to the services block of the file.
  3. Next we cache the asdf directory so that we don’t have to reinstall Elixir and Erlang between builds, which significantly speeds up our build process.
  4. In the pre block, which houses everything we want to run before CircleCI’s inferred commands, we check if asdf, asdf-erlang, and asdf-elixir exist and only install them if they don’t. Then we retrieve all our Phoenix app’s dependencies.
  5. Finally, we run our app’s test suite.

Hopefully CircleCI will support Elixir and Phoenix soon, but for the time being, that circle.yml file should be enough to get you started. You can read more about customizing your circle.yml file here on their site.

Interested in discussing custom software needs more broadly? Drop me a line at vikram@quantlayer.com — I would love to chat with you.