Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CIハンズオン@PHPカンファレンス福岡-開発

 CIハンズオン@PHPカンファレンス福岡-開発

B51ca7a51ae1fd06bc536fe83e6113e2?s=128

Kaz Watanabe

June 27, 2015
Tweet

Transcript

  1. $",&1)1ͰֶͿ ܧଓతΠϯςάϨʔγϣϯϋϯζΦϯ ։ൃฤ @PHPΧϯϑΝϨϯε෱Ԭ 2015/6/27

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

    Πϯϑϥߏஙɾӡ༻ • iOSΞϓϦ։ൃ #beer #iosdev #cakephp #cat #camp #golf #chef #vagrant
  3. WHO?

  4. ಈ࡞؀ڥ

  5. BeHat • ϏϔΠϏΞ(ৼ෣)ۦಈ։ൃ (BDD:Behaviour Driven Development) • ετʔϦʔϕʔεͷςεςΟϯάϑϨʔϜϫʔΫ

  6. BeHat 'FBUVSFͷهड़ྫ

  7. BeHat 'FBUVSFͷ࣮ߦྫ

  8. BeHat #%%։ൃϓϩηε

  9. ࠷ॳͷεϓϦϯτ͕ ऴΘΔॴἬϫʔϓ͠·͢

  10. ࠷ॳͷεϓϦϯτ͕ ׬ྃͨ͠ঢ়ଶΛ࡞੒ host $ vagrant ssh develop $ cd /var/www/application/current/app

    $ git merge origin/step2 $ composer install --dev
  11. DATABASEΛMigration

  12. DATABASEΛMigration $ cd /var/www/application/current/app $ Console/cake Migrations.migration run all

  13. http://192.168.33.10/posts

  14. UnitTest $ cd /var/www/application/current/app $ Console/cake Test app AllTests

  15. None
  16. StoryTest $ cd /var/www/application/current/app $ Console/cake Bdd.story

  17. None
  18. Jenkinsʹ૊ࠐΜͰ͍͖·͢ʂ

  19. ͦͷલʹ… $ git update-index --add --chmod=+x Console/cake $ git add

    -u $ git commit -m “cakeίϚϯυʹ࣮ߦݖΛ෇༩” $ git push origin master
  20. BUILDπʔϧΛಋೖ — phingΛಋೖ $ cd /var/www/application/current/app $ composer require --dev

    "phing/phing:~2.8"
  21. PHINGͷઆ໌

  22. BUILDπʔϧΛಋೖ — मਖ਼Λίϛοτ $ git add composer.json composer.lock $ git

    commit -m 'PhingΛ௥Ճ'
  23. 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
  24. BUILDπʔϧΛಋೖ — ϏϧυΛ࣮ߦ $ cd /var/www/application/current $ app/Vendor/bin/phing

  25. BUILDπʔϧΛಋೖ — मਖ਼Λίϛοτ $ git add build.xml $ git commit

    -m ‘ϏϧυϑΝΠϧΛ௥Ճ' $ git push origin master
  26. BUILDπʔϧΛಋೖ

  27. BUILDπʔϧΛಋೖ

  28. BUILDπʔϧΛಋೖ cd ${WORKSPACE}/app composer install --dev cd ${WORKSPACE} app/Vendor/bin/phing -logger

    phing.listener.DefaultLogger
  29. ϏϧυΛ࣮ߦʂ

  30. UnitTest݁ՌΛදࣔ

  31. 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>
  32. UnitTest݁ՌΛදࣔ͢Δ — मਖ਼Λίϛοτ $ git add build.xml $ git commit

    -m 'Ϣχοτςετͷ݁ՌΛϑΝΠϧ ʹग़ྗ' $ git push origin master
  33. UnitTest݁ՌΛදࣔ͢Δ

  34. UnitTest݁ՌΛදࣔ͢Δ app/reports/unittest.xml

  35. UnitTest݁ՌΛදࣔ͢Δ

  36. UnitTest݁ՌΛදࣔ͢Δ app/reports/ app/reports/coverage.xml मਖ਼ͨ͠Βอଘͯ͠Ϗϧυ࣮ߦ νΣοΫ͢Δ

  37. UnitTest݁ՌΛදࣔ͢Δ

  38. UnitTest݁ՌΛදࣔ͢Δ ConfigσΟϨΫτϦͷΧόϨοδ͕௿͍ʂ

  39. 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>
  40. UnitTest݁ՌΛදࣔ͢Δ — मਖ਼Λίϛοτ $ git add app/phpunit.xml $ git commit

    -m 'ΧόϨοδআ֎ର৅Λ௥Ճ' $ git push origin master
  41. UnitTest݁ՌΛදࣔ͢Δ ΧΠθϯͨ͠ʂ

  42. 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> <!-- ͜͜·Ͱ௥Ճ -->
  43. Behatͷ݁ՌΛදࣔ͢Δ — मਖ਼Λίϛοτ $ git add build.xml $ git commit

    -m 'BehatςετΛ௥Ճ' $ git push origin master
  44. Behatͷ݁ՌΛදࣔ͢Δ app/reports/*.xml ʹमਖ਼ मਖ਼ͨ͠Βอଘͯ͠Ϗϧυ࣮ߦ

  45. Behatͷ݁ՌΛදࣔ͢Δ

  46. ੩తղੳͷઃఆΛ͢Δ

  47. ੩తղੳͷઃఆΛ͢Δ $ 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.*"
  48. ੩తղੳͷઃఆΛ͢Δ — मਖ਼Λίϛοτ $ git add composer.json composer.lock $ git

    commit -m 'ΠϯεϖΫγϣϯπʔϧͷ௥Ճ' $ git push origin master
  49. ੩తղੳͷઃఆΛ͢Δ — 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"/> …
  50. ੩తղੳͷઃఆΛ͢Δ — 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>
  51. ੩తղੳͷઃఆΛ͢Δ — 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>
  52. ੩తղੳͷઃఆΛ͢Δ — 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>
  53. ੩తղੳͷઃఆΛ͢Δ — मਖ਼Λίϛοτ $ git add build.xml $ git commit

    -m 'ΠϯεϖΫγϣϯͷ࣮ߦΛ௥Ճ' $ git push origin master
  54. ੩తղੳͷઃఆΛ͢Δ app/reports/checkstyle.xml app/reports/phpmd.xml app/reports/phpcpd.xml

  55. ੩తղੳͷઃఆΛ͢Δ MigrationͱSchemaʹܯࠂ

  56. ੩తղੳͷઃఆΛ͢Δ — 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>
  57. ੩తղੳͷઃఆΛ͢Δ — मਖ਼Λίϛοτ $ git add build.xml $ git commit

    -m 'PMDͷআ֎σΟϨΫτϦΛ௥Ճ' $ git push origin master
  58. ੩తղੳͷઃఆΛ͢Δ ΧΠθϯͨ͠ʂ

  59. υΩϡϝϯτΛੜ੒͢Δ — 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>
  60. υΩϡϝϯτΛੜ੒͢Δ $ git add build.xml $ git commit -m 'υΩϡϝϯτੜ੒ͷ࣮ߦΛ௥Ճ'

    $ git push origin master
  61. υΩϡϝϯτΛੜ੒͢Δ

  62. υΩϡϝϯτΛੜ੒͢Δ ௥ՃΛԡͯ͠ҎԼͷΑ͏ʹઃఆ app/reports/doc/ index.html API Documentation

  63. υΩϡϝϯτΛੜ੒͢Δ

  64. υΩϡϝϯτΛੜ੒͢Δ 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 ࡞੒ऀͷ໊લ <test@example.com> * */ class Post extends AppModel { …
  65. υΩϡϝϯτΛੜ੒͢Δ app/Model/Post.phpʹҎԼͷ༷ʹίϝϯτΛ௥Ճ class Post extends AppModel { /** * Ұཡදࣔ࣌ͷλΠτϧʹ࢖༻͢ΔΧϥϜ໊

    * * @var string */ public $displayField = 'title'; /** * όϦσʔγϣϯϧʔϧ * * @var array */ public $validate = [
  66. υΩϡϝϯτΛੜ੒͢Δ $ git add app/Model/Post.php $ git commit -m 'DocBlockίϝϯτΛ௥Ճ'

    $ git push origin master मਖ਼ͨ͠Βอଘͯ͠Ϗϧυ࣮ߦ
  67. υΩϡϝϯτΛੜ੒͢Δ

  68. ຊ൪؀ڥʹDEPLOY͢Δ

  69. ຊ൪؀ڥʹDEPLOY͢Δ $ vagrant ssh deploy $ ssh-keygen อଘ৔ॴ,ύεϑϨʔζೖྗ(ۭͰվߦͰ) $ cat

    ~/.ssh/id_rsa.pub ެ։伴Λίϐʔ
  70. ຊ൪؀ڥʹDEPLOY͢Δ Add deploy keyΛΫϦοΫͯ͠ެ։伴Λ௥Ճ

  71. ຊ൪؀ڥʹDEPLOY͢Δ $ cd /var/www/application/current $ mkdir deploy $ cd deploy

    $ cap install CapistranoͷDeployεΫϦϓτͷͻͳܗΛ࡞੒
  72. ຊ൪؀ڥʹ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() { }); ຊ൪αʔόʔ༻ͷઃఆϑΝΠϧΛ௥Ճ
  73. ຊ൪؀ڥʹ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ઌͷઃఆ
  74. ຊ൪؀ڥʹDEPLOY͢Δ $ cd /var/www/application/current/deploy/ $ vim reploy.rb — 5ߦ໨ͷϦϙδτϦURLΛ֤ࣗमਖ਼ set

    :repo_url, 'git@github.com:phpcibook/blogapp.git' # (2) DeployεΫϦϓτΛमਖ਼ ϑΝΠϧͷৄࡉ͸ stypi_ deploy.rbࢀর
  75. ຊ൪؀ڥʹDEPLOY͢Δ $ cd /var/www/application/current/deploy/ $ cap production deploy Please enter

    Server password: (): vagrant
  76. ։ൃฤ Fin @kaz_29