UbuntuにてRoRのインストールからJenkinsの利用まで

参考のやつをそのまま試した備忘log

環境

  • Ubuntu 12.04 amd64
  • rbenv 0.4.0
  • Ruby 2.0.0
  • Ruby on Rails 4

必要な物をapt-get

1
$ sudo apt-get install build-essential git curl zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev sqlite3 libsqlite3-dev nodejs 

rbenv(0.4.0)

1
2
3
4
$ git clone git://github.com/sstephenson/rbenv.git .rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL

ruby-build

1
2
3
$ mkdir -p ~/.rbenv/plugins
$ cd ~/.rbenv/plugins
$ git clone git://github.com/sstephenson/ruby-build.git

Ruby(2.0.0)

1
2
3
$ rbenv install 2.0.0-p353
$ rbenv rehash
$ rbenv global 2.0.0-p353

.gemrc

1
2
$ echo "install: --no-rdoc --no-ri" >> ~/.gemrc
$ echo "update: --no-rdoc --no-ri" >> ~/.gemrc

Bundler

1
2
3
$ gem update
$ gem install bundler
$ rbenv rehash

Ruby on Rails(4.0.2)

1
2
$ gem install rails
$ rbenv rehash

アプリの作成

Testなしで作成

1
2
$ rails new circle -T
$ cd circle

userメンテ画面をscaffold

1
$ rails g scaffold user name:string age:integer

DBの作成 && テスト用DBの作成

1
2
$ rake db:migrate
$ rake db:test:clone

rails serveer起動

1
$ rails s

http://localhost:3000/ にて確認

Gemfileにrspec-railsを追記

1
2
3
4
$ vim Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 2.0'
end

Gemの更新

1
$ bundle install

Rspecのセットアップ

1
$ rails generate rspec:install

.rspecの以下のように変更

1
2
3
4
#変更前
--color
#変更後
--color --format documentation

Userモデルに対して、以下のバリデーションの追加

1
2
3
4
5
6
7
8
$ vim app/models/user.rb
  validates :name,
    :presence => true,
    :length => {:maximum => 20}
  validates :age,
    :presence => true,
    :numericality => {:greater_than_or_equal_to => 0,
                      :less_than_or_equal_to => 1000}
  • name : 必須入力 && 最大文字数が20
  • age : 必須入力 && 範囲が0 – 1000

UserモデルのTestの記述

1
2
$ mkdir spec/models
$ vim spec/models/user_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
require 'spec_helper'

describe User do
  context '何も入力しなかった場合' do
    subject{User.new}
    it {should_not be_valid}
    it{ should have(1).errors_on(:name) }
    it{ should have(2).errors_on(:age) }
  end

  context '名前が文字数をオーバーしている場合' do
    subject{User.new(name: 'aaaaaaaaaabbbbbbbbbbc',age: 10)}
    it {should_not be_valid}
    it{ should have(1).errors_on(:name) }
    it{ should have(0).errors_on(:age) }
  end

  context '年齢が範囲を超えている場合' do
    subject{User.new(name: 'taro',age: 1001)}
    it {should_not be_valid}
    it{ should have(0).errors_on(:name) }
    it{ should have(1).errors_on(:age) }
  end

  context '正常なデータがセットされた場合' do
    subject{User.new(name: 'taro',age: 20)}
    it {should be_valid}
  end
end

rspecの実行

1
2
3
4
5
$ bundle exec rspec
...
Finished in 0.08931 seconds
10 examples, 0 failures // <-これがでればok
...

Gemfileにcapybaraとturnipの追加

1
2
3
4
5
6
$ vim Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 2.0'
  gem 'capybara' # 追記
  gem 'turnip'   # 追記
end

Gemの更新

1
$ bundle install

.rspecファイルの修正

1
2
3
4
5
$ vim .rspec
# 変更前
--color --format documentation
# 変更後
-r turnip/rspec --color --format documentation

require の下辺りに追記

1
2
3
4
5
6
Dir.glob("spec/**/*steps.rb") { |f| load f, true}

require 'capybara/dsl'
require 'capybara/rspec'
require 'turnip'
require 'turnip/capybara'

featureファイルと、stepファイルを記述

1
2
3
$ mkdir spec/features
$ vim spec/features/users.feature
$ vim spec/steps/users_steps.rb

