LoginSignup
16
12

More than 5 years have passed since last update.

Rails アプリケーションに Travis CI を設定する

Last updated at Posted at 2016-06-21

概要

Rails のアプリケーションを Travis CI でテストしてみたかったのでやってみました。
Slack への通知方法もまとめました。
今回は Rails 標準のテスティングフレームワークを使ったものが対象になります。

準備すること

  • Rails アプリ
  • GitHub のアカウント

※ Slackへの通知を行いたい場合は Slack アカウントを用意して下さい

Travis CI の設定手順

  1. Github アカウントを使い Travis CI にログインする
  2. 対象のリポジトリを選択する
  3. リポジトリのルートディレクトリに以下のファイルを作成する
.travis.yml
language: ruby
rvm: <ruby-version>
services: postgresql
bundler_args: "--without development --deployment -j4"
cache: bundler
before_script:
  - cp config/database.travis.yml config/database.yml
  - bundle exec rake db:create
  - bundle exec rake db:migrate
script: bundle exec rake test

db/schema.rb が存在する場合は下記のように設定することも可能です。

.travis.yml
before_script:
  - cp config/database.travis.yml config/database.yml
  - bundle exec rake db:setup

また config ディレクトリに以下のファイルを作成

config/database.travis.yml
test:
  adapter: postgresql
  database: travis_ci_test
  username: postgres

変更を GitHub に push すると CI が動きます。

Slackへの通知手順

この設定を行うと、 Slack のチャンネルに CI の結果を通知できます。

  1. Travis CI のコマンドラインツールをインストール
$ gem install travis
  1. Slack を開き左上のチーム名をクリックして Customize slack を選択
  2. 左上の Menu から Configure Apps を選択
  3. Travis CI を追加し Add Configuration を選択
  4. 任意のチャンネルを設定し、 token を控える
  5. リポジトリのルートディレクトリで以下のコマンドを実行する
$ travis encrypt "<account-name>:<token>" --add notifications.slack

push して Slack に通知されるか確認してください。

16
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
12