Building and Deploying PHP Apps Using phing

42
HOW TO DEPLOY PHP APPLICATIONS SAFELY, EFFICIENTLY, AND FREQUENTLY WITHOUT LOOSING YOUR SANITY (COMPLETELY) Created by Mihail Irintchev Head of Software Development SiteGround.com Web Hosting Co. / [email protected] @irintchev

description

The focus of the presentation is on organizing your PHP app build process, employing continuous testing, JS testing, automatic documentation, software metrics and other tools. The end result is expected to be a more stable, reliable, documented and healthy code base.

Transcript of Building and Deploying PHP Apps Using phing

Page 1: Building and Deploying PHP Apps Using phing

HOW TO DEPLOY PHP APPLICATIONSSAFELY, EFFICIENTLY, AND FREQUENTLY

WITHOUT LOOSING YOUR SANITY (COMPLETELY)Created by Mihail Irintchev

Head of Software Development

SiteGround.com Web Hosting Co.

/ [email protected] @irintchev

Page 2: Building and Deploying PHP Apps Using phing

ABOUT MEName: Mihail Irintchev (Mike)From Sofia, BulgariaWeb developer since 2003Work @ SiteGroundOrganizing @bgphp UGLove beer

Page 3: Building and Deploying PHP Apps Using phing

MY USUAL ACTIVITIES

Page 4: Building and Deploying PHP Apps Using phing

MY CAR

Page 5: Building and Deploying PHP Apps Using phing

MY OFFICE

Page 6: Building and Deploying PHP Apps Using phing

WHAT IS YOUR DEPLOYMENT PROCESS LIKE?

Page 7: Building and Deploying PHP Apps Using phing

OVERVIEW OF BUILD ESSENTIALS:Revision control systemContinuous testingContinuous integrationStaging environmentAutomatic documentationCode health metricsDeployment mechanism

... and you want all that plus more wrapped in ...

AN AUTOMATED BUILD PROCEDURE!

Page 8: Building and Deploying PHP Apps Using phing

REVISION CONTROL SYSTEMS

Page 11: Building and Deploying PHP Apps Using phing

BASIC DISTINCTION BETWEENVERSIONING SYSTEMS

Local data model Distributed model

SVN GIT

CVS Bazaar

darcs

Page 12: Building and Deploying PHP Apps Using phing

BASIC DISTINCTION BETWEEN MODELS

Page 13: Building and Deploying PHP Apps Using phing

WHAT CAN THE VERSION CONTROLDO FOR YOU?

(APART FROM STORING AND VERSIONING YOUR CODE)

pre-commit and post-commit hooks

Page 14: Building and Deploying PHP Apps Using phing

WHAT IS PRE-COMMIT GOOD FOR?PREVENT PEOPLE FROM COMMITTING BAD CODE

Run php lint (php -l) on the newly committed code

for i in $SVNLOOK changed -t "$TXN" "$REPOS" | $AWK '{print $2}'do if [[ $i =~ "\.php$" ]]; then # Run php lint to make sure no code with parse errors is committed CHECK=$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHP -d html_errors=off -l || echo $i RETURN=echo $CHECK | $GREP "No syntax" > /dev/null && echo TRUE || echo FALSE if [ $RETURN = 'FALSE' ]; then echo $CHECK 1>&2; exit 1 fi fidone

Page 15: Building and Deploying PHP Apps Using phing

ENFORCE CODE STANDARDSRun PHP_CodeSniffer (phpcs)Catch any serious violations (use levels)

<?phpecho "Hello world!";

#phpcs hello.php

FILE: /home/madasha/tmp/hello.php--------------------------------------------------------------------------------FOUND 1 ERROR(S) AFFECTING 1 LINE(S)-------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment--------------------------------------------------------------------------------

Time: 16 ms, Memory: 2.50Mb

Page 16: Building and Deploying PHP Apps Using phing

PREVENT SOME FOOLISH MISTAKES... like dumping DB user and pass on your home page... or embarass yourself by leaving some othervar_dump() / print_r() / var_export() behind

Page 17: Building and Deploying PHP Apps Using phing

THE "GOLDEN" RULE

Page 18: Building and Deploying PHP Apps Using phing

SO JUST ADD ANOTHER SECTION TO THE PRE-COMMIT HOOKif [[ $i =~ "\.php$" ]]; then # ...

