Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Build Automation of PHP Applications

7,012 views

Published on

Intro to build automation using phing.

Published in: Technology

Build Automation of PHP Applications

  1. 1. Phing Automating PHP build deployment09/07/12 http://coderinsights.blogspot.in 1
  2. 2. Old Way09/07/12 http://coderinsights.blogspot.in 2
  3. 3. Why use Build Automation?“We are human, We get bored,We forget things, We make mistakes”• Improve product quality• Consolidate scripts• Eliminate repetitive tasks• Minimize error (bad builds)• Eliminate dependencies (Easier handover)• Highly extendible• Saves time09/07/12 http://coderinsights.blogspot.in 3
  4. 4. What is [PHing Is Not Gnumake]• Its a PHP project build tool based on Apache Ant• Opensource• Mostly Cross platform• Uses XML build files• No required external dependencies• Built & optimised for PHP509/07/12 http://coderinsights.blogspot.in 4
  5. 5. What can you do?• Lots –Not just for deployment• SVN tasks• PHPUnit/SimpleTest• Code analysis tasks• PhpDocumentor• Zip/Unzip• File manipulation• Various OS tasks09/07/12 http://coderinsights.blogspot.in 5
  6. 6. Features09/07/12 http://coderinsights.blogspot.in 6
  7. 7. Phing Philosophy• Build scripts contains "Targets" – Targets should be small and specialized. – Example Targets: • clean – Clear temporary and cached files • copy – Copy files to their intended destination • migrate – Upgrade the database schema09/07/12 http://coderinsights.blogspot.in 7
  8. 8. Phing Philosophy• Targets can have dependencies – Target "live" can depend on clean, copy, and migrate – Meaning, when we run the "live" target, it first runs clean, copy, then migrate • And any tasks they depend on09/07/12 http://coderinsights.blogspot.in 8
  9. 9. Installing Phing• pear channel-discover pear.phing.info• pear install phing/Phing• Want all the dependencies? – pear config-set preferred_state alpha – pear install –alldeps phing/Phing – pear config-set preferred_state stable – pear install phing/phingdocs• Also available from: – SVN – Zip Download09/07/12 http://coderinsights.blogspot.in 9
  10. 10. Running Phing• $> phing –v – Lists Phing version• Phing expects to find a file "build.xml" in the current directory – build.xml defines the targets – You can use the "-f <filename>" flag to specify an alternate build file like • $> phing -f build-live.xml09/07/12 http://coderinsights.blogspot.in 10
  11. 11. Syntax• Build File uses XML• Standard Elements – Task: code that performs a specific function – Target: groups of tasks – Project: root node• Variables – ${variablename} – Conventions • Psuedo-Namespaces using periods • ${namespace.variable.name}09/07/12 http://coderinsights.blogspot.in 11
  12. 12. Basic Conventions• build.xml – Central repository of your top-level tasks• build-*.xml – Can be included into build.xml. – Usually for grouping by target (dev, staging,prod) or task (migrate, test, etc.)• build.properties – Technically an INI file that contains variables to be included by build files.09/07/12 http://coderinsights.blogspot.in 12
  13. 13. Basic build.xml <?xml version="1.0" encoding="UTF-8"?> <project name=“opx" default="default" basedir="."> <property file="build.properties" /> <target name="default"> <echo message="Hello World!" /> </target> </project>09/07/12 http://coderinsights.blogspot.in 13
  14. 14. Basic build.properties source.directory = /mnt/work/ ## Database Configuration db.user = username db.pass = password db.host = localhost db.name = database Usage: $phing –propertyfile build.properties09/07/12 http://coderinsights.blogspot.in 14
  15. 15. Most Useful Elements• Filesets: define once,use many <fileset id="src_crons" dir="${dir.crons_path}/statistic-engine/"> <include name="*.php"/> <exclude name="populate_opx_tables.php" /> </fileset>• Mappers & Filters: Transform files during copy/move/… <copy todir="${build}"> <fileset refid="files"/> <mapper type="glob" from="*.txt" to="*.new.txt"/> <filterchain> <replaceregexp> <regexp pattern="rn" replace="n"/> <expandproperties/> </replaceregexp> </filterchain> </copy>09/07/12 http://coderinsights.blogspot.in 15
  16. 16. Executing External Tools• Nearly all file transfer tools will be external commands• For this we need the Exec task <exec command="cp file1 file2" />09/07/12 http://coderinsights.blogspot.in 16
  17. 17. ExamplesSVN, Git<svncopy username=“pavan“ password="test” repositoryurl="svn://localhost/phing/trunk/“ todir="svn://localhost/phing/tags/1.0"/><svnexport repositoryurl="svn://localhost/project/trunk/” todir="/home/pavan/dev"/><svnlastrevision repositoryurl="svn://localhost/project/trunk/” propertyname="lastrev"/><echo>Last revision: ${lastrev}</echo><svnlastrevision repositoryurl="${deploy.svn}” property="deploy.rev"/><svnexport repositoryurl="${deploy.svn}" todir="/www/releases/build-${deploy.rev}"/>09/07/12 http://coderinsights.blogspot.in 17
  18. 18. ExamplesPackaging – Tar/Zip<tar compression="gzip" destFile="package.tgz" basedir="build"/> <zip destfile="htmlfiles.zip"> <fileset dir="."> <include name="**/*.html"/> </fileset></zip>09/07/12 http://coderinsights.blogspot.in 18
  19. 19. ExamplesDocumentation: – DocBlox – PhpDocumentor – ApiGen<docblox title="Phing API Documentation"output="docs" quiet="true"><fileset dir="../../classes"> <include name="**/*.php"/></fileset></docblox>09/07/12 http://coderinsights.blogspot.in 19
  20. 20. Examples• SCP/FTP<scp username="john" password="smith" host="webserver" todir="/www/htdocs/project/"> <fileset dir="test"> <include name="*.html"/> </fileset></scp><ftpdeploy host="server01” username="john" password="smit" dir="/var/www"> <fileset dir="."> <include name="*.html"/> </fileset></ftpdeploy>09/07/12 http://coderinsights.blogspot.in 20
  21. 21. Database Migrations• Set of delta SQL files (1-create-post.sql)• Tracks current version of your db in changelog table• Generates do and undo SQL filesCREATE TABLE changelog (change_number BIGINT NOT NULL,delta_set VARCHAR(10) NOT NULL,start_dt TIMESTAMP NOT NULL,complete_dt TIMESTAMP NULL,applied_by VARCHAR(100) NOT NULL,description VARCHAR(500) NOT NULL)09/07/12 http://coderinsights.blogspot.in 21
  22. 22. Database Migrations• Delta scripts with do (up) & undo (down) parts-- //CREATE TABLE ‘post‘ (‘title‘ VARCHAR(255),‘time_created‘ DATETIME,‘content‘ MEDIUMTEXT);-- //@UNDODROP TABLE ‘post‘;-- //09/07/12 http://coderinsights.blogspot.in 22
  23. 23. Database Migrations<dbdeployurl="sqlite:test.db"dir="deltas"outputfile="deploy.sql"undooutputfile="undo.sql"/><pdosqlexecsrc="deploy.sql"url="sqlite:test.db"/>Buildfile: /home/pavan/dbdeploy/build.xmlDemo > migrate:[dbdeploy] Getting applied changed numbers from DB:mysql:host=localhost;dbname=demo[dbdeploy] Current db revision: 0[dbdeploy] Checkall:[pdosqlexec] Executing file: /home/michiel/dbdeploy/deploy.sql[pdosqlexec] 3 of 3 SQL statements executed successfullyBUILD FINISHED09/07/12 http://coderinsights.blogspot.in 23
  24. 24. Database Migrations-- Fragment begins: 1 --INSERT INTO changelog(change_number, delta_set, start_dt, applied_by, description)VALUES (1, ’Main’, NOW(), ’dbdeploy’,’1-create_initial_schema.sql’);--//CREATE TABLE ‘post‘ (‘title‘ VARCHAR(255),‘time_created‘ DATETIME,‘content‘ MEDIUMTEXT);UPDATE changelogSET complete_dt = NOW()WHERE change_number = 1AND delta_set = ’Main’;-- Fragment ends: 1 --09/07/12 http://coderinsights.blogspot.in 24
  25. 25. Database Migrations-- Fragment begins: 1 --DROP TABLE ‘post‘;--//DELETE FROM changelogWHERE change_number = 1AND delta_set = ’Main’;-- Fragment ends: 1 --09/07/12 http://coderinsights.blogspot.in 25
  26. 26. Phing Way09/07/12 http://coderinsights.blogspot.in 26
  27. 27. Pitfalls• Cleanup Deleted Source Files – Usually only a problem when you have * include patterns• Undefined properties not raised as an error• Little to no IDE support (Minimal support using Ant Plugin for Eclipse)09/07/12 http://coderinsights.blogspot.in 27
  28. 28. More!!SAMPLEhttps://bitbucket.org/arnavawasthi/php-phingWEBSITEhttp://phing.infoMAILING LISTSusers@phing.tigris.orgdev@phing.tigris.org09/07/12 http://coderinsights.blogspot.in 28

×