Codementor Events

Integrating Travis CI in your Github project

Published Jan 12, 2017Last updated Jan 18, 2017
Integrating Travis CI in your Github project

Travis CI is an open-source hosted, distributed, and continuous integration service used to build and test projects hosted at GitHub.

Continuous Integration (CI)

Continuous Integration is a process of running all unit tests on the latest code which is submitted to check if the newly added components have not broken any old functionality. It is very important from testing and deployment perspectives. It prevents the problems of after-deployment rollbacks. They are time-taking and reduce the reliability of products.

Other options

You can use [Jenkins](https://www.codementor.io/slavko/tutorials/setup-configure-jenkins-recommended-plugins-du107z6nr ''Setup & Configure Jenkins for Your Team") and many other continuous integration servers for this purpose but for using these, you will have to set them up on a server and maintain this server using your dedicated dev-ops engineer that is, if you have got one. If you can achieve this, they provide you with a lot of control and flexibility.

Why Travis CI

Let's say you don't want to hire a dedicated dev-ops engineer or are in no need to dedicate a server to CI but you want to use CI as it is very important for your product. Travis CI comes to your help here. You don't have to maintain your own server or set up travis anywhere. Just include a few files in your repository for Travis to read and you are done.

Travis has support for many languages and platforms. I use it for my Swift Cocoapod libraries, too.

In this tutorial, I am providing steps to integrate Travis CI into your public Github repository.

Steps to Integrate Ruby on Rails Github public repo with Travis CI.

  • Enable Travis from your repository settings → Integration and services → services
  • Go to Travis console from the link there. If you don’t have a Travis account, create one, and it will list all your repositories.
  • Enable the repository you wish to integrate by flicking the switch next to it.
  • Now, create a .travis.yml file similar to this:
language: ruby
rvm:
  - "2.1.3"
env:
  - DB=postgresql
script:
  - RAILS_ENV=test bundle exec rake db:create db:migrate --trace
  - bundle exec rspec test/
before_script:
  - cp config/database.travis.yml config/database.yml
  - mysql -e 'create database mydb_test'
bundler_args: --binstubs=./bundler_stubs

  • The file above file should be added to your repo's root directory that is, top level.
  • Add a config/database.travis.yml
default: &default
  adapter: postgresql
test:
  <<: *default
  database: mydb_test
  username: travis
  encoding: utf8

Now push the changes and let it create the first build.

Wrapping up

Now, every time you push changes to your repository's master branch (you can specify a different branch through settings in Travis repository settings), your unit tests will be automatically run and any failures will be mailed to your Github attached email account.

Discover and read more posts from Amrata Baghel
get started
post comments2Replies
pavan kumar Dharmavarapu
7 years ago

Hi Amrita I am new to ruby on rails I am trying to install a gem from gem.org but not able to install can you please tell me how can I install this in my app and use it like a gem please explain it to me

Jānis
8 years ago

Hi,

Does this before_script part really need to contain mysql command call if we are using postgres in earlier setup?

There could be probably some typo here in article, as in documentation (https://docs.travis-ci.com/…) I see before_script containing command psql -c 'create database travis_ci_test;' -U postgres where travis_ci_test DB name could be probably set to mydb_test for this article described case.