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 time
09/07/12 http://coderinsights.blogspot.in 3
4. What is [PHing Is Not Gnumake]
• It's a PHP project build tool based on Apache Ant
• Opensource
• Mostly Cross platform
• Uses XML build files
• No required external dependencies
• Built & optimised for PHP5
09/07/12 http://coderinsights.blogspot.in 4
5. What can you do?
• Lots –Not just for deployment
• SVN tasks
• PHPUnit/SimpleTest
• Code analysis tasks
• PhpDocumentor
• Zip/Unzip
• File manipulation
• Various OS tasks
09/07/12 http://coderinsights.blogspot.in 5
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 schema
09/07/12 http://coderinsights.blogspot.in 7
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 on
09/07/12 http://coderinsights.blogspot.in 8
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 Download
09/07/12 http://coderinsights.blogspot.in 9
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.xml
09/07/12 http://coderinsights.blogspot.in 10
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. 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
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
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 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
)
09/07/12 http://coderinsights.blogspot.in 21
22. Database Migrations
• Delta scripts with do (up) & undo (down) parts
-- //
CREATE TABLE ‘post‘ (
‘title‘ VARCHAR(255),
‘time_created‘ DATETIME,
‘content‘ MEDIUMTEXT
);
-- //@UNDO
DROP TABLE ‘post‘;
-- //
09/07/12 http://coderinsights.blogspot.in 22
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
We are human We get bored We forget things We make mistakes Repetitive tasks like Versioncontrol (Unit)Testing Configuring Packaging Uploading DBchanges
Phing is Recursive acronym In its simplest form, Phing allows you to copy code from your source control repository (SVN or Git) to your server via SSH, and perform pre and post-deploy functions like restarting a webserver, busting cache, renaming files, running database migrations and so on. With Phing it’s also possible to deploy to many machines at once. Original PHP4 version by Andreas Aderhold Cross-platform(for Windows) Build Systems Apache ANT Capastrano Plain PHP or BASH or BAT Files
Interface to various popular (PHP)tools
Introduced facade targets • Moved all the properties out • Used properties for configurability • Defined and reused elements with ids • Use of reflexives and replacements • Separating build files
Other useful options: – Help (-h) – Specify properties (-D propname=value) – List targets (-l) – Get more output (-verbose or -debug)
Task : code that performs a specific function (svncheckout, mkdir,etc.) Target : groups of tasks, can optionally depend on other targets Project : root node, contains multiple targets