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.

Travis CI is a hosted continuous integration service. To be able to use it, you need to enable it in your GitHub project's settings as a service and activate your GitHub project in your Travis CI account. That's all! In the case of Jenkins, you would need to host it, install it, configure it so on. and it can be very tricky time to time. I'll cut it short to show you what you want to know.

In this symfony application specific example, I'm going to run php-cs-fixer, phpspec and behat tests in "test" environment. I'll also setup a virtual host that is specific to behat tests. Our application is called travis and the virtual host to it is travis.dev.


Things to know



Setup


For more information about setup, visit Travis CI for Complete Beginners page. It tells you what you should do in your in project, project's GitHub settings and Travis account.


Configuration files


As you can see below, I commented lines so I think they should be enough for you to understand.


.travis.yml


The name .travis.yml is compulsory. This file goes to root of project.


language: php

cache:
directories:
# Cache composer directory ...
- $HOME/.composer/cache

php:
# Test against the given PHP version ...
- 5.6

env:
global:
# Set build specific variables ...
- SOURCE_DIR=src
- VHOST_FILE=.travis.vhost
- VHOST_CONF=travis.conf
- VHOST_URL=travis.dev

install:
# Install Apache web server and FastCGI module ...
- sudo apt-get install apache2 libapache2-mod-fastcgi > /dev/null

before_script:
# Enable PHP-FPM and FastCGI ...
- sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
- sudo a2enmod rewrite actions fastcgi alias
- echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
# Disable Xdebug ...
- phpenv config-rm xdebug.ini
# Configure application's virtual host ...
- sudo cp -f $VHOST_FILE /etc/apache2/sites-available/$VHOST_CONF
- sudo sed -e "s?%TRAVIS_BUILD_DIR%?$(pwd)?g" --in-place /etc/apache2/sites-available/$VHOST_CONF
- sudo sed -e "s?%VHOST_URL%?$VHOST_URL?g" --in-place /etc/apache2/sites-available/$VHOST_CONF
- echo "127.0.0.1 $VHOST_URL" | sudo tee -a /etc/hosts
- sudo a2enmod rewrite
- sudo a2ensite $VHOST_CONF
- sudo service apache2 restart
# Set application parameters ...
- cp app/config/parameters.yml.dist app/config/parameters.yml
# Composer install ...
- travis_retry composer install --no-interaction
# Remove application cache and logs ...
- rm -Rf app/cache/*
- rm -Rf app/logs/*
# Prepare application cache ...
- php app/console cache:warm --env=test
# Grant application cache and logs permissions ...
- chmod -Rf 777 app/cache/
- chmod -Rf 777 app/logs/
# Install application assets ...
- php app/console assets:install --symlink --relative --env=test
- php app/console assetic:dump --no-debug --env=test
# Create application database ...
- php app/console doctrine:schema:create --env=test
# Update application doctrine entities ...
- php app/console doctrine:generate:entities ApplicationTravisBundle --no-backup --env=test
# Load application data fixtures ...
- php app/console doctrine:fixtures:load --no-interaction --no-debug --env=test

script:
# Check coding standards ...
- bin/php-cs-fixer fix $SOURCE_DIR --dry-run --diff --verbose --fixers=-yoda_conditions,-phpdoc_align,short_array_syntax
# Run phpspec tests ...
- bin/phpspec run --no-ansi --format=dot
# Run behat tests ...
- bin/behat --profile=default -f progress

.travis.vhost


The name .travis.vhost is optional. I just wanted to make it similar to .travis.yml. This file goes to root of project. When the build runs, virtual host related commands in yml file above will modify TRAVIS_BUILD_DIR and VHOST_URL variables.


<VirtualHost *:80>

DocumentRoot %TRAVIS_BUILD_DIR%/web
ServerName %VHOST_URL%

<Directory "%TRAVIS_BUILD_DIR%/web">
Options FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order deny,allow
Allow from all
</Directory>

# Wire up Apache to use Travis CI's php-fpm.
<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization
</IfModule>

ErrorLog ${APACHE_LOG_DIR}/%VHOST_URL%.error.log
CustomLog ${APACHE_LOG_DIR}/%VHOST_URL%.access.log common

</VirtualHost>

Images


Travis CI project build settings



Passing build in GitHub



Passing build in Travis CI



Broken build in GitHub



Broken build in Travis CI