Travis and different PHP versions in the same repo

| 2 min read

At work we have one git repository for several projects. While this has some advantages their is also some drawbacks. Setting up a CI is one of them, especially when all the applications can't run on the same PHP version.

We use Travis in order to ensure that our tests are still green after a modification but as we are currently working on some refactoring of our legacy app written for PHP 5.3 - We are late, we know... - we wanted to be able to work on a more recent PHP version. The issue is that Travis' build matrix will fail either on the PHP 5.3 build or on the PHP 5.6 one.

We finally came up with a solution, which even if not ideal allows us to see if we are breaking something : we run some of the tests only for one version or the other.

First thing we create a variable for each version of PHP in the before_script section of travis.yml:

before_install:
    - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.3" ]]; then export PHP53=false; else export PHP53=true; fi
    - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then export PHP56=false; else export PHP56=true; fi

Notice that we are assigning false to the associated variable when the PHP version matches.

Now, for each command that Travis should execute we are able to specify if it should be run for the current build version :

install:
    - $PHP53 || ( cd $TRAVIS_BUILD_DIR/applications/app1 && composer install )

This must be read as "Cd to app1 directory and run composer install for PHP 5.3".

In the end our travis.tml looks like this:

language: php

php:
  - 5.3
  - 5.6

before_install:
    - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.3" ]]; then export PHP53=false; else export PHP53=true; fi
    - if [[ ${TRAVIS_PHP_VERSION:0:3} == "5.6" ]]; then export PHP56=false; else export PHP56=true; fi

install:
    - $PHP53 || ( cd $TRAVIS_BUILD_DIR/applications/app1 && composer install )
    - $PHP56 || ( cd $TRAVIS_BUILD_DIR/applications/app2 && composer install )

script:
  - $PHP53 || ( cd $TRAVIS_BUILD_DIR/applications/app1 && ./bin/phpunit )
  - $PHP56 || ( cd $TRAVIS_BUILD_DIR/applications/app2 && ./vendor/bin/phpspec run && ./vendor/bin/behat )

If you want a command to be run for several PHP versions you can specify them as follow:

install:
    - ( $PHP53 && $PHP56 ) || ( cd $TRAVIS_BUILD_DIR/applications/app1 && composer install )

which should be read "Cd to app1 directory and run composer install for PHP 5.3 and PHP 5.6".

Handy, right ?

Hey ! I'm on Twitter too. Come say Hi ! You can feel free to comment below as well.