Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

With the simple example below, Jenkins will run phing command to run scripts defined in build.xml file in your project called Nation.


Prerequisite


I assume that Apache, git and GitHub SSH connection is enabled on the server. On top of that you need to run commands below.


$ sudo apt-get install php5
$ sudo apt-get install libapache2-mod-php5
$ sudo apt-get install python-software-properties
$ sudo apt-get install php5-mysql
$ sudo apt-get install php5-mcrypt
$ sudo apt-get install php5-cli
$ sudo apt-get install php5-curl
$ sudo apt-get install php5-sqlite
$ sudo apt-get install sqlite3
$ sudo apt-get install libsqlite3-dev
$ sudo php5enmod mcrypt

# Restart apache server

Globally install composer


$ sudo apt-get install curl
$ curl -sSk https://getcomposer.org/installer | php -- --disable-tls
$ sudo mv composer.phar /usr/local/bin/composer

Globally install Phing


$ CURL=`which curl`
$ sudo $CURL -O https://www.phing.info/get/phing-latest.phar
$ sudo mv ./phing-latest.phar /usr/local/bin/phing
$ sudo chmod 755 /usr/local/bin/phing

Jenkins Phing plugin



Jenkins Phing configuration



Build.xml


Assume that you have build.xml file under /workspace/Nation/ projects.


<?xml version="1.0" encoding="UTF-8"?>

<project name="nation" default="build" basedir=".">

<!-- FILESET -->
<fileset id="sourcecode" dir="${project.basedir}/src">
<include name="*.php" />
<include name="**/*.php" />
</fileset>
<!-- END -->

<!-- SUITE -->
<target name="build" depends="cache, composer, doctrine, fixtures, php-cs-fixer, behat" />
<!-- END -->

<!-- CACHE -->
<target name="cache">
<echo message="Clearing cache ..." />
<exec executable="rm" checkreturn="true" passthru="true">
<arg value="-rf" />
<arg value="${project.basedir}/var/cache/test/*" />
</exec>
</target>
<!-- END -->

<!-- COMPOSER -->
<target name="composer">
<echo message="Running composer self-update ..." />
<exec executable="composer" checkreturn="true" passthru="true">
<arg value="self-update" />
</exec>
<echo message="Running composer install ..." />
<exec executable="composer" checkreturn="true" passthru="true">
<arg value="install" />
<arg value="--no-interaction" />
</exec>
</target>
<!-- END -->

<!-- DOCTRINE -->
<target name="doctrine">
<echo message="Generating entities ..." />
<exec executable="bin/console" checkreturn="true" passthru="true">
<arg value="doctrine:generate:entities" />
<arg value="AppBundle" />
<arg value="--no-backup" />
<arg value="--env=test" />
</exec>
<echo message="Creating database ..." />
<exec executable="bin/console" checkreturn="true" passthru="true">
<arg value="doctrine:schema:create" />
<arg value="--no-interaction" />
<arg value="--env=test" />
</exec>
<echo message="Updating database ..." />
<exec executable="bin/console" checkreturn="true" passthru="true">
<arg value="doctrine:schema:update" />
<arg value="--force" />
<arg value="--no-interaction" />
<arg value="--no-debug" />
<arg value="--env=test" />
</exec>
</target>
<!-- END -->

<!-- FIXTURES -->
<target name="fixtures">
<echo message="Loading data fixtures ..." />
<exec executable="bin/console" checkreturn="true" passthru="true">
<arg value="doctrine:fixtures:load" />
<arg value="--no-interaction" />
<arg value="--no-debug" />
<arg value="--env=test" />
</exec>
</target>
<!-- END -->

<!-- PHP-CS-FIXER -->
<target name="php-cs-fixer">
<echo message="Checking coding standards ..." />
<exec executable="vendor/bin/php-cs-fixer" checkreturn="true" passthru="true">
<arg value="fix"/>
<arg value="--dry-run"/>
<arg value="--verbose"/>
<arg value="--diff"/>
</exec>
</target>
<!-- END -->

<!-- BEHAT -->
<target name="behat">
<echo message="Running behat tests ..." />
<exec executable="vendor/bin/behat" checkreturn="true" passthru="true">
<arg value="--suite=app"/>
<arg value="--format=progress"/>
</exec>
</target>
<!-- END -->

</project>

Notes


This example uses symfony so you should have vhost enabled for the project and .php_cs file in the root of project.


Vhost


<VirtualHost *:8082>
ServerName nation.dev
DocumentRoot "/var/lib/jenkins/workspace/Nation/web"

<Directory "/var/lib/jenkins/workspace/Nation/web">
Options Indexes FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/nation.dev.error.log
CustomLog ${APACHE_LOG_DIR}/nation.dev.access.log combined
</VirtualHost>

php_cs


return PhpCsFixer\Config::create()
->setUsingCache(false)
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'ordered_imports' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude(['app', 'bin', 'spec', 'var', 'vendor', 'web'])
->in(__DIR__)
)
;

Manual test


Currently we're logged in as vagrant user in out vagrant machine. Lets switch to jenkins user and run tests.


$ vagrant@jenkins:~$
$ vagrant@jenkins:~$ sudo su -l jenkins

$ jenkins@jenkins:~$
$ jenkins@jenkins:~$ cd workspace/Nation/
jenkins@jenkins:~/workspace/Nation$ phing
Buildfile: /var/lib/jenkins/workspace/Nation/build.xml

nation > cache:

[echo] Clearing cache ...
[echo] Warming cache ...

// Warming up the cache for the test environment with debug true
[OK] Cache for the "test" environment (debug=true) was successfully warmed.
nation > composer:

[echo] Running composer install ...
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

// Clearing the cache for the dev environment with debug
// true

[OK] Cache for the "dev" environment (debug=true) was successfully cleared.

> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets

Trying to install assets as relative symbolic links.

-- -------- ----------------
Bundle Method / Error
-- -------- ----------------

[OK] All assets were successfully installed.

> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget

nation > doctrine:

[echo] Generating entities ...
Generating entities for bundle "AppBundle"
> generating AppBundle\Entity\Country
[echo] Updating database ...
Updating database schema...
Database schema updated successfully! "2" queries were executed

nation > fixtures:

[echo] Loading data fixtures ...
> purging database
> loading AppBundle\DataFixtures\ORM\LoadCountryData

nation > php-cs-fixer:

[echo] Checking coding standards ...
Loaded config default from "/var/lib/jenkins/workspace/Nation/.php_cs".
............
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error

Checked all files in 0.418 seconds, 8.500 MB memory used

nation > behat:

[echo] Running behat tests ...
....

2 scenarios (2 passed)
4 steps (4 passed)
0m0.24s (32.70Mb)

nation > build:

BUILD FINISHED

Total time: 7.1080 seconds

Jenkins test


I assume that there is a build in your "Build History" so just manually build. If you go to "Console Output" of your build, you should see the same output as above.