$30 off During Our Annual Pro Sale. View Details »

CakePHPで学ぶ継続的インテグレーション ハンズオン@大阪 開発編

Kaz Watanabe
November 01, 2014

CakePHPで学ぶ継続的インテグレーション ハンズオン@大阪 開発編

Kaz Watanabe

November 01, 2014
Tweet

More Decks by Kaz Watanabe

Other Decks in Programming

Transcript

  1. WHO? • Θͨͳ΂͔ͣͻΖ(@kaz_29) • ߹ಉձࣾ decr (decr.jp) • PHP(CakePHP)ͰWebΞϓϦ։ൃ •

    Πϯϑϥߏஙɾӡ༻ • iOSΞϓϦ։ൃ #beer #iosdev #cakephp #cat #camp! #golf #chef #vagrant
  2. ࠷ॳͷεϓϦϯτ͕ ׬ྃͨ͠ঢ়ଶΛ࡞੒ ! host $ vagrant ssh develop $ cd

    /var/www/application/current/app $ git checkout master $ composer install --dev
  3. ͦͷલʹ… $ git update-index --add --chmod=+x Console/cake $ git add

    -u $ git commit -m “cakeίϚϯυʹ࣮ߦݖΛ෇༩” $ git push origin master
  4. BUILDͷઃఆ <?xml version="1.0" encoding="UTF-8"?> <!-- Phing build config file. @link

    http://www.phing.info/ --> <project name="blogapp" default="build"> <!-- Properties --> <property name="basedir" value="${phing.dir}" /> <property name="appdir" value="${basedir}/app" /> <property name="logdir" value="${appdir}/tmp/logs" /> ! <!-- Build(1) --> <target name="build" depends="prepare,caketest"/> ! <!-- Prepare(2) --> <target name="prepare" description="Prepare for build"> <exec dir="${appdir}" executable="${appdir}/Console/cake" output="${logdir}/migration.log" checkreturn="true"> <arg line="migrations.migration" /> <arg line="run" /> <arg line="all" /> </exec> </target> ! <!-- CakePHP unit test with PHPUnit(3) --> <target name="caketest" description="Run CakePHP unit tests with PHPUnit"> <exec dir="${appdir}" executable="${appdir}/Console/cake" output="${logdir}/caketest.log" checkreturn="true"> <arg line="test" /> <arg line="app" /> <arg line="AllTests" /> </exec> </target> </project> /var/www/application/current/build.xml
  5. BUILDπʔϧΛಋೖ ! — मਖ਼Λίϛοτ $ git add build.xml $ git

    commit -m ‘ϏϧυϑΝΠϧΛ௥Ճ' $ git push origin master !
  6. UnitTest݁ՌΛදࣔ͢Δ ! — build.xmlΛमਖ਼ <target name="caketest" description="Run CakePHP unit tests

    with PHPUnit"> <exec dir="${appdir}" executable="${appdir}/Console/cake" output="${logdir}/caketest.log" checkreturn="true"> <arg line="test" /> <!-- ͔͜͜Β --> <arg line="--log-junit=${appdir}/reports/unittest.xml" /> <arg line="--coverage-html=${appdir}/reports" /> <arg line="--coverage-clover=${appdir}/reports/ coverage.xml" /> <!-- ͜͜·Ͱ௥Ճ --> <arg line="app" /> <arg line="AllTests" /> </exec> </target>
  7. UnitTest݁ՌΛදࣔ͢Δ ! — मਖ਼Λίϛοτ $ git add build.xml $ git

    commit -m 'Ϣχοτςετͷ݁ՌΛϑΝΠϧ ʹग़ྗ' $ git push origin master
  8. UnitTest݁ՌΛදࣔ͢Δ ! — app/phpunit.xmlΛमਖ਼(ΧόϨοδͷআ֎ઃఆΛ௥Ճ) <phpunit> <filter> <blacklist> <directory suffix=".php">Plugin</directory> <directory

    suffix=".php">Test</directory> <directory suffix=".php">Vendor</directory> <!-- ͔͜͜Β --> <directory suffix=".php">Config/bootstrap</directory> <file>Config/database.php</file> <-- ͜͜·Ͱ௥Ճ --> </blacklist> </filter> </phpunit>
  9. UnitTest݁ՌΛදࣔ͢Δ ! — मਖ਼Λίϛοτ $ git add app/phpunit.xml $ git

    commit -m 'ΧόϨοδআ֎ର৅Λ௥Ճ' $ git push origin master
  10. Behatͷ݁ՌΛදࣔ͢Δ — build.xmlΛमਖ਼ … <target name="build" depends="prepare,caketest,behat"/> … <!-- ͔͜͜Β

    --> <target name="behat" description="Run CakePHP acceptance test with Behat"> <exec command="find ${appdir}/tmp -type d -print | xargs chmod 777" escape="false" /> <exec dir="${appdir}" executable="${appdir}/Console/cake" output="${logdir}/behat.log" checkreturn="true"> <arg line="Bdd.story" /> <arg line="--format=junit" /> <arg line="--out=${appdir}/reports" /> </exec> </target> <!-- ͜͜·Ͱ௥Ճ -->
  11. Behatͷ݁ՌΛදࣔ͢Δ ! — मਖ਼Λίϛοτ $ git add build.xml $ git

    commit -m 'BehatςετΛ௥Ճ' $ git push origin master
  12. ੩తղੳΛ࣮ߦ͢Δ $ cd /var/www/application/current/app $ composer require --dev "cakephp/cakephp-codesniffer:0.1.*" $

    composer require --dev "phpmd/phpmd:2.*" $ composer require --dev "sebastian/phpcpd:2.*"
  13. ੩తղੳΛ࣮ߦ͢Δ ! — मਖ਼Λίϛοτ $ git add composer.json composer.lock $

    git commit -m 'ΠϯεϖΫγϣϯπʔϧͷ௥Ճ' $ git push origin master
  14. ੩తղੳΛ࣮ߦ͢Δ — build.xmlΛमਖ਼(1) … <!-- Properties --> <property name="basedir" value="${phing.dir}"

    /> <property name="appdir" value="${basedir}/app" /> <property name="logdir" value="${appdir}/tmp/logs" /> <!-- vendorbindir ௥Ճ --> <property name="vendorbindir" value="${appdir}/Vendor/bin" /> ! <!-- Build --> <target name="build" depends="prepare,caketest,behat,phpcs,phpmd,phpcpd"/> …
  15. ੩తղੳΛ࣮ߦ͢Δ — build.xmlΛमਖ਼(2) - PHPCodeSnifferͷઃఆΛ௥Ճ … <target name="phpcs" description="Find coding

    standard violations using PHP_CodeSniffer."> <exec dir="${basedir}" executable="${vendorbindir}/phpcs" output="$ {logdir}/phpcs.log"> <arg line="--config-set" /> <arg line="installed_paths" /> <arg line="${appdir}/Vendor/cakephp/cakephp-codesniffer" /> </exec> <exec dir="${basedir}" executable="${vendorbindir}/phpcs" output="$ {logdir}/phpcs.log"> <arg line="--ignore=Test,Vendor,Plugin" /> <arg line="--report=checkstyle" /> <arg line="--report-checkstyle=${appdir}/reports/checkstyle.xml" /> <arg line="--standard=CakePHP" /> <arg line="--extensions=php" /> <arg line="${appdir}" /> </exec> </target>
  16. ੩తղੳΛ࣮ߦ͢Δ — build.xmlΛमਖ਼(3) - PHPMDͷઃఆΛ௥Ճ … <target name="phpmd" description="Perform project

    mess detection using PHPMD"> <exec dir="${basedir}" executable="${vendorbindir}/phpmd" output="${logdir}/phpmd.log"> <arg line="${appdir}" /> <arg line="xml" /> <arg line="codesize,unusedcode,design,naming" /> <arg line="--exclude ${appdir}/Test,${appdir}/Vendor,$ {appdir}/Plugin" /> <arg line="--reportfile ${appdir}/reports/phpmd.xml" /> </exec> </target>
  17. ੩తղੳΛ࣮ߦ͢Δ — build.xmlΛमਖ਼(4) - PHPCPDͷઃఆΛ௥Ճ … <target name="phpcpd" description="Perform project

    mess detection using PHPMD"> <exec dir="${basedir}" executable="${vendorbindir}/phpcpd" output="${logdir}/phpcpd.log"> <arg line="--exclude Vendor" /> <arg line="--exclude Plugin" /> <arg line="--exclude webroot" /> <arg line="--log-pmd ${appdir}/reports/phpcpd.xml" /> <arg line="--names *.php,*.cpt" /> <arg line="${appdir}" /> </exec> </target>
  18. ੩తղੳΛ࣮ߦ͢Δ ! — मਖ਼Λίϛοτ $ git add build.xml $ git

    commit -m 'ΠϯεϖΫγϣϯͷ࣮ߦΛ௥Ճ' $ git push origin master
  19. ੩తղੳΛ࣮ߦ͢Δ — build.xmlΛमਖ਼(5) - PHPMDͷআ֎σΟϨΫτϦ௥Ճ … <target name="phpmd" description="Perform project

    mess detection using PHPMD"> <exec dir="${basedir}" executable="${vendorbindir}/phpmd" output="${logdir}/phpmd.log"> <arg line="${appdir}" /> <arg line="xml" /> <arg line="codesize,unusedcode,design,naming" /> <!-- ͜͜ʹઃఆΛ௥Ճ(,${appdir}/Config/Migration,${appdir}/Config/Schema) --> <arg line="--exclude ${appdir}/Test,${appdir}/Vendor,$ {appdir}/Plugin,${appdir}/Config/Migration,${appdir}/Config/Schema" /> <!- ͜͜·Ͱ --> <arg line="--reportfile ${appdir}/reports/phpmd.xml" /> </exec> </target>
  20. ੩తղੳΛ࣮ߦ͢Δ ! — मਖ਼Λίϛοτ $ git add build.xml $ git

    commit -m 'PMDͷআ֎σΟϨΫτϦΛ௥Ճ' $ git push origin master
  21. υΩϡϝϯτΛੜ੒͢Δ — build.xmlΛमਖ਼(1) … <target name="build" depends=“prepare,caketest,behat,phpcs,phpmd,phpcpd,phpdoc"/> … <!—- ҎԼΛ௥Ճ

    —> <target name="phpdoc" description="Generate Application Documentation using phpDocumentor"> <exec dir="${basedir}" executable="phpdoc" output="${logdir}/phpdoc.log" checkreturn="true"> <arg line="run" /> <arg line="-t ${appdir}/reports/doc" /> <arg line="-d ${appdir}/" /> <arg line="-e php" /> <arg line="-i ${appdir}/Vendor/,${appdir}/Plugin/,${appdir}/Test/,$ {appdir}/Config/" /> <arg line="-p" /> </exec> </target>
  22. υΩϡϝϯτΛੜ੒͢Δ ! $ git add build.xml $ git commit -m

    'υΩϡϝϯτੜ੒ͷ࣮ߦΛ௥Ճ' $ git push origin master
  23. υΩϡϝϯτΛੜ੒͢Δ app/Model/Post.phpʹҎԼͷ༷ʹίϝϯτΛ௥Ճ <?php App::uses('AppModel', 'Model'); ! /** * Post Model

    * * ϒϩάهࣄ༻ϞσϧͰ͢ * * @copyright php_ci_book * @link https://github.com/phpcibook/blogapp/blob/master/app/Model/Post.php * @since 1.0 * @auther ࡞੒ऀͷ໊લ <[email protected]> * */ class Post extends AppModel { …
  24. υΩϡϝϯτΛੜ੒͢Δ app/Model/Post.phpʹҎԼͷ༷ʹίϝϯτΛ௥Ճ class Post extends AppModel { ! /** *

    Ұཡදࣔ࣌ͷλΠτϧʹ࢖༻͢ΔΧϥϜ໊ * * @var string */ public $displayField = 'title'; ! /** * όϦσʔγϣϯϧʔϧ * * @var array */ public $validate = [
  25. υΩϡϝϯτΛੜ੒͢Δ ! $ git add app/Model/Post.php $ git commit -m

    'DocBlockίϝϯτΛ௥Ճ' $ git push origin master मਖ਼ͨ͠Βอଘͯ͠Ϗϧυ࣮ߦ
  26. ຊ൪؀ڥʹDEPLOY͢Δ $ cd /var/www/application/current $ mkdir deploy $ cd deploy

    $ cap install ! — .gitignoreʹҎԼΛ௥Ճ deploy/config/deploy/production.php CapistranoͷDeployεΫϦϓτͷͻͳܗΛ࡞੒
  27. ຊ൪؀ڥʹDEPLOY͢Δ deploy/config/deploy/production.php ! <?php ! Environment::configure('production', true, [ 'MYSQL_DB_HOST' =>

    'localhost', 'MYSQL_USERNAME' => 'webapp', 'MYSQL_PASSWORD' => 'passw0rd', 'MYSQL_DB_NAME' => 'blog', 'MYSQL_PREFIX' => '', 'debug' => 0, ], function() { }); ຊ൪αʔόʔ༻ͷઃఆϑΝΠϧΛ௥Ճ
  28. ຊ൪؀ڥʹDEPLOY͢Δ $ cd /var/www/application/current/deploy/config/deploy $ vim production.rb ! server '192.168.33.200',

    roles: %w{web app db}, user: 'vagrant', ssh_options: { password: fetch(:password) }, cake_env: "production", app_config:'production.php', group: 'vagrant' Deployઌͷઃఆ
  29. ຊ൪؀ڥʹDEPLOY͢Δ $ cd /var/www/application/current/deploy/ $ vim reploy.rb ! — 5ߦ໨ͷϦϙδτϦURLΛ֤ࣗमਖ਼

    set :repo_url, '[email protected]:phpcibook/blogapp.git' # (2) DeployεΫϦϓτΛमਖ਼ ϑΝΠϧͷৄࡉ͸ stypi_ deploy.rbࢀর