ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

CakePHPで継続的インテグレーションその1

スポンサードリンク

photo by iogi

CakePHPで学ぶ継続的インテグレーション

CakePHPで学ぶ継続的インテグレーション

CakePHPで学ぶ継続的インテグレーション (impress top gear)

CakePHPで学ぶ継続的インテグレーション (impress top gear)

(2つあるけど何故?)

約1年前にこの本を買いました。

読んではみたのですが、ちんぷんかんぷんで全く頭に入ってこない。なので、お蔵入りに。。。(´;ω;`)ウッ…

しかーし。

最近Jenkinsの勉強会に参加するようになったのでなんとなーく使い方を理解しはじめてきました。

んで、ちょっとtry&errorを繰り返したところ、PHPのテスト環境構築が中々うまくいかなくて。

そういえば、そんな本もっていたよな。。。と探したら見つけ再読しています。

この本の通りとはいきませんが、とりあえずインテグレーションの環境構築を試してみようと思います。

大体やりたいことだけ抜き取ってやろうとすると環境があってないとかで中々前に進まないので、まずは書籍の通りに進めます。

また、エラーとか他足りないものを補うような+@になることも追記するのでよろしくです。

環境

$ sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.10.5
BuildVersion:   14F27

Jenkinsはインストール済みです。

phpを5.6へバージョンアップ

チャレンジする前にPHPのバージョンもついでに上げてみる。

$ brew install php56 --with-apxs2=/usr/sbin/apxs

# ちょっと時間かかるけどとくに問題なくインストールできた

$ source ~/.bash_profile
$ php -v
PHP 5.6.0 (cli) (built: Jun  5 201x xx:04:44)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

.gitignoreの用意

github.com

$ cd ~/project/
$ git init

.gitignoreを用意

# CakePHP 3

/vendor/*
/config/app.php
/tmp/*
/logs/*

# CakePHP 2

/app/tmp/*
/app/Config/core.php
/app/Config/database.php
/vendors/*

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
$ vim .gitignore

これで良し。

first commit

んで、はじめてのコミットを実行

$ git add .
$ git commit -m "first commit"

composerで初期構築

次にcomposerを使って環境構築していきます。

$ composer init
Warning: This development build of composer is over 30 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.

                                            
  Welcome to the Composer config generator  
                                            


This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [my/cakephp_integlation]: php_ci_book/blogapp
Description []: 
Author [myName <test@gmail.com>]: 
Minimum Stability []: 
Package Type []: 
License []: 

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

{
    "name": "php_ci_book/blogapp",
    "authors": [
        {
            "name": "myName",
            "email": "test@gmail.com"
        }
    ],
    "require": {}
}

いくつか質問に回答する必要がありますが、↑のような感じで入力します。

CakePHPをインストール

$ composer require "cakephp/cakephp":"2.6.*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - cakephp/cakephp 2.6.9 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.8 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.7 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.6 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.5 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.4 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.3 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.2 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.11 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.10 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.1 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - cakephp/cakephp 2.6.0 requires ext-mcrypt * -> the requested PHP extension mcrypt is missing from your system.
    - Installation request for cakephp/cakephp 2.6.* -> satisfiable by cakephp/cakephp[2.6.0, 2.6.1, 2.6.10, 2.6.11, 2.6.2, 2.6.3, 2.6.4, 2.6.5, 2.6.6, 2.6.7, 2.6.8, 2.6.9].


Installation failed, reverting ./composer.json to its original content.

(°ω°!

エラーがめちゃんこ出力される。どうやらmcryptってのがないらしい

$ php -m | grep mcrypt

なんにもヒットしない。ビンゴだね。

$ brew install php56-mcrypt



Error: Unable to guess PHP branch for Formulary::Formulae::Php56Mcrypt

?!!!!

updateしてねだって

$ sudo brew update
$ brew install php56-mcrypt

$ php -m | grep mcrypt
mcrypt

おkおk

もっかい実行

$ composer require "cakephp/cakephp":"2.6.*"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing cakephp/cakephp (2.6.11)
    Downloading: 100%         

Writing lock file
Generating autoload files

できました〜ヾ(´∀`)ノキャッキャ

$ ls -l Vendor/
total 8
-rw-r--r--  1 my  staff  183  9  5 17:35 autoload.php
drwxr-xr-x  3 my  staff  102  9  5 17:35 bin
drwxr-xr-x  3 my  staff  102  9  5 17:35 cakephp
drwxr-xr-x  8 my  staff  272  9  5 17:35 composer

ほげっ!いつものCakePHPじゃないぞ。。。。? ディレクトリ構造がよくわからん。。。。?

とりあえず、bakeしたらなんとかなるらしい

$ Vendor/bin/cake bake project .

yしか押してないからね。

