LoginSignup
3
2

More than 5 years have passed since last update.

Rails 5でTravis CIを利用する

Posted at

Travis CIを利用するには設定ファイルを書く必要がありますが、Rails 5のアプリケーション用にいい感じに書いている記事を見つけられなかったので備忘録として残しておきます。

各種バージョン

  • Rails: 5.1.4
  • Ruby: 2.5.0
  • Bundler: 1.16.1

設定ファイルを書く

こんな感じで書きました。

.travis.yml
language: ruby

services:
  - postgresql

bundler_args: "--without development --jobs=3"

cache: bundler

before_script:
  - psql -c 'create database travis_ci_test;' -U postgres
  - cp config/database.yml.travis config/database.yml

script:
  - bin/rails db:migrate RAILS_ENV=test
  - bin/rails test

notifications:
  email: false

rvm

Building a Ruby Project # Using-.ruby-version - Travis CI

省略すると.ruby-versionに書いてあるバージョンが設定されます。

services

Setting up Databases - Travis CI

Travis CI内で利用するDBを指定します。
別途、DBユーザーの作成やdatabase.ymlの設定等が必要になります。
database.ymlの設定はドキュメントに倣って以下のような設定ファイルを用意し、ビルド時に置き換えるようにしました。

database.yml.travis
test:
  adapter: postgresql
  database: travis_ci_test

bundler_args

Building a Ruby Project # Speeding-up-your-build-by-excluding-non-essential-dependencies - Travis CI

bundle installのオプションを指定できます。
developmentでのみ利用しているGemは不要だったのでwithoutオプションで除外しました。
jobsオプションで並列処理をさせて高速化させます。

cache

Caching Dependencies and Directories - Travis CI

依存パッケージのキャッシュをするために、利用しているパッケージ管理ツールを指定します。

利用しているパッケージ管理ツールがBundlerで、インストール先のパスがvendor/bundleならbundlerと書くだけで大丈夫です。

before_script

Customizing the Build # The-Build-Lifecycle - Travis CI

scriptの前に何か実行したいときに利用します。
ここにはPostgreSQLのセットアップを入れています。

script

Customizing the Build - Travis CI

ビルドスクリプトを実行するのに利用します。
本命であるrailsのコマンドの実行はここでやっています。

notifications

Configuring Build Notifications - Travis CI

Travis CIの結果を通知するのに利用します。
デフォルトの設定だと失敗したときや前回の失敗から成功した際に通知されてしまうので、通知が不要なら切ってしまうのがよいでしょう。

終わりに

今回はRails Tutorialで作ったRails5のアプリケーション用に設定しました。この規模のアプリケーションなら上記のような設定で簡単にセットアップできるのでいいですね。

設定のためにPull Requestも作ったので参考までに置いておきます。

Travis CIを導入する by ku00 · Pull Request #10 · ku00/sample_app_5

3
2
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
3
2