Speedup Travis CI for PHP apps

At GRRR we use Travis as Continues Integration tool. Running unit tests, deploying and notifying are the three main tasks. To have a smooth workflow, speed is vital.


We already published a post about auto-deploying at GRRR. The last couple of months I improved the readability and speed of our Travis builds and deploys. Some optimizations are PHP specific, but others could be used for other stacks.

Xdebug

By default Travis enables Xdebug. It’s useful to have when you want to generate code coverage reports. We don’t use it, so let’s disable it.

before_script:
    - phpenv config-rm xdebug.ini

Enable caching for Composer and Yarn

Downloading packages takes a lot of time so caching them improves build time a lot. Don’t cache vendor/, because that can cause wrong package versions when dependencies change but the version constraints still match.

cache:
    yarn: true
    directories:
        - $HOME/.composer/cache/files

Slow database migrations

Every build migrations must prepare the database. Most migrations are very fast – the database is empty and creating a table won’t blow up your database server. But as the application ages the amount of migrations increases. 100 very fast migrations end up taking quite some time. Beside that, running those migrations again and again is useless so let’s dump the database and import it.

Dump a database in the state of one month ago. You can choose your own time frame of course.

./migrate --until 1-month-ago # Or when your tool doesn't support it:
git checkout 'master@{2019-01-01}' # Go back in Git time and migrate your database

mysqldump -r database-until-1-month-ago.sql your-database

git add database-until-1-month-ago.sql
git commit -m "Add db dump to bypass migrations"

We add these commands to the project Makefile. To make the date dynamic you can use date.
MacOS date -v-1m +%Y-%m-%d
Linux date --date="-1 month" +%Y-%m-%d

Import the database dump before the migrations are executed. The migration up ’til now needs to run.

before_script
- mysql -e "source database-until-1-month-ago.sql" travis-database
- ./migrate

Auto Cancellation

Last, but not least. Enable Travis Auto Cancellation. It cancels builds for your branch or pull request that are waiting to run. When you and your colleagues merge multiple PRs at the same time a lot of builds for master are added to the queue. This feature allows you to only run builds for the latest commit, by removing waiting builds from the queue.