$ ls
Config      Controller  Locale      Plugin      Vendor      composer.json   index.php   webroot
Console     Lib     Model       Test        View        composer.lock   tmp

おおっ! すばらしい。勝手(自動)に生成された。

CaKPHP Environments Library のインストール

コレ使うと環境によって設定ファイルを切り替えることができる!(らしい)

$ composer require "josegonzalez/cakephp-enviroments":"1.0.0"

# ~ 省略 ~

  Problem 1
    - The requested package josegonzalez/cakephp-enviroments could not be found in any version, there may be a typo in the package name.

えっ(’・∀・)

なんかダウンロードできんかった。。。。?

しょうがいなのでWebで検索してGitからダウンロードします。

$ git submodule add git://github.com/josegonzalez/cakephp-environments.git Plugin/Environments
$ git submodule init
$ git submodule update

おk

ではファイルを作成していきます。

$ mkdir -p ./Config/bootstrap/environments
$ touch ./Config/bootstrap/environments.php
  • ./Config/bootstrap/environments.php
<?php
CakePlugin::load('Environments');

App::uses('Environment', 'Environments.Lib');

// Webサーバに設定する
$env = env('CAKE_ENV');

switch ($env) {
    // 本番環境
    case 'production':
        include dirname(__FILE__) . DS . 'environments' . DS . 'production.php';
        break;
    // 開発環境
    default:
        include dirname(__FILE__) . DS . 'environments' . DS . 'development.php';
        include dirname(__FILE__) . DS . 'environments' . DS . 'test.php';
        include dirname(__FILE__) . DS . 'environments' . DS . 'ci.php';
        break;
}

Environment::start();

んで、それぞれ下記のようにファイルを用意していく

  • ~/Config/bootstrap/environments/development.php
<?php

Environment::configure('development', true, [
    'MYSQL_DB_HOST'      => 'localhost',
    'MYSQL_USERNAME'     => 'localhost',
    'MYSQL_PASSWORD'     => 'localhost',
    'MYSQL_DB_NAME'      => 'localhost',
    'MYSQL_TEST_DB_NAME' => 'localhost',
    'MYSQL_PREFIX'       => 'localhost'
]);

用意したファイルを読み込むように./Config/bootstrap.phpの19行目あたりに追記する。

  • ~/Config/bootstrap.php
<?php
// ~ 省略 ~
include dirname(__FILE__) . DS . 'bootstrap' . DS . 'environments.php';

んで、./Config/database.phpにさっき用意した定数を追記する。

  • ~/Config/database.php
<?php
class DATABASE_CONFIG {

    public $default;
    public $test;

    public function __construct() {
        $this->default = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host'       => $this->read('MYSQL_DB_HOST'),
            'login'      => $this->read('MYSQL_USERNAME'),
            'password'   => $this->read('MYSQL_PASSWORD'),
            'database'   => $this->read('MYSQL_DB_NAME'),
            'prefix'     => $this->read('MYSQL_PREFIX'),
            'encoding'   => 'utf8',
        );

        $this->test = array(
            'datasource' => 'Database/Mysql',
            'persistent' => false,
            'host'       => $this->read('MYSQL_TEST_DB_NAME'),
            'login'      => $this->read('MYSQL_USERNAME'),
            'password'   => $this->read('MYSQL_PASSWORD'),
            'database'   => $this->read('MYSQL_DB_NAME'),
            'prefix'     => $this->read('MYSQL_PREFIX'),
            'encoding'   => 'utf8',
        );
    }

    public function read($key, $default = null) {
        $value = env($key);
        if ($value !== null) {
            return $value;
        }

        $value = Configure::read($key);
        if ($value !== null) {
            return $value;
        }

        return $default;
    }
}

んで、nginxに設定します。

 70         location ~ \.php$ {
 71             root           /usr/local/cakephp_integlation/webroot;
 72             fastcgi_pass   127.0.0.1:9003;
 73             fastcgi_index  index.php;
 74             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 75             fastcgi_param  CAKE_ENV         'development'; # ここを追加
 76             include        fastcgi_params;
 77         }

これ追加

bitbucketにpush

これの説明は省きます。(前やったような気が。。。)

aipacommander.hatenablog.jp

$ git remote add origin git@bitbucket.org:xxxx/cakephp_integlation.git
$ git push origin master

登録してgit pushしてしまえばおk。

Jenkinsの設定

jobを作ります。

f:id:aipacommander:20150909202119p:plain

gitの設定をします。

pluginが必要だったかも。。。。

f:id:aipacommander:20150909202555p:plain

トリガも準備

これすごい。hookしなくてもいいんだね。

f:id:aipacommander:20150909202115p:plain

メモ

とりあえずここまで!

次回は、DBマイグレーションかもしくは、開発の部分か。どっちかです。