# Grep for var_dump/var_export/print_r (the last two without second param passed) # to prevent code committed with debugging instructions to go into the repo CHECK=$SVNLOOK cat -t "$TXN" "$REPOS" $i | \ $GREP -e "\(var_dump[ \t]*(\|\(var_export\|print_r\)[ \t]*([,)]\+)\)" -m1 -c RETURN=echo $CHECK | $GREP "1" > /dev/null && echo TRUE || echo FALSE if [ $RETURN = 'TRUE' ]; then echo "var_dump/print_r/var_export found in $i" 1>&2; exit 2 fi

# ... fi

Page 19: Building and Deploying PHP Apps Using phing

ANYTHING ELSE?YOUR IMAGINATION IS PRETTY MUCH THE BOUNDARY

Page 20: Building and Deploying PHP Apps Using phing

WHAT IS POST-COMMIT GOOD FOR?Automated loggingNotifications

Page 21: Building and Deploying PHP Apps Using phing

THERE ARE OTHER HOOKS AS WELLSVN GIT

pre-lockpre-unlockpost-lockpost-unlockstart-commitpre-revprop-changepost-revprop-change

prepare-commit-msgpost-mergepre-receivepost-receivepost-checkoutpre-applypatchpost-applypatch

Page 22: Building and Deploying PHP Apps Using phing

THE BUILD SOFTWARE

Page 23: Building and Deploying PHP Apps Using phing

WHAT DOES PHING STAND FOR?PHing Is Not GNU make

“It's a PHP project build system or build toolbased on . You can do anythingwith it that you could do with a traditional

build system like GNU make...”

Apache Ant

Page 24: Building and Deploying PHP Apps Using phing

BUT WHY PHING?

Page 25: Building and Deploying PHP Apps Using phing

SERIOUSLY, WHY PHING?Very flexible and robust build toolSimple XML build fileVariety of built-in and optional tasksVery easy to write your own custom taskGood and communityPlatform independentNo required external dependencies

docs

Page 26: Building and Deploying PHP Apps Using phing

VERY EASY INSTALLATIONThrough PEAR:

$ pear channel-discover pear.phing.info$ pear install [--alldeps] phing/phing

Through Composer:

{ "require-dev": { "phing/phing": "2.*" }}

Page 27: Building and Deploying PHP Apps Using phing

A SAMPLE XML BUILD FILE

<?xml version="1.0" encoding="UTF-8"?><project name="FooBar" default="dist"> <!-- ============================================ --> <!-- Target: prepare --> <!-- ============================================ --> <target name="prepare"> <echo msg="Making directory ./build" /> <mkdir dir="./build" /> </target> <!-- ============================================ --> <!-- Target: build --> <!-- ============================================ --> <target name="build" depends="prepare"> <echo msg="Copying files to build directory..." /> <echo msg="Copying ./about.php to ./build directory..." /> <copy file="./about.php" tofile="./build/about.php" /> <echo msg="Copying ./browsers.php to ./build directory..." /> <copy file="./browsers.php" tofile="./build/browsers.php" /> <echo msg="Copying ./contact.php to ./build directory..." /> <copy file="./contact.php" tofile="./build/contact.php" /> </target> <!-- ============================================ --> <!-- (DEFAULT) Target: dist -->

Page 28: Building and Deploying PHP Apps Using phing

TASK #1: UPDATE FROM SVN

<!-- ============================================================ --><!-- Target: svn_update - updates from svn, prints last revision --><!-- ============================================================ --><target name="svn_update"> <svnupdate svnpath="/usr/bin/svn" nocache="true" username="${svn_user}" password="${svn_pass}" todir="${srcdir}" />

<svnlastrevision svnpath="/usr/bin/svn" workingcopy="${srcdir}" propertyname="svn.lastrevision" /> <echo msg="Updated ${srcdir} to revision ${svn.lastrevision}" /></target>

Page 29: Building and Deploying PHP Apps Using phing

TASK #2: UNIT-TESTSWHAT IS YOUR PHP UNIT-TESTING PREFERRED FRAMEWORK?

By Sebastian Bergmann

Page 30: Building and Deploying PHP Apps Using phing

TASK #2: RUN UNIT-TESTS IN PHING

<!-- ===================================================================== --><!-- Target: phpunit - a subtask that runs PHPUnit on phpunit tests/suits --><!-- ===================================================================== --><target name="phpunit"> <echo msg="Running PHPUnit tests after SVN update:" /> <phpunit haltonfailure="true" haltonerror="true" bootstrap="bootstrap.php"> <formatter type="plain" usefile="false" /> <batchtest> <fileset dir="tests"> <include name="*Test.php"/> </fileset> </batchtest> </phpunit></target>

