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

第2回です。

前回のおさらい
イテレーション 0: 準備
必要となるソフトウェアのインストール等、進めるための準備をしました。

今回の内容
Symfony2のプロジェクトを作成し、GitLabで管理します。
また、PHPUnitを導入してテストを実行できるようにします。

おことわり
Symfony2でどのようにアプリケーションを構築するか、については本来の目的から離れるため、省略しSymfony2に含まれているデモアプリケーションを利用します。

いまどこ?

  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を導入する


イテレーション A: Symfony2でプロジェクトをはじめる

タスク A-1: Symfony2プロジェクトを作成する

  • プロジェクトを格納するディレクトリを作る
C:\>mkdir workspace

  • composerをインストール
C:\>cd workspace

C:\workspace>curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: C:\workspace\composer.phar
Use it: php composer.phar

[参考]
 Composer

  • プロジェクトの作成
C:\workspace>php composer.phar create-project symfony/framework-standard-edition Symfony2 2.2.1
Installing symfony/framework-standard-edition (v2.2.1)
  - Installing symfony/framework-standard-edition (v2.2.1)
(中略)
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
プロジェクト名
「Symfony2」を指定しています。
使用するバージョン
2013/05/20時点での最新安定板2.2.1を指定しています。

[参考]
 Installing and Configuring Symfony (2.2) - Symfony

  • composerをプロジェクトルートに移動
C:\workspace>mv composer.phar Symfony2

後々便利なので。。。

  • ビルトインサーバーを利用して動作確認
C:\workspace>cd Symfony2

C:\workspace\Symfony2>php app/console server:run
Server running on localhost:8000

ブラウザで、http://localhost:8000/ にアクセスし、以下の画面が出てくればOKです。

f:id:piccagliani:20130520145007p:plain

  • デモアプリの動作確認

初期状態でデモアプリが入ってますので、動作確認をします。
http://localhost:8000/demo/ にアクセスし、以下の画面が出てくればOKです。

f:id:piccagliani:20130520151801p:plain


タスク A-2: GitLabでソースコードを管理する

C:\workspace\Symfony2>git init
Initialized empty Git repository in C:/workspace/Symfony2/.git/

C:\workspace\Symfony2>git add .
warning: LF will be replaced by CRLF in .gitignore.
The file will have its original line endings in your working directory.
(中略)
warning: LF will be replaced by CRLF in web/config.php.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in web/robots.txt.
The file will have its original line endings in your working directory.

C:\workspace\Symfony2>git commit -m "Initial Commit"
[master (root-commit) 0ba6022] Initial Commit
warning: LF will be replaced by CRLF in .gitignore.
(中略)
 create mode 100644 web/apple-touch-icon.png
 create mode 100644 web/config.php
 create mode 100644 web/favicon.ico
 create mode 100644 web/robots.txt

GitLabにログインし、https://gitlab.com/projects/new にアクセスします。
「Project Name is」に「Symfony2」を入力して、「Create project」ボタンをクリックします。
f:id:piccagliani:20130520154341p:plain

C:\workspace\Symfony2>git remote add gitlab https://gitlab.com/{$user_name}/symfony2.git

C:\workspace\Symfony2>git remote -v
gitlab  https://gitlab.com/{$user_name}/symfony2.git (fetch)
gitlab  https://gitlab.com/{$user_name}/symfony2.git (push)
origin
upstream

  • GitLabにpush
C:\workspace\Symfony2>git push gitlab master
Username for 'https://gitlab.com':
Password for 'https://{$user_name}@gitlab.com':
(中略)
Writing objects: 100% (86/86), 63.52 KiB, done.
Total 86 (delta 2), reused 0 (delta 0)
To https://gitlab.com/{$user_name}/symfony2.git
 * [new branch]      master -> master

タスク A-3: PHPUnitを導入する

  • composerで依存関係を追加
C:\workspace\Symfony2>php composer.phar require --dev phpunit/phpunit 3.7.*
composer.json has been updated
Loading composer repositories with package information
(中略)
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution

本番環境では必要ないと思うので、「--dev」オプションをつけています。

  • テストを実行してみる
C:\workspace\Symfony2>bin\phpunit -c app
PHPUnit 3.7.20 by Sebastian Bergmann.

Configuration read from C:\workspace\Symfony2\app\phpunit.xml.dist

.

Time: 1 second, Memory: 21.25Mb

OK (1 test, 1 assertion)

デモアプリのテストケースが1個、実行されます。
テストコード自体は
C:\workspace\Symfony2\src\Acme\DemoBundle\Tests\Controller\DemoControllerTest.php
です。


最後に

  • composer.json
  • composer.lock

PHPUnitの追加によって変更されていますので、コミットしてGitLabにも反映しておきます。

C:\workspace\Symfony2>git add composer.json composer.lock

C:\workspace\Symfony2>git commit -m "適当なメッセージ"

C:\workspace\Symfony2>git push gitlab master


以上です。

第3回はイテレーション B: Phingでプロジェクトをビルドする