CircleCI + Rubocop

Maxim Abramchuk
3 min readJun 15, 2015

--

Some time ago I decided to take control of the code style at my project at work, because it was a mess of really different code styles and it looked very bad. So, I was checking every pull request for Ruby, SCSS, git commit messages style according to well-known style guides and leaving comments on Github about it.

But after some weeks of doing this stuff I was really exhausted. So, when I heard that CircleCI became free for private Github repos I decided to set it up instead of Jenkins, which we had before.

I also had an idea of automating my work on code style violations checking. As I know there is a HoundCI service which costs 12$ for one private Github repo, but the idea of doing the same functionality by combining two different free tools impressed me a lot.

So, I decided to connect CircleCI and Rubocop at first. After spending some time googling stuff related to these 2 things I found a ruby gem called pronto, which provides you with the ability of doing this.

So, it was pretty easy and clear to do this from my local computer. You just need to install this gem, specify a Github pull request ID and your Github access token to be able to post comments.

But my idea was to setup this stuff on the remote CircleCI server for every time it builds my Rails application. So, I decided to write a Bash script which will be executed after every CircleCI build.

So, I need only 2 things:

  1. ID of the pull request which is building.
  2. Github access token.

Let’s start from the first point.

Every time CircleCI builds your app it automatically sets a bunch of environment variables, which contain all the stuff like your Github username, build number, Github reponame, git branch and etc.

But it seems that CircleCI doesn’t really have an ID of the pull request which it is building, like TravisCI does in it’s TRAVIS_PULL_REQUEST variable. After some time I found a variable called CI_PULL_REQUEST, which contains a PR url like https://github.com/rails/rails/pull/20554. So, I wrote a regexp to get the PR number from the URL. It looked like:

So, I got my PULL_REQUEST_ID, GITHUB_ACCESS_TOKEN, my Bash script was working ok locally, but after trying to build the first PR it caused an error at the remote machine. After some hours of debugging the pronto gem, debugging my script at the remote server via SSH, I figured out that CircleCI does a shallow clone of my repo, which contains only a part of my commits. So, I have to override the CircleCI checkout process and specify a depth of fetching to 1 million commits. After that everything works like a charm.

So, here is a step-by-step tutorial how to add code style violation checking to your CircleCI server.

Tutorial:

Gist with all the necessary code is here.

  1. Add gem pronto to your Gemfile.
  2. Add other gems according to your needs (ruby, scss, coffee, js and etc).

https://github.com/mmozuras/pronto#runners

Here you can find list of other gems, such as pronto-rubocop, pronto-scss, pronto-coffeelint to be able to setup the linters you want.

3. Add to your .circle.yml following lines:

4. Create an executable file called cisetup (or whatever you wrote in .circle.yml) with following lines in it:

5. The game begins.

If you are facing some trouble trying to set up this feature you can easily contact me :) Good luck!

--

--

Maxim Abramchuk

Co-Founder and CTO at @getstorylinecom, @boostvc Tribe X, Voice-powered Cyborg 🤖