travisci privateでheroku deployまで

今回はrailsプロジェクトでテスト実行からdeployまでやってみましょう。

1.
まずテストが上手く動くか確認。

postgreを利用するので以下のようにdatabase.yml.travisと.travis.ymlを用意します。

.travis.yml

language: ruby
rvm:
  - 2.1.3
before_script:
  - psql -c 'create database travis_ci_test;' -U postgres
  - cp config/database.yml.travis config/database.yml
addons:
  postgresql: '9.3'

config/database.yml.travis

test:
  adapter: postgresql
  database: travis_ci_test
  username: postgres

一度pushしてテスト確認。

2.
次に、travis CLIのinstallをして、heroku deploy用の情報を作成します。
http://blog.travis-ci.com/2013-01-14-new-client/

$ gem uninstall travis-cli
$ gem install travis
$ travis login --pro --github-token yourtoken

yourtoken情報は
https://magnum.travis-ci.com/profile/info
Travis.com > Account > Profile
で確認できます。

しかし、なぜかログインできず。

$ travis login --pro

で、仕方なくgithubのusernameとpasswordを入力しました。

$ travis setup heroku
Deploy only from username/reponame? |yes| 
Encrypt API key? |yes|

質問に答えると以下の様にgenerateされます。

deploy:
  provider: heroku
  api_key:
    secure: secure_api_key
  app: app-name
  on:
    repo: username/reponame
  run:
    - 'rake db:migrate'
    - 'rake db:seed'

再度pushしてdeploy確認しました。
しかしここでtravisエラー

The default strategy for Heroku deployments is currently "anvil".
This will be changed to "api" in the near future.
Consider setting it explicitly to "api" or "git".

なんでそんなdefaultの設定になっているんだ(・o・)、仕方なくstrategy optionを追加して完了です。

まとめ

上記1,2をまとめて、用意した設定ファイル以下2つ。

.travis.yml

# 言語の指定
language: ruby
# rvmの指定
rvm:
  - 2.1.3
# rake前に実行
before_script:
  - psql -c 'create database travis_ci_test;' -U postgres
  - cp config/database.yml.travis config/database.yml
# addonの設定
addons:
  postgresql: '9.3'
# deploy用設定
deploy:
  # deploy先指定
  provider: heroku
  # deploy方法指定
  strategy: git
  # heroku api_key指定
  api_key:
    secure: secure_api_key
  # deploy先herokuアプリ名
  app: app-name
  # deploy元を指定
  on:
    repo: username/reponame
  # deploy後に実行
  run:
    - 'rake db:migrate'
    - 'rake db:seed'

config/database.yml.travis

test:
  adapter: postgresql
  database: travis_ci_test
  username: postgres