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.
2 responses to “Fun with Travis CI and PHP projects”
To give project managers a good idea of whether or not a pull request would break your build, Travis CI automatically builds your project with that pull request and shows itâ€™s status on the PR.
Agree with the above comment and this is the biggest benefit of Travis CI. Also, I had written a basic guide which might benefit the beginners: https://www.cloudways.com/blog/php-continuous-integration-travis-ci/