building and deploying php applications with phing

60
Building and deploying PHP applications with Phing Michiel Rook PHP UK Conference 2012

Upload: michiel-rook

Post on 08-May-2015

39.498 views

Category:

Technology


3 download

DESCRIPTION

Slides for my talk at the PHP UK Conference 2012. Some of the examples discussed during the talk can be found at http://www.touchdownconsulting.nl/conferences/phing-phpuk2012-examples.tgz If you attended, please leave me some feedback at http://joind.in/4954 - thanks!

TRANSCRIPT

Page 1: Building and deploying PHP applications with Phing

Building and deploying PHP applicationswith Phing

Michiel Rook

PHP UK Conference 2012

Page 2: Building and deploying PHP applications with Phing

About me

Building and deploying PHP applications with Phing

• Freelance PHP & Java contractor / consultant

• PHP since ’99

• Phing project lead

• http://www.linkedin.com/in/michieltcs

• @michieltcs

Page 3: Building and deploying PHP applications with Phing

This Talk

Building and deploying PHP applications with Phing

• Why use a build tool

• What is Phing

• Usage

• Various examples

• Extending Phing

Page 4: Building and deploying PHP applications with Phing

Why Use A Build Tool?

Page 5: Building and deploying PHP applications with Phing

Why Use A Build Tool

Building and deploying PHP applications with Phing

Repetitionhttp://www.flickr.com/photos/andrewmalone/5162632817/

Page 6: Building and deploying PHP applications with Phing

Repetition

Building and deploying PHP applications with Phing

• We are human

• We get bored

• We forget things

• We make mistakes

Page 7: Building and deploying PHP applications with Phing

Repetition

Building and deploying PHP applications with Phing

• Version control

• (Unit) Testing

• Configuring

• Packaging

• Uploading

• DB changes

• ...

Page 8: Building and deploying PHP applications with Phing

Repetition

Building and deploying PHP applications with Phing

• Version control

• (Unit) Testing

• Configuring

• Packaging

• Uploading

• DB changes

• ...

• Boring!

Page 9: Building and deploying PHP applications with Phing

Why Use A Build Tool

Building and deploying PHP applications with Phing

Automate!http://www.flickr.com/photos/patrick_h/6209981673/

Page 10: Building and deploying PHP applications with Phing

Automate!

Building and deploying PHP applications with Phing

• Developers, testers, administrators...

• Easier handover to new team members

• Improves quality

• Reduces errors

• Saves time

• Consolidate scripts, reduce technical debt

Page 11: Building and deploying PHP applications with Phing

What Is Phing

Building and deploying PHP applications with Phing

http://www.flickr.com/photos/canucksfan604/5471322484/

Page 12: Building and deploying PHP applications with Phing

What Is Phing

Building and deploying PHP applications with Phing

• PHing Is Not GNU make; it’s a PHP project build system or build toolbased on Apache Ant.

• Originally developed by Binarycloud

• Ported to PHP5 by Hans Lellelid

• 2004: my first commit

• 2009: lead

Page 13: Building and deploying PHP applications with Phing

What Can Phing Do

Building and deploying PHP applications with Phing

• Scripting using XML build files

• Human readable

• Mostly cross-platform

• Minimal dependencies

• Interface to various popular (PHP) tools

Page 14: Building and deploying PHP applications with Phing

What Can Phing Do

Building and deploying PHP applications with Phing

• Scripting using XML build files

• Human readable

• Mostly cross-platform

• Minimal dependencies

• Interface to various popular (PHP) tools

• ”Good glue”

Page 15: Building and deploying PHP applications with Phing

What Can Phing Do

Building and deploying PHP applications with Phing

Page 16: Building and deploying PHP applications with Phing

Why Use Phing

Building and deploying PHP applications with Phing

• Ant?

• Rich set of tasks

• Integration with PHP specific tools

• Allows you to stay in the PHP infrastructure

• Easy to extend

• Embed PHP code directly in the build file

Page 17: Building and deploying PHP applications with Phing

The Basics

Page 18: Building and deploying PHP applications with Phing

Installing Phing

Building and deploying PHP applications with Phing

• PEAR installation

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

• Optionally, install the documentation package

$ pear install phing/phingdocs

Page 19: Building and deploying PHP applications with Phing

Build Files

Building and deploying PHP applications with Phing

• Phing uses XML build files

• Contain standard elements

• Task: code that performs a specific function (svn checkout,mkdir, etc.)

