Hi, It's been a long time since I posted last, I've been working on shifting my rails app to React front-end and use Rails for the back-end. I'm also using RSpec to test my codebase.

When I started building rails apps, I use to write code and only maintain my codebase with git, this means no CI/CD no tests and no test coverage. But this time I wanted to learn DevOps and implement it that enabled me to maintain my codebase more efficiently.

Introduction before starting

Travis CI

Travis CI is a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub.

Code Climate

Code Climate provides automated code review for test coverage, maintainability and more so that you can save time and merge with confidence.

Test Coverage

It is defined as a metric that measures the amount of testing performed by a set of test. Example: If you have 4 controllers, you write 3 controllers tests, you get 75% test coverage score. This means how much of your tests are covered. The more test coverage score, the better it is.

Maintainability

It means how easy it is to maintain the system or how easy it is to analyze, change and test the application.

You can find more about various workflow and terms here

So now our workflow will be like this:

  1. We already have a rails app with some code and some tests written.
  2. We push our code to GitHub.
  3. Travis CI makes a note of it and starts building(testing) our project.
  4. When the build is successful, it will send the test coverage statistics to the Code climate.
  5. Also, now say, If someone submits a pull request to your project, this will also run the build in Travis CI, and you(project owner) can see these checks directly in GitHub pull requests page and can approve the pull request if there are no failing tests.

Implementing Travis CI

We'll use rails app with MySQL database and RSpec as testing framework.

Before starting make sure your repository is pushed to GitHub.

First, goto https://travis-ci.com and sign up --> connect your GitHub repo --> Then in the repo lists --> Find your repo --> Slide the enable button. That's it.

Then, make sure that all your tests pass in your local environment. Now, install the travis gem.

# Gemfile
gem 'travis'

Then run, bundle install

Travis CI needs a file called .travis.yml, this will tell Travis details about your app and what steps it needs to take to run the tests. Create a file with .travis.yml in the root of your rails app.

env:
  global:
    - RAILS_ENV=test
addons:
  apt:
    sources:
      - mysql-5.7-trusty
    packages:
      - mysql-server
      - mysql-client
before_install:
  - sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('1234') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;"
  - sudo mysql_upgrade -u root -p1234
  - sudo service mysql restart
language: ruby
script:
  - bundle install --jobs=3 --retry=3
  - bundle exec rake db:create
  - bundle exec rake db:migrate
  - bundle exec rspec
notifications:
  - email: false
branches:
  only: 
    - master

First, we're setting the Environment(ENV) which is global to all commands: RAILS_ENV=test

Then, we're telling Travis to use external service mysql since we've mysql2 as our database. This is done in addons section.

Next, before installing the app, we'll need to change the credentials of MySQL to reflect our config/database.yml file. You can change the root password as above.

Next, we tell Travis to use language ruby.

The script section is where the actual magic happens. It will test our code here.

Travis CI will send emails after every build pass/fail. You can disable this in notifications section.

Travis CI will build all the branches it finds in the repo. I usually disable this feature. Consider a scenario when someone will send a pull request --> They will create a new branch say bug/fixed-navbar --> They will send a pull request --> Now Travis CI will build bug/fixed-navbar branch --> Also it will build the pull request. So it's 2 times. With branches section, we'll tell Travis to build only when we push code to master branch and also this will automatically will build whenever someone submits pull request.

Don't push your code to GitHub yet, next we'll implement Code climate.

Implementing Code Climate

First, sign up at https://codeclimate.com with your GitHub account --> Select your repo --> Enable. This will do some tests, and it'll give you Maintainability stats.

Goto your repo settings in code climate --> Github section --> Click on Setup on Pull request status updates. This will run the test coverage tests in our pull requests.

Again in your repo settings --> Test coverage --> Get the Test reporter ID and save it somewhere.

Now back to your local environment, add the simplecov gem:

# Gemfile
group :test do
  gem 'simplecov'
end

Then, run bundle install. In your spec/rails_helper.rb file, at the top add:

# spec/rails_helper.rb

require 'simplecov'
SimpleCov.start 'rails'

Now, whenever you run bundle exec rspec, this will run the test coverage and include it in the coverage folder in the app's root. You can check the coverage/index.html file for all the stats.

Don't forget to ignore this folder in .gitignore file:

# .gitignore

coverage/

Now, we'll tell the travis CI that whenever we build the app --> Send the test coverage stats to the code climate. The updated .travis.yml file:

env:
  global:
    - CC_TEST_REPORTER_ID=YOUR_TEST_REPORTER_ID
    - RAILS_ENV=test
addons:
  apt:
    sources:
      - mysql-5.7-trusty
    packages:
      - mysql-server
      - mysql-client
before_install:
  - sudo mysql -e "use mysql; update user set authentication_string=PASSWORD('1234') where User='root'; update user set plugin='mysql_native_password';FLUSH PRIVILEGES;"
  - sudo mysql_upgrade -u root -p1234
  - sudo service mysql restart
language: ruby
script:
  - bundle install --jobs=3 --retry=3
  - bundle exec rake db:create
  - bundle exec rake db:migrate
  - bundle exec rspec
notifications:
  - email: false
branches:
  only: 
    - master
before_script:
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter
  - ./cc-test-reporter before-build
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

Make sure to change your TEST_REPORTER_ID in env section.

Now, you can push the code to the GitHub master branch(or create a pull request). This will run the Travis build --> if all the tests pass, it will show success and also will send the test reports to code climate.

The END

CI is nowadays much crucial in the software development sector. I hope you've learned something new and we saw that it's relatively simple to implement Travis CI and Code climate in our project to make workflow more efficient. You can learn more about CI here.