2012 Oscon Integrating PHP Projects With Jenkins PDF
2012 Oscon Integrating PHP Projects With Jenkins PDF
with Jenkins
Sebastian Bergmann
OSCON July 16th 2012
Sebastian Bergmann
Has instrumentally
contributed to tranforming
PHP into a reliable platform for
large-scale, critical projects.
sharing experience
sharing experience
Continuous Integration
Software development practice where
members of a team integrate their work
frequently, usually each person integrates at
least daily leading to multiple integrations
per day. Each integration is verified by an
automated build (including test) to detect
integration errors as quickly as possible.
Martin Fowler
sharing experience
e
d
o
C
Test
Ship
http://en.wikipedia.org/wiki/File:Devops.png
sharing experience
sharing experience
sharing experience
a
e
d
I
C od e
Value
sharing experience
http://en.wikipedia.org/wiki/File:Agile-vs-iterative-flow.jpg
sharing experience
Quantum of Deployment
The smallest number of steps, with the smallest
number of people and the smallest amount of
ceremony required to get new code running on
your servers.
http://codeascraft.etsy.com/2010/05/20/quantum-of-deployment
sharing experience
Continuous Delivery
http://en.wikipedia.org/wiki/File:Continuous_Delivery_process_diagram.png
sharing experience
Code
Automated Tests
Feature Branches
Automation
Build
Deployment
Continuous Integration
sharing experience
Continuous Integration
Reduce risks
sharing experience
Jenkins
Open Source
Extendable
* http://jenkins-ci.org/
sharing experience
Installing Jenkins
mkdir /usr/local/jenkins
cd /usr/local/jenkins
wget http://mirrors.jenkins-ci.org/war-stable/latest/jenkins.war
export JENKINS_HOME=/usr/local/jenkins
java -jar jenkins.war
sharing experience
sharing experience
Build
A build acts as the process for putting source
code together and verifying that the software
works as a cohesive unit.
Paul M. Duvall
sharing experience
Build Automation
Build automation is the act of scripting or
automating a wide variety of tasks that software
developers do in their day-to-day activities
including compiling, packaging, running tests,
deployment to production
http://en.wikipedia.org/wiki/Build_Automation
sharing experience
What's in a build?
(Compilation)
Automated Tests
Code Analysis
Documentation Generation
Packaging / Deployment
...
sharing experience
Autoloader
Object-Relational Mapper
PHPAB*
Model PHP and SQL code
Framework
Scaffolding
* http://github.com/theseer/Autoload
sharing experience
sharing experience
sharing experience
sharing experience
Compilation
HipHop
Syntax Check
php -l*
* http://www.php.net/manual/en/features.commandline.options.php
sharing experience
Syntax Check
<project name="my-project" default="build">
<target name=build" depends="prepare,lint" />
<!-- ... -->
<target name="lint">
<apply executable="php" failonerror="true">
<arg value="-l" />
<fileset dir="${basedir}/src">
<include name="**/*.php" />
</fileset>
<fileset dir="${basedir}/tests">
<include name="**/*.php" />
</fileset>
</apply>
</target>
</project>
sharing experience
Syntax Check
<project name="my-project" default="build">
<target name=build" depends="prepare,lint" />
<!-- ... -->
<target name="lint">
<apply executable="php" failonerror="true">
<arg value="-l" />
<fileset dir="${basedir}/src">
<include name="**/*.php" />
<modified />
</fileset>
<fileset dir="${basedir}/tests">
<include name="**/*.php" />
<modified />
</fileset>
</apply>
</target>
</project>
sharing experience
PHPUnit*
Logfiles
* http://phpun.it/
sharing experience
PHPUnit
<project name="my-project" default="build">
<target name=build" depends="prepare,lint,phpunit" />
<target name=clean">
<delete dir="${basedir}/build/coverage"/>
<delete dir="${basedir}/build/logs"/>
</target>
<target name=prepare" depends="clean,phpab">
<mkdir dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/logs"/>
</target>
<target name="phpunit" description="Run unit tests with PHPUnit">
<exec executable="phpunit" failonerror="true"/>
</target>
</project>
sharing experience
PHPUnit
<phpunit bootstrap="src/autoload.php">
<testsuite name="my-project">
<directory suffix="Test.php">tests</directory>
</testsuite>
<logging>
<log type="coverage-html" target="build/coverage" />
<log type="coverage-clover" target="build/logs/clover.xml" />
<log type="junit" target="build/logs/junit.xml" />
</logging>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<file>src/autoload.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/xUnit+Plugin
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/Clover+PHP+Plugin
sharing experience
PHPLOC*
Logfile: CSV
* http://github.com/sebastianbergmann/phploc
sharing experience
PHPLOC
Directories:
Files:
Lines of Code (LOC):
Cyclomatic Complexity / Lines of Code:
Comment Lines of Code (CLOC):
Non-Comment Lines of Code (NCLOC):
Namespaces:
Interfaces:
Traits:
Classes:
Abstract:
Concrete:
Average Class Length (NCLOC):
Methods:
Scope:
Non-Static:
Static:
Visibility:
Public:
Non-Public:
Average Method Length (NCLOC):
Cyclomatic Complexity / Number of Methods:
11
22
601
0.04
7
594
11
1
0
20
1 (5.00%)
19 (95.00%)
22
38
38 (100.00%)
0 (0.00%)
26 (68.42%)
12 (31.58%)
11
1.58
Anonymous Functions:
Functions:
1
0
Constants:
Global constants:
Class constants:
0
0
0
sharing experience
PHPLOC
<target name="phploc">
<exec executable="phploc">
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
<arg path="${basedir}/src" />
</exec>
</target>
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/Plot+Plugin
sharing experience
PHP_CodeSniffer*
* http://pear.php.net/package/PHP_CodeSniffer
sharing experience
PHP_CodeSniffer
PHP CODE SNIFFER VIOLATION SOURCE SUMMARY
-------------------------------------------------------------------------------STANDARD CATEGORY
SNIFF
COUNT
-------------------------------------------------------------------------------CodeRevi Functions
Global function found
2297
CodeRevi PHP
Global keyword not allowed
938
Generic
PHP
No silenced errors discouraged
523
CodeRevi PHP
Forbidden functions found
77
Generic
Code analysis
For loop with test function call not allowe 53
Generic
Code analysis
Empty statement not allowed warning
34
Generic
PHP
Deprecated functions found
28
Generic
Code analysis
Useless overriding method found
4
Generic
Classes
Duplicate class name found
2
Generic
Code analysis
Unconditional if statement found
1
-------------------------------------------------------------------------------A TOTAL OF 3957 SNIFF VIOLATION(S) WERE FOUND IN 10 SOURCE(S)
-------------------------------------------------------------------------------Time: 08:02, Memory: 1750.25Mb
sharing experience
PHP_CodeSniffer
FILE: /tmp/wordpress/wp-includes/admin-bar.php
-------------------------------------------------------------------------------FOUND 7 ERROR(S) AND 16 WARNING(S) AFFECTING 23 LINE(S)
-------------------------------------------------------------------------------18 | WARNING | Consider refactoring "_wp_admin_bar_init" to avoid global
|
| functions.
19 | ERROR
| Use of the "global" keyword is forbidden
52 | WARNING | Consider refactoring "wp_admin_bar_render" to avoid global
|
| functions.
53 | ERROR
| Use of the "global" keyword is forbidden
78 | WARNING | Consider refactoring "wp_admin_bar_my_account_menu" to avoid
|
| global functions.
79 | ERROR
| Use of the "global" keyword is forbidden
101 | WARNING | Consider refactoring "wp_admin_bar_dashboard_view_site_menu"
|
| to avoid global functions.
119 | WARNING | Consider refactoring "wp_admin_bar_my_sites_menu" to avoid
|
| global functions.
120 | ERROR
| Use of the "global" keyword is forbidden
154 | WARNING | Consider refactoring "wp_admin_bar_shortlink_menu" to avoid
|
| global functions.
176 | WARNING | Consider refactoring "wp_admin_bar_edit_menu" to avoid global
|
| functions.
.
.
.
sharing experience
PHP_CodeSniffer
<target name="phpcs">
<exec executable="phpcs" output="/dev/null">
<arg value="--report=checkstyle" />
<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
<arg value="--standard=${basedir}/build/phpcs.xml" />
<arg value="--ignore=autoload.php" />
<arg path="${basedir}/src" />
</exec>
</target>
sharing experience
PHP_CodeSniffer
<ruleset name="name-of-your-coding-standard">
<description>Description of your coding standard</description>
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<!-- ... -->
</ruleset>
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin
sharing experience
* http://github.com/sebastianbergmann/phpcpd
sharing experience
sharing experience
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/DRY+Plugin
sharing experience
PHP_Depend*
* http://pdepend.org/
sharing experience
PHP_Depend
pdepend --overview-pyramid=overview_pyramid.svg src
PHP_Depend 1.0.7 by Manuel Pichler
Parsing source files:
......................
22
Executing Coupling-Analyzer:
.....
108
Executing CyclomaticComplexity-Analyzer:
.....
105
Executing Inheritance-Analyzer:
.
36
Executing NodeCount-Analyzer:
...
70
Executing NodeLoc-Analyzer:
....
91
sharing experience
PHP_Depend
ANDC
AHH
NOP
NOC
NOM
LOC
CYCLO
CALLS
FANOUT
PHP_Depend
pdepend --jdepend-chart=abstraction_instability.svg src
PHP_Depend 1.0.7 by Manuel Pichler
Parsing source files:
......................
22
Executing Dependency-Analyzer:
...
70
sharing experience
PHP_Depend
sharing experience
PHP_Depend
pdepend --jdepend-xml=jdepend.xml src
PHP_Depend 1.0.7 by Manuel Pichler
Parsing source files:
......................
22
Executing Dependency-Analyzer:
...
70
sharing experience
PHP_Depend
sharing experience
PHP_Depend
pdepend --summary-xml=summary.xml src
PHP_Depend 1.0.7 by Manuel Pichler
Parsing source files:
......................
Executing CyclomaticComplexity-Analyzer:
.....
22
105
Executing ClassLevel-Analyzer:
....
85
Executing CodeRank-Analyzer:
.
32
Executing Cohesion-Analyzer:
.......
154
Executing Coupling-Analyzer:
.....
108
Executing Hierarchy-Analyzer:
....
87
Executing Inheritance-Analyzer:
.
36
Executing NPathComplexity-Analyzer:
.....
105
Executing NodeCount-Analyzer:
...
70
Executing NodeLoc-Analyzer:
....
91
sharing experience
Cyclomatic Complexity
Number of possible decision paths in a program or
program unit
Thomas J. McCabe, A Complexity Measure,
IEEE Transactions on Software Engineering 2, No. 4
(IEEE Computer Society Press, Los Alamitos, CA, USA,
1976).
sharing experience
Cyclomatic Complexity
inspect.php summary.xml --metric0 ccn
=================================================================
Name
| Value
=================================================================
BankAccountMapper::findById()
| 4.0000
Router::route()
| 4.0000
BankAccountController::execute()
| 3.0000
Request::__call()
| 3.0000
ControllerFactory::getController()
| 3.0000
BankAccount::setBalance()
| 2.0000
MapperFactory::getMapper()
| 2.0000
BankAccountMapper::getAllIds()
| 2.0000
BankAccountMapper::insert()
| 2.0000
BankAccountMapper::delete()
| 2.0000
BankAccountMapper::update()
| 2.0000
BankAccountListView::render()
| 2.0000
HashMap::get()
| 2.0000
BankAccount::depositMoney()
| 1.0000
.
.
.
sharing experience
NPath Complexity
Number of acyclic execution paths in a program or
program unit
Brian A. Nejmeh, NPATH: A Measure of Execution
Path Complexity and its Applications,
Communications of the ACM 31, Issue 2
(February 1988): 188200. ISSN 0001-0782.
sharing experience
NPath Complexity
inspect.php summary.xml --metric0 npath
=================================================================
Name
| Value
=================================================================
Router::route()
| 8.0000
Request::__call()
| 6.0000
BankAccountMapper::findById()
| 6.0000
BankAccountController::execute()
| 4.0000
ControllerFactory::getController()
| 3.0000
BankAccountMapper::getAllIds()
| 2.0000
BankAccountListView::render()
| 2.0000
BankAccount::setBalance()
| 2.0000
MapperFactory::getMapper()
| 2.0000
BankAccountMapper::update()
| 2.0000
BankAccountMapper::delete()
| 2.0000
BankAccountMapper::insert()
| 2.0000
HashMap::get()
| 2.0000
BankAccount::withdrawMoney()
| 1.0000
.
.
.
sharing experience
PHP_Depend
<target name="pdepend">
<exec executable="pdepend">
<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
<arg path="${basedir}/src" />
</exec>
</target>
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/JDepend+Plugin
sharing experience
* http://phpmd.org/
sharing experience
sharing experience
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/PMD+Plugin
sharing experience
PHP_CodeSniffer
* http://wiki.jenkins-ci.org/display/JENKINS/Violations
sharing experience
PHP_CodeBrowser*
* http://github.com/mayflower/PHP_CodeBrowser
sharing experience
PHP_CodeBrowser
<target name="phpcb">
<exec executable="phpcb">
<arg value="--log" />
<arg path="${basedir}/build/logs" />
<arg value="--source" />
<arg path="${basedir}/src" />
<arg value="--output" />
<arg path="${basedir}/build/code-browser" />
</exec>
</target>
sharing experience
* http://wiki.jenkins-ci.org/display/JENKINS/HTML+Publisher+Plugin
sharing experience
Jenkins PHP*
* http://jenkins-php.org/
sharing experience
Deployment
Version of PHP
PHP Extensions
Database
...
sharing experience
Package Management
PEAR Installer
sharing experience
sharing experience
RPM/DEB/... Packages
Configuration
sharing experience
RPM/DEB/... Packages
Reproducible deployment
Redeployable
Reversible
Automatable
Single Machine
Multiple Machines
sharing experience
Automated Deployment
Build package
sharing experience
sharing experience
Chef
Puppet
...
sharing experience
Web
http://thePHP.cc/
http://Sebastian-Bergmann.de/
Twitter @S_Bergmann
Slides http://talks.thePHP.cc/
sharing experience