• Target: groups of tasks, can optionally depend on other targets

• Project: root node, contains multiple targets

Page 20: Building and deploying PHP applications with Phing

Example Build File

Building and deploying PHP applications with Phing

<project name="Example" default="world"><target name="hello">

<echo>Hello</echo></target>

<target name="world" depends="hello"><echo>World!</echo>

</target></project>

Buildfile: /home/michiel/phing/simple.xml

Example > hello:

[echo] Hello

Example > world:

[echo] World!

BUILD FINISHED

Page 21: Building and deploying PHP applications with Phing

Properties

Building and deploying PHP applications with Phing

• Simple key-value files (.ini)

## build.propertiesversion=1.0

• Can be expanded by using ${key} in the build file

$ phing -propertyfile build.properties ...

<project name="Example" default="default"><target name="default">

<property file="build.properties" />

<echo>${version}</echo></target>

</project>

Page 22: Building and deploying PHP applications with Phing

Filesets

Building and deploying PHP applications with Phing

• Constructs a group of files to process

• Supported by most tasks

<fileset dir="./application" includes="**"/>

<fileset dir="./application"><include name="**/*.php" /><exclude name="**/*Test.php" />

</fileset>

• References: define once, use many

<fileset dir="./application" includes="**" id="files"/>

<fileset refid="files"/>

Page 23: Building and deploying PHP applications with Phing

Filesets

Building and deploying PHP applications with Phing

• Selectors allow fine-grained matching on certain attributes

• contains, date, file name & size, ...

<fileset dir="${dist}"><and>

<filename name="**"/><date datetime="01/01/2011" when="before"/>

</and></fileset>

Page 24: Building and deploying PHP applications with Phing

Mappers & Filters

Building and deploying PHP applications with Phing

• Transform files during copy/move/...

• Mappers

• Change filename

• Flatten directories

• Filters

• Strip comments, white space

• Replace values

• Perform XSLT transformation

• Translation (i18n)

Page 25: Building and deploying PHP applications with Phing

Mappers & Filters

Building and deploying PHP applications with Phing

<copy todir="${build}"><fileset refid="files"/><mapper type="glob" from="*.txt" to="*.new.txt"/><filterchain>

<replaceregexp><regexp pattern="\r\n" replace="\n"/><expandproperties/>

</replaceregexp></filterchain>

</copy>

Page 26: Building and deploying PHP applications with Phing

Examples

Page 27: Building and deploying PHP applications with Phing

Examples

Building and deploying PHP applications with Phing

• Version control

• Unit testing

• Packaging

• Deployment

• Database migration

• Continuous integration

Page 28: Building and deploying PHP applications with Phing

Version Control

Building and deploying PHP applications with Phing

• (CVS), SVN, Git

<svncopyusername="michiel"password="test"repositoryurl="svn://localhost/phing/trunk/"todir="svn://localhost/phing/tags/1.0"/>

<svnexportrepositoryurl="svn://localhost/project/trunk/"todir="/home/michiel/dev"/>

<svnlastrevisionrepositoryurl="svn://localhost/project/trunk/"propertyname="lastrev"/>

<echo>Last revision: ${lastrev}</echo>

Page 29: Building and deploying PHP applications with Phing

PHPUnit

Building and deploying PHP applications with Phing

• Built-in support for most configuration options

• Gathers code coverage information

• Various output formats (JUnit / Clover)

• Reporting (JUnit style)

Page 30: Building and deploying PHP applications with Phing

PHPUnit Example

Building and deploying PHP applications with Phing

• Stop the build when a test fails

<phpunit haltonfailure="true" haltonerror="true"bootstrap="my_bootstrap.php" printsummary="true"><batchtest>

<fileset dir="src"><include name="**/*Test.php"/>

</fileset></batchtest>

</phpunit>

Buildfile: /home/michiel/phpunit/build.xml

Demo > test:

[phpunit] Total tests run: 1, Failures: 1, Errors: 0,Incomplete: 0, Skipped: 0, Time elapsed: 0.00591 s

Execution of target "test" failed for the following reason:/home/michiel/phpunit/build.xml:3:44: Test FAILURE (testSayHello inclass HelloWorldTest): Failed asserting that two strings are equal.

Page 31: Building and deploying PHP applications with Phing

PHPUnit Example

Building and deploying PHP applications with Phing

• Determine which files to include in the coverage report

<coverage-setup database="reports/coverage.db"><fileset dir="src">

