Ruby on Rails(楽天API) → TravisCI → heroku アプリ制作
enPiT のビジネスアプリケーション演習のまとめを書きます٩( º ◡ º )و イエイ
環境構築からherokuへのデプロイまでの流れです。
仮想環境
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
VirtualBox:4.2.18
Vagrant:1.2.7
Ubuntu:12.04.3 LTS
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
開発環境
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
Ruby:ruby 2.0.0p247
Raisl:Rails 4.0.0
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
まずは仮想環境から作ります!
今回はVirtualBoxを使い、Vargrantで簡単に環境構築を行いました。
Virtual Box
https://www.virtualbox.org/wiki/Downloads
Vargrant
http://downloads.vagrantup.com/
インストールが完了したら、早速vargrantを使い環境構築をします。
今回はboxファイルが用意されていたため、それを貰うことに。
$ vagrant box add enpit ~/Desktop/enpit_package.box Downloading or copying the box... Extracting box...te: 41.7M/s, Estimated time remaining: 0:00:01) The box you're attempting to add already exists: Name: enpit Provider: virtualbox
約3分程度で完了。ローカルで入れてるから早いね。
$ vagrant init enpit A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. $ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'enpit'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Fixed port collision for 22 => 2222. Now on port 2200. [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2200 (adapter 1) [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Mounting shared folders... [default] -- /vagrant
これで仮想OSの起動はOKです!!
sshで入ります。
$ vagrant ssh Welcome to Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-23-generic-pae i686) * Documentation: https://help.ubuntu.com/ Welcome to your Vagrant-built virtual machine. Last login: Sun Sep 1 04:01:07 2013 from 10.0.2.2
vagrantを使うことにすでに環境構築されているOS使うことができるのです。
早速環境を見てみましょう。
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 12.04.3 LTS Release: 12.04 Codename: precise $ rbenv -v rbenv 0.4.0 $ ruby -v ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux] $ rails -v Rails 4.0.0
素晴らしい!!!
vargrantのいいところはこれだけではありません。
なんと、/vagrant ではホストOS側と繋がっているのです (´・∀・`) ヘー
/vagrant に保存したファイルはホスト側(Mac)から開くことができる!
今回は /vagrant/… で作業を行います。
$ mkdir /vagrant/enpit $ cd /vagrant/enpit/
今回はRuby on Railsを使うため、3000番ポートにアクセスを行います。
ホストOS側からlocalhost:3000にアクセスして、ゲストOS側の3000番ポートにアクセスができるようにしましょう。
$ emacs /vagrant/Vagrantfile ----コメントを外し、3000に変更する------- # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. config.vm.network :forwarded_port, guest: 3000, host: 3000 $ logout $ vagrant reload … [default] -- 3000 => 3000 (adapter 1) … $ vagrant ssh
デフォルトでは日本語が使えないようです。
なので、日本語の設定もしちゃいます。
$ sudo locale-gen ja_JP.UTF-8 Generating locales... ja_JP.UTF-8... done Generation complete. $ sudo update-locale LANG=ja_JP.UTF-8 $ sudo update-locale LC_ALL=ja_JP.UTF-8
では、早速Railsアプリを作ってみましょう。
今回は動くことを確認するためにscaffoldで作ります。
$ rails new enpit_app
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/secret_token.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
run bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.0)
Using i18n (0.6.5)
Installing minitest (4.7.5)
Using multi_json (1.7.9)
Using atomic (1.1.13)
Using thread_safe (0.1.2)
Using tzinfo (0.3.37)
Using activesupport (4.0.0)
Using builder (3.1.4)
Using erubis (2.7.0)
Using rack (1.5.2)
Using rack-test (0.6.2)
Using actionpack (4.0.0)
Using mime-types (1.25)
Using polyglot (0.3.3)
Using treetop (1.4.15)
Using mail (2.5.4)
Using actionmailer (4.0.0)
Using activemodel (4.0.0)
Using activerecord-deprecated_finders (1.0.3)
Using arel (4.0.0)
Using activerecord (4.0.0)
Using bundler (1.3.5)
Installing coffee-script-source (1.6.3)
Installing execjs (2.0.1)
Installing coffee-script (2.2.0)
Using thor (0.18.1)
Using railties (4.0.0)
Installing coffee-rails (4.0.0)
Using hike (1.2.3)
Installing jbuilder (1.5.1)
Installing jquery-rails (3.0.4)
Installing json (1.8.0)
Using tilt (1.4.1)
Using sprockets (2.10.0)
Using sprockets-rails (2.0.0)
Using rails (4.0.0)
Installing rdoc (3.12.2)
Installing sass (3.2.10)
Installing sass-rails (4.0.0)
Installing sdoc (0.3.20)
Installing sqlite3 (1.3.8)
Installing turbolinks (1.3.0)
Installing uglifier (2.2.1)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
$ cd enpit_app
$ emacs Gemfile
----追加-----
group :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
-------------
$ bundle install --without production
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.0)
Using i18n (0.6.5)
Using minitest (4.7.5)
Using multi_json (1.7.9)
Using atomic (1.1.13)
Using thread_safe (0.1.2)
Using tzinfo (0.3.37)
Using activesupport (4.0.0)
Using builder (3.1.4)
Using erubis (2.7.0)
Using rack (1.5.2)
Using rack-test (0.6.2)
Using actionpack (4.0.0)
Using mime-types (1.25)
Using polyglot (0.3.3)
Using treetop (1.4.15)
Using mail (2.5.4)
Using actionmailer (4.0.0)
Using activemodel (4.0.0)
Using activerecord-deprecated_finders (1.0.3)
Using arel (4.0.0)
Using activerecord (4.0.0)
Using bundler (1.3.5)
Using coffee-script-source (1.6.3)
Using execjs (2.0.1)
Using coffee-script (2.2.0)
Using thor (0.18.1)
Using railties (4.0.0)
Using coffee-rails (4.0.0)
Using hike (1.2.3)
Using jbuilder (1.5.1)
Using jquery-rails (3.0.4)
Using json (1.8.0)
Using tilt (1.4.1)
Using sprockets (2.10.0)
Using sprockets-rails (2.0.0)
Using rails (4.0.0)
Using rdoc (3.12.2)
Using sass (3.2.10)
Using sass-rails (4.0.0)
Using sdoc (0.3.20)
Using sqlite3 (1.3.8)
Using turbolinks (1.3.0)
Using uglifier (2.2.1)
Your bundle is complete!
Gems in the group production were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
$ rails g scaffold Blog acount_id:integer acount_name:string
acount_name:string
invoke active_record
create db/migrate/20130907060754_create_blogs.rb
create app/models/blog.rb
invoke test_unit
create test/models/blog_test.rb
create test/fixtures/blogs.yml
invoke resource_route
route resources :blogs
invoke scaffold_controller
create app/controllers/blogs_controller.rb
invoke erb
create app/views/blogs
create app/views/blogs/index.html.erb
create app/views/blogs/edit.html.erb
create app/views/blogs/show.html.erb
create app/views/blogs/new.html.erb
create app/views/blogs/_form.html.erb
invoke test_unit
create test/controllers/blogs_controller_test.rb
invoke helper
create app/helpers/blogs_helper.rb
invoke test_unit
create test/helpers/blogs_helper_test.rb
invoke jbuilder
create app/views/blogs/index.json.jbuilder
create app/views/blogs/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/blogs.js.coffee
invoke scss
create app/assets/stylesheets/blogs.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
$ bundle exec rake db:migrate
== CreateBlogs: migrating ====================================================
-- create_table(:blogs)
-> 0.0113s
== CreateBlogs: migrated (0.0136s) ===========================================
$ rails s
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
…アクセスの確認
http://localhost:3000/
Blogの確認
http://localhost:3000/blogs
これでRuby on Railsの動作確認はOKですね。
では、楽天APIを使ったアプリケーションを作り…たいですが、今回はもらってきましょうw
もらってくるためにGitHubを使います。
kawaharaさんのGitHubから rakuten-ws-rails-sample をもらってきましょう。
自分のプロジェクトをして扱うので、forkします!
kawahara/rakuten-ws-rails-sample
https://github.com/kawahara/rakuten-ws-rails-sample
forkしたら、cloneを行いソースを持ってくきます。
cloneを行う前にRSA鍵認証でソースを持ってくるためにRSA鍵の登録を行いましょう。
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/vagrant/.ssh/id_rsa. Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub. The key fingerprint is: 68:d6:ae:d3:47:81:78:0e:a0:60:17:4d:e5:ce:25:47 vagrant@precise The key's randomart image is: +--[ RSA 2048]----+ | .+... E | |.. .... . | |.... . + + | | . =o* . | | +*S . | | o .. . | | ... | | ... . | | .. . | +-----------------+ $ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6ShUmuXr6tnwKkmbPZzXoG5tdX6o9MYQetgj9WVkYlR+RHeTF4CRXlqHmfr+yyniEzKS9C8rWvq+DM68dh0TzDMwdldwHiUFm5mDT1Zwhjw4QBMt6IoZSxHkLftFPmM5sm8j6rR7qtXSZ8sba+VrHIYWkpvat9IRFiQr8VR8KERyNkgkRSKll9dWn7LqXkFrKvMCD6rbn+rAmEeLN7+YQ8wqVmmv3F6HhT2h4Vky+RV8GSKuabSJ08VMpWsqlRUnsgaYuhd2IN3w8H4vW7tA9Nal4qLStYK4fHqRe+YVQPGO9rBFJlLNbhEW/mjeZLd15JA7HUx/v746y3NHGfTLH vagrant@precise
公開鍵を作成したら、公開鍵をGitHubに登録します。
「Account Setting」→「SSH Keys」→「Add HHS key」

鍵の登録が終わったら、cloneしてきましょう。
あとついでにGitの初期設定も行いましょう。
$ cd /vagrant/enpit/ $ git clone git@github.com:himajin315/rakuten-ws-rails-sample.git Cloning into 'rakuten-ws-rails-sample'... remote: Counting objects: 62, done. remote: Compressing objects: 100% (49/49), done. remote: Total 62 (delta 2), reused 53 (delta 2) Receiving objects: 100% (62/62), 14.09 KiB, done. Resolving deltas: 100% (2/2), done. $ cd rakuten-ws-rails-sample/ $ git config --global user.name "himajin315" $ git config --global user.email himajin315@gmail.com
動かす前にいろいろと変更します。
作業する時は一度Branchを切って作業をしましょう!!
$ git checkout -b 'edit_Gem_controller' $ git branch * edit_Gem_controller master
では、Gemfileとapp/controllers/itemsearch_controller.rbのソースコードを変更しましょう。
$ emacs Gemfile -----------(追加:heroku)----------- ruby '2.0.0' group :production do gem 'rails_12factor' end ----------------------------------- $ bundle install --without production Resolving dependencies... Using rake (10.1.0) Using i18n (0.6.5) Using minitest (4.7.5) Using multi_json (1.7.9) Using atomic (1.1.13) Using thread_safe (0.1.2) Using tzinfo (0.3.37) Using activesupport (4.0.0) Using builder (3.1.4) Using erubis (2.7.0) Using rack (1.5.2) Using rack-test (0.6.2) Using actionpack (4.0.0) Using mime-types (1.25) Using polyglot (0.3.3) Using treetop (1.4.15) Using mail (2.5.4) Using actionmailer (4.0.0) Using activemodel (4.0.0) Using activerecord-deprecated_finders (1.0.3) Using arel (4.0.0) Using activerecord (4.0.0) Using bundler (1.3.5) Using coffee-script-source (1.6.3) Using execjs (2.0.1) Using coffee-script (2.2.0) Using thor (0.18.1) Using railties (4.0.0) Using coffee-rails (4.0.0) Using hike (1.2.3) Using httpclient (2.3.4.1) Using jbuilder (1.5.1) Using jquery-rails (3.0.4) Using json (1.8.0) Using tilt (1.4.1) Using sprockets (2.10.0) Using sprockets-rails (2.0.0) Using rails (4.0.0) Using rdoc (3.12.2) Using sass (3.2.10) Using sass-rails (4.0.0) Using sdoc (0.3.20) Using turbolinks (1.3.0) Using uglifier (2.2.1) Your bundle is complete! Gems in the group production were not installed. Use `bundle show [gemname]` to see where a bundled gem is installed.
今回はデータベースを使っていないため、rake db:migrate は行わなくても大丈夫です。
次に先ほど取得したapplication_secretとaffiliateIdを書き換えます。
$ emacs app/controllers/itemsearch_controller.rb
-----------------------
begin
data = httpClient.get_content('https://app.rakuten.co.jp/services/api/Ichi\
baItem/Search/20130805', {
'applicationId' => '1009720612747315343',
'affiliateId' => '11b23e39.373d568a.11b23e3a.912e032a',
'keyword' => keyword
})
-----------------------動作確認します。
$ rails s => Booting WEBrick => Rails 4.0.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2013-09-11 05:19:12] INFO WEBrick 1.3.1 [2013-09-11 05:19:12] INFO ruby 2.0.0 (2013-06-27) [i686-linux] [2013-09-11 05:19:12] INFO WEBrick::HTTPServer#start: pid=15402 port=3000 …
サーバ起動したので、サイトにアクセスしてみましょう。
http://localhost:3000/
動作確認ができたら、GitHubにpushをしましょう。
$ git stage -p (git add . でもOK)
diff --git a/Gemfile b/Gemfile
index cb26a93..5781409 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,8 +1,12 @@
source 'https://rubygems.org'
+ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
+group :production do
+ gem 'rails_12factor'
+end
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
Stage this hunk [y,n,q,a,d,/,s,e,?]? y
diff --git a/Gemfile.lock b/Gemfile.lock
index 37c5a19..65d4729 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -65,6 +65,11 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 4.0.0)
sprockets-rails (~> 2.0.0)
+ rails_12factor (0.0.2)
+ rails_serve_static_assets
+ rails_stdout_logging
+ rails_serve_static_assets (0.0.1)
+ rails_stdout_logging (0.0.2)
railties (4.0.0)
actionpack (= 4.0.0)
activesupport (= 4.0.0)
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
@@ -113,6 +118,7 @@ DEPENDENCIES
jbuilder (~> 1.2)
jquery-rails
rails (= 4.0.0)
+ rails_12factor
sass-rails (~> 4.0.0)
sdoc
turbolinks
Stage this hunk [y,n,q,a,d,/,K,g,e,?]? y
diff --git a/app/controllers/itemsearch_controller.rb b/app/controllers/itemsearch_controller.rb
index 12d4a4f..22cea19 100644
--- a/app/controllers/itemsearch_controller.rb
+++ b/app/controllers/itemsearch_controller.rb
@@ -12,8 +12,8 @@ class ItemsearchController < ApplicationController
begin
data = httpClient.get_content('https://app.rakuten.co.jp/services/api/IchibaItem/Search/20130805', {
- 'applicationId' => '1025899060325418115',
- 'affiliateId' => '0dd78f8a.26c4c8c0.0dd78f8b.0ee811d2',
+ 'applicationId' => '1051519437665746699',
+ 'affiliateId' => '11b23e39.373d568a.11b23e3a.912e032a',
'keyword' => keyword
})
@jsonData = JSON.parse data
Stage this hunk [y,n,q,a,d,/,e,?]? y
$ git status
# On branch edit_Gem_controller
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: Gemfile
# modified: Gemfile.lock
# modified: app/controllers/itemsearch_controller.rb
#
$ git commit -m "edit_Gem_contlloer"
# On branch edit_Gem_controller
nothing to commit (working directory clean)
$ git checkout master
M Gemfile
M Gemfile.lock
M app/controllers/itemsearch_controller.rb
Switched to branch 'master'
$ git merge edit_Gem_controller
Updating e3e2c4b..f7062ce
Fast-forward
Gemfile | 4 ++++
Gemfile.lock | 6 ++++++
app/controllers/itemsearch_controller.rb | 4 ++--
3 files changed, 12 insertions(+), 2 deletions(-)
$ git push
Counting objects: 13, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 755 bytes, done.
Total 7 (delta 6), reused 0 (delta 0)
To git@github.com:himajin315/rakuten-ws-rails-sample.git
e3e2c4b..f7062ce master -> master
次にherokuにデプロイする作業に入ります。
$ heroku login
Enter your Heroku credentials.
Email: yoshi315@live.jp
Password (typing will be hidden):
Authentication successful.
$ heroku keys:add
Found existing public key: /home/vagrant/.ssh/id_rsa.pub
Uploading SSH public key /home/vagrant/.ssh/id_rsa.pub... done
$ heroku create
Creating whispering-plains-2548... done, stack is cedar
http://whispering-plains-2548.herokuapp.com/ | git@heroku.com:whispering-plains-2548.git
Git remote heroku added
$ git push heroku master
The authenticity of host 'heroku.com (50.19.85.132)' can't be established.
RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'heroku.com,50.19.85.132' (RSA) to the list of known hosts.
Counting objects: 69, done.
Compressing objects: 100% (56/56), done.
Writing objects: 100% (69/69), 14.63 KiB, done.
Total 69 (delta 8), reused 56 (delta 2)
-----> Ruby/Rails app detected
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using Bundler version 1.3.2
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
Fetching gem metadata from https://rubygems.org/..........
Fetching gem metadata from https://rubygems.org/..
Installing rake (10.1.0)
Installing i18n (0.6.5)
Installing minitest (4.7.5)
Installing multi_json (1.7.9)
Installing atomic (1.1.13)
Installing thread_safe (0.1.2)
Installing tzinfo (0.3.37)
Installing activesupport (4.0.0)
Installing builder (3.1.4)
Installing erubis (2.7.0)
Installing rack (1.5.2)
Installing rack-test (0.6.2)
Installing actionpack (4.0.0)
Installing mime-types (1.25)
Installing polyglot (0.3.3)
Installing treetop (1.4.15)
Installing mail (2.5.4)
Installing actionmailer (4.0.0)
Installing activemodel (4.0.0)
Installing activerecord-deprecated_finders (1.0.3)
Installing arel (4.0.0)
Installing activerecord (4.0.0)
Installing coffee-script-source (1.6.3)
Installing execjs (2.0.1)
Installing coffee-script (2.2.0)
Installing thor (0.18.1)
Installing railties (4.0.0)
Installing coffee-rails (4.0.0)
Installing hike (1.2.3)
Installing httpclient (2.3.4.1)
Installing jbuilder (1.5.1)
Installing jquery-rails (3.0.4)
Installing json (1.8.0)
Using bundler (1.3.2)
Installing tilt (1.4.1)
Installing sprockets (2.10.0)
Installing sprockets-rails (2.0.0)
Installing rails (4.0.0)
Installing rails_serve_static_assets (0.0.1)
Installing rails_stdout_logging (0.0.2)
Installing rails_12factor (0.0.2)
Installing rdoc (3.12.2)
Installing sass (3.2.10)
Installing sass-rails (4.0.0)
Installing sdoc (0.3.20)
Installing turbolinks (1.3.0)
Installing uglifier (2.2.1)
Your bundle is complete! It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data --install
= 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
I, [2013-09-11T05:43:52.546333 #1097] INFO -- : Writing /tmp/build_94bzp3cfvo1n/public/assets/application-268d763ee3d9fa5a009263184a8d78b2.js
I, [2013-09-11T05:43:52.569063 #1097] INFO -- : Writing /tmp/build_94bzp3cfvo1n/public/assets/application-58c7c0e35a67f189e19b8c485930e614.css
Asset precompilation completed (8.06s)
Cleaning assets
-----> Discovering process types
Procfile declares types -> (none)
Default types for Ruby/Rails -> console, rake, web, worker
-----> Compiled slug size: 34.0MB
-----> Launching... done, v5
http://whispering-plains-2548.herokuapp.com deployed to Heroku
To git@heroku.com:whispering-plains-2548.git
* [new branch] master -> masterherokuの動作確認しましょう。
http://whispering-plains-2548.herokuapp.com
OK.
herokuへのデプロイが完了したので、今度はTravisCIとの連携に行きましょう。
https://travis-ci.org/
TravisCIを使うために設定ファイルを作成します。
$ git checkout -b travis $ emacs .travis.yml ----------追加------------ language: ruby rvm: - 2.0.0 -------------------------- $ bundle install $ git stage -p diff --git a/Gemfile b/Gemfile index 5781409..93ba3bc 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,10 @@ group :production do gem 'rails_12factor' end +group :test do + gem 'rake' +end + # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0' Stage this hunk [y,n,q,a,d,/,e,?]? y diff --git a/Gemfile.lock b/Gemfile.lock index 65d4729..2f6760f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,7 @@ DEPENDENCIES jquery-rails rails (= 4.0.0) rails_12factor + rake sass-rails (~> 4.0.0) sdoc turbolinks Stage this hunk [y,n,q,a,d,/,e,?]? y $ git stage .travis.yml $ git status # On branch travis # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: .travis.yml # modified: Gemfile # modified: Gemfile.lock # $ git commit -m 'tavisci_add_yum_rake' [travis f0d10f6] tavisci_add_yum_rake 3 files changed, 8 insertions(+) create mode 100644 .travis.yml $ git checkout masterSwitched to branch 'master' $ git merge travis Updating f7062ce..f0d10f6 Fast-forward .travis.yml | 3 +++ Gemfile | 4 ++++ Gemfile.lock | 1 + 3 files changed, 8 insertions(+) create mode 100644 .travis.yml
今度はGitHubとTravisCI側の設定をします。
まず、GitHubのレポジトリに行きます。
「Setteing」→「Service Hooks」→「Travis」


TravisCIのユーザ名とトークンを書きます。
トークンはTravisCIのアカウントから見ることができます。

トークンをコピーしましょう。
コピーしたトークンはGitHubのTokenに入力しましょう。
Activeにチェックを付けたら、「Update settings」をクリックをします。

次にTravis側のRepositoriesの「rakuten-ws-rails-sample」をONにします。

GutHubとTravisの設定は完了です。
GitHubにpushをし、動作確認を行いましょう。
(pushをするたびにTravisが動く)
$ git push Counting objects: 8, done. Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 481 bytes, done. Total 5 (delta 3), reused 0 (delta 0) To git@github.com:himajin315/rakuten-ws-rails-sample.git f7062ce..f0d10f6 master -> master
動作確認OK.
次にTravisとherokuとの連携です。
travisというコマンドがあるそうなので、gemでインストールしましょう。
$ git checkout travis $ gem install travis -v 1.5.3 --no-rdoc --no-ri Fetching: multipart-post-1.2.0.gem (100%) Successfully installed multipart-post-1.2.0 Fetching: faraday-0.8.8.gem (100%) Successfully installed faraday-0.8.8 Fetching: faraday_middleware-0.9.0.gem (100%) Successfully installed faraday_middleware-0.9.0 Fetching: highline-1.6.19.gem (100%) Successfully installed highline-1.6.19 Fetching: netrc-0.7.7.gem (100%) Successfully installed netrc-0.7.7 Fetching: backports-3.3.3.gem (100%) Successfully installed backports-3.3.3 Fetching: addressable-2.3.5.gem (100%) Successfully installed addressable-2.3.5 Fetching: net-http-persistent-2.9.gem (100%) Successfully installed net-http-persistent-2.9 Fetching: net-http-pipeline-1.0.1.gem (100%) Successfully installed net-http-pipeline-1.0.1 Fetching: gh-0.13.0.gem (100%) Successfully installed gh-0.13.0 Fetching: launchy-2.3.0.gem (100%) Successfully installed launchy-2.3.0 Fetching: coderay-1.0.9.gem (100%) Successfully installed coderay-1.0.9 Fetching: slop-3.4.6.gem (100%) Successfully installed slop-3.4.6 Fetching: method_source-0.8.2.gem (100%) Successfully installed method_source-0.8.2 Fetching: pry-0.9.12.2.gem (100%) Successfully installed pry-0.9.12.2 Fetching: ffi-1.9.0.gem (100%) Building native extensions. This could take a while... Successfully installed ffi-1.9.0 Fetching: ethon-0.6.1.gem (100%) Successfully installed ethon-0.6.1 Fetching: typhoeus-0.6.5.gem (100%) Successfully installed typhoeus-0.6.5 Fetching: websocket-1.0.7.gem (100%) Successfully installed websocket-1.0.7 Fetching: ruby-hmac-0.4.0.gem (100%) Successfully installed ruby-hmac-0.4.0 Fetching: pusher-client-0.3.1.gem (100%) Successfully installed pusher-client-0.3.1 Fetching: travis-1.5.3.gem (100%) Building native extensions. This could take a while... Successfully installed travis-1.5.3 22 gems installed $ rbenv rehash $ travis version 1.5.3 $ travis setup heroku Outdated CLI version, run `gem install travis` or use --skip-version-check. $ gem install travis Fetching: terminal-notifier-1.5.1.gem (100%) Successfully installed terminal-notifier-1.5.1 Fetching: travis-1.5.4.gem (100%) Building native extensions. This could take a while... Successfully installed travis-1.5.4 Parsing documentation for terminal-notifier-1.5.1 Installing ri documentation for terminal-notifier-1.5.1 Parsing documentation for travis-1.5.4 Installing ri documentation for travis-1.5.4 2 gems installed $ rbenv rehash $ travis setup heroku Deploy only from himajin315/rakuten-ws-rails-sample? |yes| yes Encrypt API key? |yes| yes
.travis.ymlに追加されていることを確認
$ cat .travis.yml language: ruby rvm: - 2.0.0 deploy: …
$ git stage . $ git commit -m "tarvis_yml_add_deploy" [travis 864dc50] tarvis_yml_add_deploy 1 file changed, 8 insertions(+), 1 deletion(-) $ git checkout master $ git merge travis Updating f0d10f6..864dc50 Fast-forward .travis.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
一応これでもできるけど、ついでにtestも書いちゃいましょう!
テストについては「RSpec/Capybara -- はじめの一歩」を参照しました。
http://www.oiax.jp/rails/rspec_capybara_primer/rspec_capybara_the_first_step.html
$ emacs Gemfile
---------追加(rspec-rails)------
group :test do
gem 'rake'
gem 'rspec-rails'
gem "capybara"
end
--------------------------------
$ bundle install
$ rm -rf test
(既存のtestを削除)
$ emacs config/application.rb
#require "rails/test_unit/railtie" コメントアウト
$ emacs spec/spec_helper.rb
#config.fixture_path = "#{::Rails.root}/spec/fixtures" コメントアウト
#config.use_transactional_fixtures = true コメントアウト
$ rails g rspec:install
create .rspec
create spec
create spec/spec_helper.rb
$ rspec
No examples found.
Finished in 0.00019 seconds
0 examples, 0 failures失敗するテストコードを書きます!
$ mkdir spec/features
$ emacs spec/features/top_page_spec.rb
-------------------------------
require 'spec_helper'
describe 'トップページ' do
specify '挨拶文を表示' do
visit root_path
expect(page).to have_css('p', text: 'Hello World!')
end
end
-------------------------------
$ rspec
F
Failures:
1) トップページ 挨拶文を表示
Failure/Error: expect(page).to have_css('p', text: 'Hello World!')
Capybara::ExpectationNotMet:
expected to find css "p" with text "Hello World!" but there were no matches
# ./spec/features/top_page_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 0.54378 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/top_page_spec.rb:5 # トップページ 挨拶文を表示
Randomized with seed 11915失敗しました。
がないとテスト失敗となります。テストが成功するように書き換えましょう。
$ emacs app/views/itemsearch/index.html.erb ---------追加(1行目)-------- <p>Hello World!</p> ----------------------------- $ rspec. Finished in 0.5469 seconds 1 example, 0 failures Randomized with seed 16208
成功です!!
では、GitHubにpushをし、TravisCIでテストが通り、herokuにデプロイできるかどうか確認しましょう。
$ git add . $ git status # On branch travis # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: .rspec # modified: Gemfile # modified: Gemfile.lock # modified: app/views/itemsearch/index.html.erb # modified: config/application.rb # new file: spec/features/top_page_spec.rb # new file: spec/spec_helper.rb # new file: spec/spec_helper.rb~ # deleted: test/controllers/.keep # deleted: test/fixtures/.keep # deleted: test/helpers/.keep # deleted: test/integration/.keep # deleted: test/mailers/.keep # deleted: test/models/.keep # deleted: test/test_helper.rb # $ git commit -m "remove_test,add_rspec" [travis 1157c46] remove_test,add_rspec 9 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 .rspec create mode 100644 spec/features/top_page_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/spec_helper.rb~ delete mode 100644 test/controllers/.keep delete mode 100644 test/fixtures/.keep delete mode 100644 test/helpers/.keep delete mode 100644 test/integration/.keep delete mode 100644 test/mailers/.keep delete mode 100644 test/models/.keep delete mode 100644 test/test_helper.rb $ git checkout master $ git merge travis Updating 864dc50..1157c46 Fast-forward .rspec | 1 + Gemfile | 2 ++ Gemfile.lock | 25 +++++++++++++++++++++ app/views/itemsearch/index.html.erb | 4 ++-- config/application.rb | 2 +- spec/features/top_page_spec.rb | 9 ++++++++ spec/spec_helper.rb | 42 +++++++++++++++++++++++++++++++++++ spec/spec_helper.rb~ | 42 +++++++++++++++++++++++++++++++++++ test/test_helper.rb | 7 ------ 9 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 .rspec create mode 100644 spec/features/top_page_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/spec_helper.rb~ delete mode 100644 test/controllers/.keep delete mode 100644 test/fixtures/.keep delete mode 100644 test/helpers/.keep delete mode 100644 test/integration/.keep delete mode 100644 test/mailers/.keep delete mode 100644 test/models/.keep delete mode 100644 test/test_helper.rb $ git push Counting objects: 29, done. Compressing objects: 100% (16/16), done. Writing objects: 100% (19/19), 2.85 KiB, done. Total 19 (delta 9), reused 0 (delta 0) To git@github.com:himajin315/rakuten-ws-rails-sample.git f0d10f6..1157c46 master -> master
テストが通ったら、herokuの方を見てみましょう!
http://whispering-plains-2548.herokuapp.com/

反映されてる ヽ(*´∀`)/
お疲れ様でした!!!!!!