Page 31: Building and Deploying PHP Apps Using phing

TASK #3: JS UNIT-TESTSYour client-side code can be just as crucial as the server-side

one, so why not test it continuously as well?

THE TOOLS?

Page 32: Building and Deploying PHP Apps Using phing

TASK #3: RUN QUNIT TESTS IN PHING

<path id="project.class.path"> <pathelement dir="./lib/" /></path>

<taskdef name="qunit" classname="QunitTask"> <classpath refid="project.class.path" /></taskdef>

<target name="qunit" description="JavaScript Unit Test"> <qunit executable="/usr/bin/phantomjs" haltonfailure="true" runner="lib/run-qunit.js"> <fileset dir="."> <include name="jstests/runner.htm" /> </fileset> </qunit></target>

The example above is using by MartinJonsson

Qunit Phing Task

Page 33: Building and Deploying PHP Apps Using phing

TASK #4: AUTOMATIC DOCUMENTATIONThe tool: phpDocumentor 2

<phpdoc2 title="Gallery Documentation" destdir="docs" template="responsive-twig"> <fileset dir="./classes"> <include name="**/*.php" /> </fileset></phpdoc2>

Page 34: Building and Deploying PHP Apps Using phing

CODE METRICS TASKSTHE TOOLS?

phpmdphpcpdpdependphpcs

Guess what? There exist ready-made phing tasks for all ofthem!

Page 35: Building and Deploying PHP Apps Using phing

TASKS #5: PHPMDPerson behind: Manuel Pichler

<!-- ===================================================================== --><!-- Target: phpmd - a subtask that runs PHPMD on predefined dirs/files --><!-- and generates reports in the desired format --><!-- ===================================================================== --><target name="phpmd"> <phpmd rulesets="cleancode,design,unusedcode"> <fileset dir="${srcdir}"> <include name="classes/**/*.php" /> </fileset> <formatter type="xml" outfile="${srcdir}/reports/pmd.xml"/> </phpmd></target>

Page 36: Building and Deploying PHP Apps Using phing

TASK #6: PHPCPDPerson behind: Sebastian Bergmann

<!-- ===================================================================== --><!-- Target: phpcpd - a subtask that runs PHPCPD on predefined dirs/files --><!-- and generates reports in the desired format --><!-- ===================================================================== --><target name="phpcpd"> <phpcpd> <fileset dir="${srcdir}"> <include name="classes/**/*.php" /> </fileset> <formatter type="pmd" outfile="${srcdir}/reports/pmd-cpd.xml"/> </phpcpd></target>

Page 37: Building and Deploying PHP Apps Using phing

TASK #7: PDEPENDPerson behind: Manuel Pichler

<!-- ===================================================================== --><!-- Target: pdepend - a subtask that runs pdepend on predefined dirs/files--><!-- and generates reports in the desired format --><!-- ===================================================================== --><target name="pdepend"> <phpdepend> <fileset dir="${srcdir}"> <include name="classes/**/*.php" /> </fileset>

<logger type="jdepend-xml" outfile="${srcdir}/reports/pdepend.xml"/> <logger type="jdepend-chart" outfile="${srcdir}/reports/pchart.svg"/> <logger type="overview-pyramid" outfile="${srcdir}/reports/pyramid.svg"/> <analyzer type="coderank-mode" value="method"/> </phpdepend></target>

Page 38: Building and Deploying PHP Apps Using phing

WHAT ELSE CAN YOU USE PHING FOR?Encrypting/obfuscating code before deploymentGenerating static assetsGenerate and deploy static sites with tools like sculpinExecute post-deploy scriptsPretty much anything useful around the build/deploy

Page 39: Building and Deploying PHP Apps Using phing

TAKEAWAYSThere are a lot of things you can automate in a build(strong emphasis on continuous testing & integration)

There are a lot of tools out there that can help you do that

phing is a very neat tool to organize your build process

Page 40: Building and Deploying PHP Apps Using phing

CREDITS & REFERENCES"Quality Assurance for PHP Projects" by Michelangelo van Dam

by Jordan Kasper"Browser Eyeballing != JavaScript Testing"

The PHP Quality Assurance Toolchain

Page 41: Building and Deploying PHP Apps Using phing

QUESTIONS?

Page 42: Building and Deploying PHP Apps Using phing

THANK YOU!

[email protected]

@irintchev