<include name="**/*.php"/><exclude name="**/*Test.php"/>

</fileset></coverage-setup>

• Gather code coverage and other data during the test run

<phpunit codecoverage="true"><formatter type="xml" todir="reports"/><batchtest>

<fileset dir="src"><include name="**/*Test.php"/>

</fileset></batchtest>

</phpunit>

Page 32: Building and deploying PHP applications with Phing

PHPUnit Example

Building and deploying PHP applications with Phing

• Generate some reports

<phpunitreport infile="reports/testsuites.xml"format="frames" todir="reports/tests"/>

<coverage-report outfile="reports/coverage.xml"><report todir="reports/coverage" title="Demo"/>

</coverage-report>

Page 33: Building and deploying PHP applications with Phing

Documentation

Building and deploying PHP applications with Phing

• Phing currently integrates with popular documentation tools

• DocBlox

• PhpDocumentor

• ApiGen

• Also supports r(e)ST (reStructuredText)

<docblox title="Phing API Documentation"output="docs" quiet="true"><fileset dir="../../classes">

<include name="**/*.php"/></fileset>

</docblox>

Page 34: Building and deploying PHP applications with Phing

DocBlox

Building and deploying PHP applications with Phing

Page 35: Building and deploying PHP applications with Phing

Packaging

Building and deploying PHP applications with Phing

• Create bundles or packages

• Phing supports most popular formats: tar (pear), zip, phar

<pearpkg name="demo" dir="."><fileset refid="files"/>

<option name="outputdirectory" value="./build"/><option name="description">Test package</option><option name="version" value="0.1.0"/><option name="state" value="beta"/>

<mapping name="maintainers"><element>

<element key="handle" value="test"/><element key="name" value="Test"/><element key="email" value="[email protected]"/><element key="role" value="lead"/>

</element></mapping>

</pearpkg>

Page 36: Building and deploying PHP applications with Phing

Packaging - TAR / ZIP

Building and deploying PHP applications with Phing

<tar compression="gzip" destFile="package.tgz"basedir="build"/>

<zip destfile="htmlfiles.zip"><fileset dir=".">

<include name="**/*.html"/></fileset>

</zip>

Page 37: Building and deploying PHP applications with Phing

Packaging - PHAR

Building and deploying PHP applications with Phing

<pharpackagecompression="gzip"destfile="test.phar"stub="stub.php"basedir="."><fileset dir="hello">

<include name="**/**" /></fileset><metadata>

<element name="version" value="1.0" /><element name="authors">

<element name="John Doe"><element name="e-mail"value="[email protected]" />

</element></element>

</metadata></pharpackage>

Page 38: Building and deploying PHP applications with Phing

Putting it all together - deployments

Page 39: Building and deploying PHP applications with Phing

Copying to a server

Building and deploying PHP applications with Phing

• SSH

<scp username="john" password="smith"host="webserver" todir="/www/htdocs/project/"><fileset dir="test">

<include name="*.html"/></fileset>

</scp>

• FTP

<ftpdeployhost="server01"username="john"password="smit"dir="/var/www"><fileset dir=".">

<include name="*.html"/></fileset>

</ftpdeploy>

Page 40: Building and deploying PHP applications with Phing

Symbolic links

Building and deploying PHP applications with Phing

• All releases stored in ”backup” directory

• Symlink application directory to latest release (similar to Capistrano)

• Allows for easy (code) rollbacks

<svnlastrevision repositoryurl="${deploy.svn}"property="deploy.rev"/>

<svnexport repositoryurl="${deploy.svn}"todir="/www/releases/build-${deploy.rev}"/>

<symlink target="/www/releases/build-${deploy.rev}"link="/www/current"/>

• Also on a remote server

<ssh host="webserver" command="ln -s/www/releases/build-${deploy.rev} /www/current"/>

Page 41: Building and deploying PHP applications with Phing

Multiple servers / targets

Building and deploying PHP applications with Phing

• Several deployment targets: testing, staging, production, ...

• Keep one property file per target

• Select property file based on input

<input propertyname="env"validargs="testing,staging,production">

Enter environment name</input>

<property file="${env}.properties"/>

<ssh host="${deploy.host}" command="..."/>

Page 42: Building and deploying PHP applications with Phing

Database Migration

Building and deploying PHP applications with Phing

• Set of delta SQL files (1-create-post.sql)

• Tracks current version of your db in changelog table

• Generates do and undo SQL files

