I know I should have done this a long time ago, but I finally got my hands dirty with Travis CI. I wanted to set up a php project on github to use Travis CI to monitor the status, in case I forgot to run the tests prior to pushing. Unfortunately it was not as easy as it’s made out to be. But now that I’ve done it once, it’ll be easier next time. So, here is how I tackled it.
First, creating an account and getting started was easy. I simply clicked the “sign in” link on the Travis CI site and entered my github credentials which authorized Travis CI to connect to my account. (the site informs you exactly what Travis CI will have access to) Once that’s done Travis gets all my repos, so I can then activate them for Travis CI. If that doesn’t happen automatically there is a handy “sync now” button to coax Travis CI.
NOTE: To connect public repos you would use https://travis-ci.org, while for private repos you would use https://travis-ci.com. While pubic repos are free private ones cost money, though you do get 100 pushes free to get you started.
Second, it’s now time to click the + and add a new repo to be tracked by Travis CI. After clicking you will be presented with a list of your repos to choose from. It is simply a matter of turning the repo ON by clicking the switch. I also clicked the wrench to select the option to “Built only if .travis.yml is present”.
The structure of the app I added looks like this: (including all files needed for travis and unit testing) https://github.com/adamculp/api-consumer
Third, I needed to create the .travis.yml file with the directives needed to make it all work. Here is what my file looked like.
Pretty simple. Here is what it all means: I specified what language to use (php), and what versions of the language to test with (5.3, 5.4, and 5.5), I also instructed to have Composer install prior to any other scripts run (needed to ensure there was an autoloader, created by Composer), and finally I add the phpunit command and tell it where to find the phpunit.xml file (in this case it was in the tests directory).
Fourth, ensure that PHPUnit runs as expected locally. Yes, you will need unit tests on your code. That’s like one of the main reasons to do this in the first place. Here is what my phpunit.xml and bootstrap.php look like:
The phpunit.xml file is fairly simple. It informs where PHPUnit can find the bootstrap.php file (same directory as a the phpunit.xml), and sets a whitelist of directory where code can be found (some use a blacklist and specify not to use the /vendor directory), and what directory to find tests in (the current directory).
The bootstrap.php file specified by the phpunit.xml file is even simpler, as it only specifies where to find the autoload.php file created by the Composer install.
Fifth, take a quick peek at the settings for the repo on github and you will notice that Travis CI should be already set up and ready for something to be pushed.
Final, that’s it! That is everything needed for Travis CI. Of course this example is very simplistic, since the repo and tests are only on a single wrapper class I created. But it’s good enough for a start, and if you need more the documentation is pretty good. With these files created, all that remains is for me to commit changes to git, then do a push to origin at github. Once the push to origin happens a Travis CI trigger at github fires and informs Travis CI to create a new build on a VM (virtual machine) then run the tests on the newest code. After Travis CI finishes it lets you know with an email, and through the site. (green = good)
One last thing you may want to do is add the Travis CI image to your README.md at github. This will allow you and others to see whether the current master branch had a successful build, or if it failed.