spec/features/users.feature の中身

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# encoding: utf-8
# language: ja
機能: ユーザー管理
  ユーザー管理を行いたい。なぜならサークル活動に参加するメンバーを管理する必要があるからだ。

  シナリオ: 一覧画面から新規登録画面に移動する
    前提 ユーザー管理の一覧画面を開く
    もし "New User" リンクをクリックする
    ならば ユーザー管理の新規登録画面が表示される

  シナリオ: 登録画面で登録すると詳細画面に移動する
    前提 ユーザー管理の新規登録画面を開く
    もし "Name" に "アリス" と入力する
    かつ "Age" に "16" と入力する
    かつ "Create User" ボタンをクリックする
    ならば ユーザー管理の詳細画面が表示される

spec/steps/users_steps.rbの中身

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# encoding: utf-8

step 'ユーザー管理の一覧画面を開く' do
  visit '/users'
end

step '画面を目視' do
  save_and_open_page
end

step ':name リンクをクリックする' do |name|
  first(:link, name).click
end

step ':name ボタンをクリックする' do |name|
  first(:button, name).click
end

step 'ユーザー管理の新規登録画面が表示される' do
  current_path = URI.parse(current_url).path
  current_path.should == '/users/new'
end

step 'ユーザー管理の詳細画面が表示される' do
  current_path = URI.parse(current_url).path
  current_path.should == '/users/1'
end

step 'ユーザー管理の新規登録画面を開く' do
  visit '/users/new'
end

step ':field に :value と入力する' do |field, value|
  fill_in(field, :with => value)
end

Turnipを実行

1
$ bundle exec rspec spec/features/users.feature

Gitで管理

1
2
3
4
5
6
$ git init
$ git config --global color.ui true
$ git config --global user.email "hoge@fuga.com"
$ git config --global user.name "kanpe777"
$ git stage .
$ git commit -m "Initial commit"

Jenkins側

rbenv Pluginのインストール

  • Manage Jenkins –> Plugin Manager
  • Availableタブを選択し、RVM Pluginにチェックを入れる
  • Install without restart

Git Pluginのインストール

  • Manage Jenkins –> Plugin Manager
  • Availableタブを選択し、Git Pluginにチェックを入れる
  • Download now and install after restart
  • sudo service jenkins restart

ジョブの作成

  • top
  • New Job
  • Job name に「circle」, Build a free-style software projectにチェック
  • OKボタン
  • Souce Code Management の欄でGitを選択
  • Repository URLはcircleがある場所(pwd等で確認可能, 自分の場合は/home/vagrant/circle)
  • Build Environment欄でrbenv build wrapperにチェック
  • Implementationにruby 1.9.3-p429を記述
  • Build欄でAdd build step, Execute shell (メモリに注意)

Commandの中身

1
2
3
4
bundle install
bundle exec rake db:migrate
bundle exec rake db:test:clone
bundle exec rspec
  • Apply –> Save
  • Topにてcircleを選択して、Build Now等でbuild実行
  • 青いマークが成功、赤いマークが失敗

自動実行

  • circle ジョブを選択し、Configureでジョブの設定へ
  • Build Triggers欄にてPoll SCMにチェック
  • H/5 * * * *を記述(意味は5分毎にコミットを確認する)

Build Pipelie の可視化

  • Jobを2つ作成

    1つめ:circle_unit_test

  • Souce Code Management欄でGitを選択し、リポジトリの場所を指定(自分の場合は/home/vagrant/circle)
  • Build Triggers欄でPoll SCMを選択し、H/5 * * * *を記述
  • Build 欄でAdd build stepより、execute shellを選択し、Commandに以下を記述

Commandの中身

1
2
3
4
bundle install
bundle exec rake db:migrate
bundle exec rake db:test:clone
bundle exec rspec spec/*/*_spec.rb
  • Post-build Actions欄にてAdd post-build actionより、Trigger parameterized build on other projectsを選択
  • Projects to build 欄にcircle_integration_testと記述(事前にcircle_integration_testジョブを作成必要あり)

参考

[Ruby on Rails] Ubuntu に rbenv + Bundler で Rails 4 をインストールする

Jenkinsで小さなテストを実行してみよう(Mac OS X&Linux)

アプリケーションをデプロイしてみよう(Mac OS X&Linux)