CREATE 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

)

Page 43: Building and deploying PHP applications with Phing

Database Migration

Building and deploying PHP applications with Phing

• Delta scripts with do (up) & undo (down) parts

--//

CREATE TABLE ‘post‘ (‘title‘ VARCHAR(255),‘time_created‘ DATETIME,‘content‘ MEDIUMTEXT

);

--//@UNDO

DROP TABLE ‘post‘;

--//

Page 44: Building and deploying PHP applications with Phing

Database Migration

Building and deploying PHP applications with Phing

<dbdeployurl="sqlite:test.db"dir="deltas"outputfile="deploy.sql"undooutputfile="undo.sql"/>

<pdosqlexecsrc="deploy.sql"url="sqlite:test.db"/>

Buildfile: /home/michiel/dbdeploy/build.xml

Demo > 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 successfully

BUILD FINISHED

Page 45: Building and deploying PHP applications with Phing

Database Migration

Building and deploying PHP applications with Phing

-- 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 --

Page 46: Building and deploying PHP applications with Phing

Database Migration

Building and deploying PHP applications with Phing

-- Fragment begins: 1 --

DROP TABLE ‘post‘;

--//

DELETE FROM changelogWHERE change_number = 1AND delta_set = ’Main’;

-- Fragment ends: 1 --

Page 47: Building and deploying PHP applications with Phing

Phing & Jenkins

Building and deploying PHP applications with Phing

• Continuous integration

• Phing plugin

• Build periodically or after each commit

• Verify and test the build

• Deploy results

Page 48: Building and deploying PHP applications with Phing

Phing & Jenkins

Building and deploying PHP applications with Phing

Page 49: Building and deploying PHP applications with Phing

Phing & Jenkins

Building and deploying PHP applications with Phing

Page 50: Building and deploying PHP applications with Phing

Phing & Jenkins

Building and deploying PHP applications with Phing

Page 51: Building and deploying PHP applications with Phing

Demonstration

Page 52: Building and deploying PHP applications with Phing

Extending Phing

Page 53: Building and deploying PHP applications with Phing

Extending Phing

Building and deploying PHP applications with Phing

• Numerous extension points

• Tasks

• Types

• Selectors

• Filters

• Mappers

• Loggers

• ...

Page 54: Building and deploying PHP applications with Phing

Sample Task

Building and deploying PHP applications with Phing

• Extends from Task

• Contains main() method and optionally init()

• Setter method for each attribute in the build file

class SampleTask extends Task{

private $var;

public function setVar($v){

$this->var = $v;}

public function main(){

$this->log("value: " . $this->var);}

}

Page 55: Building and deploying PHP applications with Phing

Sample Task

Building and deploying PHP applications with Phing

• Use taskdef to make Phing aware of your new task

<project name="Example" default="default"><taskdef name="sample"

classpath="/dev/src"classname="tasks.my.SampleTask" />

<target name="default"><sample var="Hello World" />

</target></project>

Page 56: Building and deploying PHP applications with Phing

Ad Hoc Extension

Building and deploying PHP applications with Phing

• Define a task within your build file

<target name="main"><adhoc-task name="foo"><![CDATA[class FooTest extends Task {

private $bar;

function setBar($bar) {$this->bar = $bar;

}

function main() {$this->log("In FooTest: " . $this->bar);

}}]]></adhoc-task><foo bar="TEST"/>

</target>

Page 57: Building and deploying PHP applications with Phing

Future Improvements

Building and deploying PHP applications with Phing

• More tasks & support

• Better performance

• PHAR package (including popular dependencies)

• More documentation

• Increased test coverage

• IDE support

Page 58: Building and deploying PHP applications with Phing

Future Improvements

Building and deploying PHP applications with Phing

• More tasks & support

• Better performance

• PHAR package (including popular dependencies)

• More documentation

• Increased test coverage

• IDE support

• Pull requests! :-)

Page 59: Building and deploying PHP applications with Phing

Helpful Links

Building and deploying PHP applications with Phing

• http://pear.php.net/

• http://www.docblox-project.org/

• http://www.dbdeploy.com/

• http://www.jenkins-ci.org/

• http://www.phing.info/docs/guide/stable/

• http://github.com/phingofficial/phing

Page 60: Building and deploying PHP applications with Phing

Questions?

Building and deploying PHP applications with Phing

http://joind.in/4954

http://www.phing.info

#phing (freenode)

@phingofficial

Thank you!