Skip to main content

Travis CI and PHP applications

Travis CI and Symfony console applications

Travis is a great tool for testing and deploying your applications. I like Travis because it's free for open source projects, and also because you don't have to install anything on your servers. If you are using let's say Jenskins, you know that you have to install it on your server and that can be a problem because it will use your precious server resources and because you will have another application on your server that you have to maintain and keep updated.

Setting up Travis is easy. If you already don't have an account you can create it with your Github credentials here: https://travis-ci.org. Once you are logged in go to your profile and you will see all your Github repositories. You can enable or disable Travis integration with a simple click of a button.

Travis repositories

Now you need only one more thing to get things up and running. You must create .travis.yml file in your repository, and that is where all the magic happens.

Let's take a look at one example file, that I'm using for my project and go line by line. You can checkout the repo here: https://github.com/gnikolovski/dix

.travis.yml

branches:
    only:
        - master
sudo: required
language: php
php:
    - '7.0'
    - '7.1'
    - '7.2'
install:
    - composer install
script: 
    - wget -O box.phar https://github.com/box-project/box2/releases/download/2.7.5/box-2.7.5.phar
    - php -d phar.readonly=0 box.phar build && sudo mv dix.phar /usr/local/bin/dix && sudo chmod +x /usr/local/bin/dix
    - vendor/bin/phpunit
notifications:
email: false

Key branches is used to specify when Travis CI should build your project. In my case I want to build my project only when there is a change in the master branch. Changes in all other branches are ignored. Every push to master will trigger a build!

In my case I need sudo because I'm moving files and changing file permissions. If you don't set sudo to required you won't be able to do that. You might not need this in your project.

Next key is language. It's pretty self-explanatory what this means. My project is written in PHP and I want to test it on all 7.x versions.

Since my project depeneds on some other packages and I'm using Composer to manage my dependencies I need to run composer install command to get all required packages. Without this build will fail, so if you are using Composer and you don't keep your vendor packages in the repo make sure to add this command.

I'm using box2 to package my application in a single *.phar file. Because I don't keep box2 in my repository, I need to tell Travis to download it. The right place to do this is the script key. Once the box2 is downloaded, the next thing that is running is box2 build command and after that I'm moving the built file to /usr/local/bin/ folder.

Because I have some PHP unit test (you can see it here: https://github.com/gnikolovski/dix/blob/master/tests/ConfigCommandTest.php), I can tell Travis to execute it. Command vendor/bin/phpunit is doing exactly that. If your tests fail, the build will also fail!

The last key is notifications. I disabled email notifications, but you can keep it if you want. You can also send notifications to HipChat, Slack or IRC.

If everything goes well after you push your code to the master branch and Travis builds your project you will see in the Travis profile that build has passed. You can also add the status image to your readme file that will display build status on the Github project page. Just use the link bellow and replace repository names.

[![Build Status](https://travis-ci.org/gnikolovski/dix.svg?branch=master)](https://travis-ci.org/gnikolovski/dix)

 This is how it will look:

Travis CI build status

The last thing, that I still don't have in my .travis.yml file is a piece of code that will push built dix.phar file to my site where users can download it. I plan to add this in the future.

As you can see you can use Travis CI only to test your project - you don't have to deploy it anywhere if you don't want. This is a good option because you can test your app on multiple versions of PHP with ease and without wasting your time and resources on your server. If you want, you can also deploy the app to your production server, but make sure that you have some test coverage to avoid surprises.

This is only the tip of the iceberg. Travis CI is so much more powerful. Head over to the Travis documentation and start exploring this amazing tool or go the my app repository and checkout it out.

Share