Symfony2プロジェクトをGitLabで管理してVagrantで立てたJenkinsでCIする(4)

第4回です。

前回のおさらい
イテレーション B: Phingでプロジェクトをビルドする
Phingビルドツールを導入し、PHPUnitをPhingから実行できるようになりました。

今回の内容
Vagrantを利用してJenkinsを構築します。


いまどこ?

  1. イテレーション 0: 準備
    1. タスク 0-1: 必要なソフトウェアをインストールする
    2. タスク 0-2: GitLabのアカウントを作成する
  2. イテレーション A: Symfony2でプロジェクトをはじめる
    1. タスク A-1: Symfony2プロジェクトを作成する
    2. タスク A-2: GitLabでソースコードを管理する
    3. タスク A-3: PHPUnitを導入する
  3. イテレーション B: Phingでプロジェクトをビルドする
    1. タスク B-1: Phingを導入する
    2. タスク B-2: PhingとPHPUnitを連携する
  4. イテレーション C: JenkinsをVagrantで構築する ←いまココ!
    1. タスク C-1: VMを初期化する
    2. タスク C-2: 構築に必要なCookbooksを取得・作成する
    3. タスク C-3: Jenkinsを起動する
  5. イテレーション D: JenkinsでCIする
    1. タスク D-1: Jenkinsにプロジェクトを設定する
    2. タスク D-2: テストレポートを表示できるように設定する
    3. タスク D-3: コードカバレッジレポートを表示できるように設定する
  6. イテレーション E: 各種ツールの導入
    1. タスク E-1 PHPMDを導入する
    2. タスク E-2 PHPCSを導入する


イテレーション C: JenkinsをVagrantで構築する

の繰り返しも含みますが、1から書いていきます。

タスク C-1: VMを初期化する

  • 準備

作業用ディレクトリを作ります。(以降、見やすさのためにプロンプト部分は一部省略します。)

C:\Users\{$user}\VirtualBox VMs>mkdir centos

  • boxの追加、VMの初期化
\centos>vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130309.box
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130309.box
[vagrant] Extracting box...91722240 / 491722240)[vagrant]
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

\centos>vagrant init centos
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.

タスク C-2: 構築に必要なCookbooksを取得・作成する

  • cookbook格納用ディレクトリの作成
\centos>mkdir cookbooks

\centos>cd cookbooks
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/yum
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/php
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/mysql
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/openssl
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/xml
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/java
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/windows
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/chef_handler
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/git
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/build-essential
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/dmg
\centos\cookbooks>git clone https://github.com/opscode-cookbooks/runit
\centos\cookbooks>git clone https://github.com/binarymarbles/chef-timezone timezone
\centos\cookbooks>git clone https://github.com/zircote/chef-composer composer

今回はphpサードパーティのものを利用します。
mysqlやらopensslやら一見関係のなさそうに見えるものもありますが、依存関係の都合で必要です。

  • オレオレcookbookを作成

Jenkinsのみ「これだ!」というサードパーティのものがなかったので、オレオレです。

\centos\cookbooks>knife cookbook create jenkins -o .

C:\Users\{$user}\VirtualBox VMs\centos\cookbooks\jenkins\recipes\default.rb を以下の内容にします。

yum_key "RPM-GPG-KEY-jenkins" do
  url "http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key"
  action :add
end

remote_file "/etc/yum.repos.d/jenkins.repo" do
  source "http://pkg.jenkins-ci.org/redhat/jenkins.repo"
  mode 0644
  action :create_if_missing
end

yum_package "jenkins" do
  action :install
end

タスク C-3: Jenkinsを起動する

  • Vagrantfileを編集する

C:\Users\{$user}\VirtualBox VMs\centos\Vagrantfile を編集します。
編集箇所は以下の2か所です。

ネットワーク設定

config.vm.network :hostonly, "192.168.33.10"

ホストオンリーにします。

Chef-Soloによるプロビジョニング

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "./cookbooks"
    chef.add_recipe "timezone"
    chef.add_recipe "yum::ius"
    chef.add_recipe "php"
    chef.add_recipe "composer"
    chef.add_recipe "java"
    chef.add_recipe "jenkins"
    chef.add_recipe "git"

    # You may also specify custom JSON attributes:
    chef.json = {
      :timezone => {
        :area => "Asia",
        :zone => "Tokyo"
      },
      :yum => {
        :ius_release => "1.0-11"
      },
      :php => {
        :install_method => "package",
        :packages => %w(php54 php54-mbstring php54-pdo php54-xml php54-pecl-xdebug),
        :directives => {
          "date.timezone" => "Asia/Tokyo"
        }
      },
      :composer => {
        :install_path => "/usr/local/bin"
      },
      :java => {
        :install_flavor => "oracle",
        :jdk_version => 7,
        :oracle => {
          :accept_oracle_download_terms => true
        }
      }
    }
  end

  • VMを起動する
C:\Users\{$user}\VirtualBox VMs\centos>vagrant up
[default] Importing base box 'centos'...
[default] Matching MAC address for NAT networking...
(中略)
[2013-05-21T21:24:14+09:00] INFO: Processing package[git] action install (git::default line 31)
[2013-05-21T21:24:14+09:00] INFO: Chef Run complete in 91.731508737 seconds
[2013-05-21T21:24:14+09:00] INFO: Running report handlers
[2013-05-21T21:24:14+09:00] INFO: Report handlers complete

結構、時間かかります。

  • Jenkinsを起動する

SSHでログイン

C:\Users\{$user}\VirtualBox VMs\centos>vagrant ssh
Last login: Tue May 21 21:24:33 2013 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.


Firewallを停止

[vagrant@localhost ~]$ sudo service iptables stop
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[vagrant@localhost ~]$ sudo service ip6tables stop
ip6tables: Flushing firewall rules:                        [  OK  ]
ip6tables: Setting chains to policy ACCEPT: filter         [  OK  ]
ip6tables: Unloading modules:                              [  OK  ]

本来はきっちり設定した方がよいと思います。

起動

[vagrant@localhost ~]$ sudo service jenkins start
Starting Jenkins                                           [  OK  ]


ブラウザから確認
http://192.168.33.10:8080/ に接続し、以下が表示されればOKです。
f:id:piccagliani:20130521112457p:plain


以上です。

第5回はJenkinsでCIする