2012 Paris JUC: Sebastian Bergmann – Integrating PHP Projects with Jenkins

hi I’m Sebastian for 14 years now I’m doing stuff with PHP in the beginning and for 13 years to PHP and around PHP it was a really slippery slope from starting to figure out how this PHP thing works to finding bugs in it and asking the right questions to the right people on how to fix these bugs and became a contributor and then at some point a professor at university taught us about j unit and he knew that i was doing PHP and didn’t really like java and sorry wrong conference made me to tell to say that but and he said well you seem to like this testing and quality approach that i just showed you is j unit and all the other nice tools that started to come up in the Java world at that point and he said well would this be enough for you to make a jump to something like Java because you don’t have these tools I said well I’d rather stay with PHP and build those tools even if I have to build a myself maybe some others they’ll pick up and help me over the years and this is basically what happens and what I’m going to talk a little bit about this presentation so I don’t really want to do marketing and really bad at marketing but I have a company I’ve over the last years I’ve helped dozens of companies around the world from really small startups to fortune 500 companies get started with PHP quality assurance in their projects mostly using Jenkins some of them initially wanted bamboo or some other commercial solution I help them vez that as well but they quickly figure out that a commercial when there is really not as good as the open source community when it comes to customizations and books and fixing them in a timely manner and it’s not really my idea of a good product when you say hey I found a bug can you please fix this because I’m not productive this bug is fixed and this and Windows tells you hey by a different license and we give you the source code and you can buy fix the book yourself that’s not how it’s supposed to work so the one thing that I want to start with is a short history of continuous integration in the PHP world why are we using mostly Jenkins today so it starts very similar I guess when you compare the peach people to the Java world but in the beginning you were using cruise control which i have to say was probably the worst piece of software that i ever had to work with it crashed a lot it needed to be restarted multiple times a day at least for production size instances it usually ate your data for breakfast lunch and made me sometimes even dinner and was not reliable at all and it was and it was the most the biggest problem with it was that it had no concept of plugins if you wanted to change something you had to basically download the cruise control source code and hack in there and make the fixes that you want that you needed to get it to run even or add new features and then compile it and yet this one monolithic beasts that you had to deal with totally different compared to Jenkins or yeah so me in the PHP community started to use cruise control around early 2006 and it went on for a while and soon one company that I know in Germany they started to build customizations for cruise control to make it nicer to look at and easier to work with for PHP projects they wanted to commercialize these plugins and deliver a quality platform for PHP project or whatever they wanted to call it that never happened because Manuel officially had the idea of just taking cruise control and changing a little bit of Java code here and there adding a little bit of HTML and JavaScript and xslt style sheets and add something on top of it he called PHP under control it can also came with a command-line tool that allowed you to just specify I won the new project I want a new job in my cruise control instance and it’s for a PHP project and this is where my CVS or subversion repositories and I’m using PHP unit for my tests and please generate me an exam l configuration file for cruise control and generate me and

build script to actually orchestrate all these tools and it took a lot of lower the barrier to entry for using cruise control a lot but it was flawed in a way that it was still cruise control with all his crashes and nastiness is under the hood so it really never really took off and the community around it wasn’t really that nice either so I remember sometime in 2007 getting an email from someone after I posted something on the cruise control mailing list was a private reply not to the mailing list that’s why it doesn’t show in the public archive saying you people from the PHP will stop using our tools these were built by Java developers for Java developers and you are stealing them again not my idea of open source so I was really pleasantly surprised in more than one way when i came across hudson for the first time not only was it a much better product and had a plug-in architecture and lots of plugins around it but also the community was a lot nicer and then well the separation happened and whatever and that’s what we are we talking about Jenkins today still very happy with it at law made lots of experience over the last couple of years in trying to figure out how to make the best use of it in the PHP setting and it’s so much easier when you’re compared to other solutions because there’s a plug-in for almost everything and doesn’t really matter to the community if you’re using it for java project or for a ruby project or a heightened project or for a PHP project they don’t just care about the developers and not about one specific programming language stack sure there’s a focus on Java because it’s built in Java and the initial target audience was just Java but I never felt alienated in the community so now you know a little bit about that guys standing on the podium right here hopefully maybe maybe not but I don’t have the first clue about you guys in this room because this is very strange setting for me to present on continuous integration in PHP environment I usually have the Reavers where i talk to PHP developers that you want to get started with jenkins and tell them all about these tools that they have at our disposal and how to just orchestrate and integrate in this Jenkins so at least I think I’m now in a situation where I’m talking to people or discuss with people that know what continuous integration is know what Jenkin is and what it does but maybe have not so much of a PHP background so it would be really helpful for me if at least some of you could introduce themselves or explain why they are here and yes just sitting here and having water or whatever and space to work and network is a valid answer yes so let’s see if the few slides that I have prepared actually work out for this so so basically what i prepared slides before i go into more hands-on / demo kind of way of presenting it’s just a given overview what kind of tools exists in the PHP world and how you can listen to them in jenkins through various plugins so first is phpunit this is how it all started and of course I’m a little bit biased there because I wrote phpunit and take everything i say with a grain of salt plenty of other testing frameworks but this is the one that’s most commonly used and it turns out that i threw laziness 11 12 years ago also made the right decision by saying okay I somehow may at some point in the future want to do something useful this data that I collect when running the test so i probably should write a log file and it was too lazy to figure out my own log file format so i looked at what j unit was writing and so that’s why phpunit just generates j unit XML which is why it was so easy for cruise control to immediately do something with these log files it just didn’t care about that this log file was not generated by J unit but by another tool that just wrote to say mimics ml and all the other tools that I’m going to going through for quality assurance things in a PHP world took build upon the same idea took the

idea and also looked at other tools from the drama world mostly with regard to the way that they lock the results little bit later in couple of years later I add support for code courage in PHP unit once we had the low level of functionality for that in the PHP interpreter that only exists because I bug the right guy about hey I need this for phpunit this would be really awesome if he had that so once again I needed to figure out an XML format and I looked at clover because that at least to me as an outsider with regard to the java community felt like a de facto standard in the Java world so when working with Jenkins for PHP projects there’s a couple of plugins that you need it’s the x unit plug in to interact with the log files that jacket for unit generates it’s not a hundred percent compatible as I figure out over the years with genuine and external log files so you need this X units armed plugin that reads the PHP genetics and l test result log files and the reason why you need that is Rico’s phpunit users nested test suite elements because you can have nested test Suites and for a very long time we were using the clover plugin for Jenkins to get the code coverage statistics into Jenkins but there was something it always bugged me about that and bucked others about that because it’s expected not only a clover XML log file as input but also it’s somehow expected the actual clover product to be installed and have a license key and it would print warnings or errors when it wouldn’t find that and of course you don’t have a Java code coverage tool installed when you’re working in the PHP environment and you would have and just what what annoyed me at least the most was there was a button or a link in the build on the build page in Jenkins that said hey click here for more detailed clover information and it would always would always lead to a 404 HTTP error because you didn’t have clover installed so i ignored that for a very long time and at some point somebody just wrote a plug-in called clover PHP which is basically a fork or a rewrite of the clover plugin just just just just just read the exam a log file and just useful stuff with that and doesn’t interact mr clover tool PHP code sniffer is probably the oldest static code analysis tool that we have in the PHP world it was written originally just to figure out whether or not to detect violations with regard to coding standards formatting opening curly braces same line next line taps or spaces and what have you but over the years it has grown and now comes out of the box with a couple of hundred sniffs that sniff at your code and tell you where something is broken so it can detect potential performance problems potential bugs and so on and just as a trivia side note it’s the only code analysis tool for PHP that is used that is commonly use it is not written by a German I get this question at ph three conferences around the world a lot why is it always the Germans that built these tools that tell me that my code is broken I have no answer to that but at least their speed records new voyages the outlier that says hey it’s not just us that care about quality it’s also the Australians so she cooks liver generates check starlix and i’ll check style similar tool in a driveable to probably all know that so that’s why we use a check style plugin or the violations plugin because they just natively do know what to do with these log files they don’t care at all about the these log files are not written by the original to next caller static analysis tool that we use is PGP copy paste detector or PHP CPD for short and on the command line and it just does what the name suggests it looks for duplicate code it produces log file output in PMD CPD XML so for

instance Jenkins dry plug-in knows exactly what to do with that and how to visualize that and how to keep track of trends and so on PHP depend the name may already give it away the port of Jay depend or inspired by Jay depend collects and calculates raw code cover a raw software metrics information about your PHP project and stores the result in a jade appendix and L file and in a custom XML log file that you can use from some more data mining on your software and it also does software visualizations like a software overview pyramid and so on and of course you use a JD pen plugin for that again doesn’t care about DXM metallography I’m not coming from J deep end we have PHP md the HP mess detector which sits basically on top of PHP depend allows you to define rules that operate on the raw data collected by PHP depend and when one of those rules is violated you get a violation report and it’s inspired by PMD the project mass detector or program of mass destruction or pretty much done or any day now maybe the PMD project decides on what the acronym actually stands for last time I look at a website ever at least 10 different suggestions what PMD was supposed to stand for most commonly used ones probably project meth detector and naturally PHP emily just writes PMD xml log files as a result which allows us to use a Jenkins PMD plug-in to keep track of trend miss regards to the mess that we carry around no HP project it may be a bit overkill that I in my instances use both the textile plugin and the violations plugin and I may stop using your checks i’ll plug in at some point but at least in rigging and i find it interesting to have two different views on all of this data but since the violations tool or the violations violations plugin supports all of these different log file format that come out of the PHP quality assurance tools it’s a really nice central place to look at the trends with regard to the findings from a psychic analysis tools so that’s really useful to have and in the PHP project we use it to do the useful stuff is PHP code sniffer pitch recovery phase detector and pms detector there are a couple of tools that i left out for engine for instance there’s also PHP dcd the PHP dead code detector but it’s not ya ready for production or anything it was an experiment at it a couple of years ago on a flight home from Brazil it basically works as far as its unit test goes but every time I used it on a production code base it failed which is not surprising given the highly dynamic language highly dynamic nature of the PHP programming language where you can have variable variables and variable method calls and so on so as and its limitation of most of these static analysis was the more dynamic features user to language the more problems you get visitor analysis tools I mentioned earlier which is a company or that was a company that wanted to do a commercial support offering around cruise control a couple of years ago geared towards PHP what surance and continuous integration and they basically gave up on that idea one speech be under control came out but the centerpiece of what they did back then has been open sourced since under the name PHP code browser and this is just a small PHP tool that takes all the exam a log files from all the other tools as input plus the source code and generates a static HTML output where you can browse through your code base and get highlighted sections in the code with annotations like because never found something here phpunit says that this method has less than eighty percent code coverage and so on so that’s a nice integrated way of looking at your code and doing stuff there’s also an eclipse plugin from the same developers of peach and PHP code browser that integrates all

this information that’s gathered by your jenkins into eclipse and shows why you’re editing while you’re working on the code a peach because that I’ve found something here please fix that a generic plug-in that we use a lot of the HTML publisher for gender for publishing API documentation for instance or the output of PHP code browser or PHP units code coverage report PHP loc really small tool that initially just did what the name suggests just count the lines of code in your project but over the years grew and grew and grew and now also counts other things like how many classes do I have and are they abstract or or not or how many methods do i have in our day evil as in static or not and this tool grow grew over the years because i tend to do a lot of code reviews of code that i have not the first clue about and the first thing that I want to know is how much code do I have and how is it structured and by looking at how many of how much or to what extent new fever newer features of the PHP language are used I can make it guess of how old the code base is and how our new concepts are used and so on so that I was too lazy to build separate tools for that so I put all of that in PHP LLC the interesting thing though is that you can take this data and provide it to the plot the plot plugin for Jenkins and get metrics collected over time like this can be really interesting if you are refactoring a legacy PHP for code base with lots of duplicated code with no object-oriented features being used to more modern php5 code so as you refactor that a number of duplicate code goes down but also the number of lines of code goes down and the number of classes goes up and so on and because I’m really really lazy and don’t want to do the same things over and over and over again at some point as I said okay I don’t want to go through the same steps every time i’m configuring a new jenkins job for a PHP project so i did what i usually do when i found a problem like that i create a new open source project so i created jenkins PHP drinking stash PHP dot org which is first and foremost a template configuration external files or jenkins for a PHP job but also gives you a pretty good foundation of a Apache and build script for your PHP project it orchestrates all these different tools that i just mentioned and invokes then there’s the right argument and puts all of you build artifacts all the log files in a standard directory structure so the jenkins template can pick them up and you don’t or you almost have no configuration to do as you’ll see in just a minute so this slide is probably superfluous because you probably all know how to install Jenkins usually my audiences don’t know so for the demo I usually just download the latest archive and just set the environment variable and run it and then I instill a couple of plugins I’m I guess by now I’ve said it quite often I’m lazy so I don’t like to use the web user interface so install all the plugins and I need I just used the command line interface just tell it all the plugins that I need and there are very few things that I don’t like about Jenkins and one of them is that it comes out of the box with support for CVS and subversion but not for something that is from this millennium so I always have to install gate manually and I install the green balls plugin because blue is not really associated with success for me so it’s weird how from when you grow up and look at a traffic light for the first time and you are told you only go when you agreeing how that’s programming sticks with you and it kind of just easily adapt to something else so that was just an overview of all the tools that there are and what kind of plugins we use in Jenkins to pull the information in and get them in our Jenkins setup so any

questions so far question was do I use sonar not yet I tried or roll back every couple of months I try to use it for a PHP project so far it’s not as easy to set up as I would like it to be it’s gotten a lot better now that the dependency on maven is gone and it actually can work with and bass bills and it doesn’t run it does not want to run the tools twice and so on but it’s not really nice yet I can get it to work but it feels like I need to do too much work to get it to work any other questions okay so as I mentioned earlier I don’t install the service container or whatever because usually Jenkins in the environment that I work in Jenkins is the only piece of Java that’s there or at least available to the team that I’m working with so there’s no need for a full-fledged service container so out of the box it just works great what we usually do is put in a another HTTP server in front of it like engine eggs or live DVD just to have some proxying going on and some mechanism for HTTP authentication for instance that the admins are more familiar with than having to learn something new so this is the website de mentioned earlier Jenkins dash PHP dot org you can get a template for Jenkins jobs for PHP projects it basically lists all the plugins that i mentioned mentions how to install them tells you how to install the required peach p tools which is really simple if you have a sane PHP environment you just do pair install para PHP QA tools org slash peach p QA tools this is just a meta package that pulls in all the dependencies all the required peach p tools and like phpunit PHP code sniffer PHP depende de pitch Petry copy paste texture and all of them and you’re good go in a minute or so okay so this is a template project that i mentioned earlier so when i create a new job like i have a project an example project that i use in my phpunit training called bank account it’s really boring but it shows a lot of different things that you can do miss Peecher unit with regard to testing the various layers of your application from really small pure unit tests over integration tests all the way to edge to edge and system-level end-to-end tests and then you just copy this from the PHP template and the only thing left to do after that is enabling the build and configuring the version control system and just for completeness specify the master and that should be it and of course we need to configure the bills trigger usually no I don’t want to do that but for demo purposes yes this is exactly what I want every minute and that’s it and now something useful should happen at some point so yeah why do I wait because when I started to use Jenkins the built now button didn’t work and it’s just maybe I think it was only in the one release and a week later it was fixed but it is something that just stuck with me so I usually just wait the minute and in the meantime do something

else that’s just me okay oh ok so instead of rat invoking the command line install thingy for all in plugins individually I can do it in one command I didn’t know that is that new 400 years okay for 100 raises 100 weeks okay I think I looked at that tried that one in the beginning and it didn’t work and then after that it was implemented ok thanks I’ll fix that because that’s something that nag me yeah good ok now we we have a built green bowl and the sun is shining although the sunset and the Oracle came up and whatever but it’s still sunny and this is what the project overview looks like for a PHP project if you use this template you get the code coverage report API documentation the PHP code browser output and test results and everything and of course this is really boring because it’s the first build there’s all these charts that need two data points at least to do something nice are not there and it’s boring because there are no violations as other person code coverage and everything but if you’ve never seen it before this is what the code coverage report looks like it comes out of each unit this is what the code browser looks like so you can browse through your code and it tells you where it’s will found the violation and so on and normal output from the JD Penn plug-in doesn’t care about the fact that it’s not a java project not the real JD pen was run but PHP deep end that generated in a deep end log file same with the violations and the test result i think i have this year it has some more charts but some more data points ladies actually see the charts but so if you go to the console output we see what actually happened it’s just plain and build that happens behind the scenes of course you can use everything every other to build tool that is supported by by Jenkins it’s just my personal preference that I like and or end is good enough for me it just works it works everywhere yes somebody ported Apache end to PHP the fing project which is also natively supported by Jenkins as a fing plugin that you can install but I don’t see the point in porting such a tool from Java to PHP just to have it in PHP I also don’t resent some people that implement continuous integration service in PHP because they don’t want to use something that is not written in PHP I cannot understand that I’m much more pragmatic than that so we invoke a couple of tools the PHP autoload builder PHP a b which is really the tool of choice to do your autoloading your class loading in PHP these days be prepared a build by creating a couple of directories we do a lint check no point in trying to run the unit test if there’s a syntax error you don’t need something like that in Java because the Java compiler takes care of that in an interpreted language it’s really useful to have tool in check and then all the tools that i mentioned PHP LLC PHP depend HP messy Tector or PHP code

sniffer HP copy/paste detector PHP dogs for API documentation phpunit to run our unit tests and then in the end PHP code browser at tools in all leagues and I logged miles and integrates them interlaces them with a source code to give us a unified browsing experience and then the build is done and jenkins starts to do its thing it collects all of the information publishes the results delicious any questions what’s the tool that I use for the lint check the syntax check and you just use the PHP interpreter PHP dash l it’s the only thing that you need there’s no other link checker that I know for PHP it’s it’s built into the interpreter it’s really easy to use so in the end script you just used you apply the apply task instead of the exact ask apply takes as an argument of file set so you can say okay every fifth every every file that ends in dot PHP in my source directory please give that file to the path to that file as argument to PHP dash L and it will do that unfortunately PHP dash L can only accept one file at a time so you need a new PHP interpreter for each file that you check which is really really slow but if you use the modified option here and will only run the lint check on all files the first time it’s run and after that it will only run it on the file that have changed in the last time you invoke the build it keeps track of which files have changed for you that’s one of the advanced our file set features of an that you can use and with all the other tools it’s just like invoke it with same defaults and write the exam I’ll log files or other build artifacts to a standardized location so that the Jenkins template can pick up on that that makes it really really easy to use and still gives you lots of flexibility to customize it to your needs so the question is in a typical PHP project you not only have PHP but also JavaScript for the front end and how how do I cope with that so there are unit testing tools for JavaScript that you can use basically every major framework has their own that they advocate the couple of standard ones like the one from yui is pretty popular there’s jas unit that’s somewhat popular PHP code sniffer most a lot of the sniffs of PHP code sniffer can also be applied to JavaScript code so there’s a JavaScript parser built-in in PHP as part of PHP code sniffer that allows you to apply those rules also to JavaScript code so you can use that to enforce coding standard for instance on JavaScript code I don’t know about any specific setting analysis tools for JavaScript but I would assume that they exist but probably have similar or even worse problems than the PHP tools because javascript is also very dynamic and you cannot figure out everything aesthetically but I believe that there is at least some rudimentary tool that you can use but i would probably put it also into jenkins probably not into the same job though because just because i want to test my my PHP code shouldn’t necessarily mean i also want to run my JavaScript unit tests so I would what I’ve done in the past is have one job in Jenkins that runs the PHP unit tests and then one job that runs the JavaScript unit tests and when both are ok run a different job that runs selenium tests for instance

because now you know the bag and code works correctly as according to its unit tests and the front end unit tests work correctly and now you have to do the integration tests any other questions or requests wishes for things to look at because this is as much as I have prepared a broad overview of all the things that they are I again I had not have no idea what your level of knowledge is with regards to PHP development and quality assurance tools and and so on so if you’ve never seen one of these tools in action we could look at how to use PHP code sniffer of PHP deep end or PHP messy Tector and how to specify rules configure rules build your own rules depends on what you want to do so the question is how do i do a continuous deployment with Jenkins in a petri environment for instance okay yeah okay at the end of the build you build a package and it depends on what kind of package you want to use I’ve worked with teams to just build pair packages or far archives or build distribution packages like rpm there on redhead or all centers or Debian packages or whatever so so there are a couple of questions in there and I try to repeat them all right at the same time prioritizing them somehow the question one question in there was do I think it’s overkill to Jenner to create linux distribution based packages like rpms or Debian packages or whatever no I don’t think that’s overkill I think it’s a really nice way of expressing all the dependencies of your application so for instance you can make a new release of your software and say okay if you want to install this I need this version of PHP I need this version of MySQL I need this version of memcache de or whatever other system level components you have and this is the stuff that you cannot really express in in a package XML for pair for instance yes you can express I need PHP version 5 40 but if you want to install that using your parents all right you don’t have that PHP version it will just say okay I cannot install this because this dependency is not met if you do this with a system level package like an RPM or a Debian package and the dependency can be resolved it will be resolved for you you will get the new version of newer version of PHP and so on but it’s really easy to unify all of your dependency management in one place which also makes it easy going one step further of this roof is regard to provisioning of new machines using chef or puppet just say okay I want to set up a new web server and then your script knows okay web server means i need this distribution i’m going to install that and then I’m installing this list of packages and oh this application is supposed to be installed there so I install that package as well and pull it in there the remaining dependencies that is really powerful and not it’s really a simple solution for a complex problem there are a lot of complicated solutions for that doing it manually would be the most complicated one I can think of but this makes it really easy and helps the admit the admins stay sane so no I wouldn’t say it’s overkill to generate distribution packages so question is selenium for front-end testing or you should have as few selenium tests as you can get away with the larger the test gets the less useful it becomes the less valuable the information pays you back with a really large test can only tell you that something is broken doesn’t really tell you where something is broken that’s what I want to have as much as many unit tests as possible it has just one unit of code in isolation from all of the dependency so that then when it has fails you know exactly where something is broken but yes selenium is very

useful I don’t know at the moment something that’s better than selenium at automating a browser and similar and not stimulating but rigging triggering a real request that goes all the way to réserver does the real response and everything what I see more and more though is that in addition to just using selenium which can be further tests can be used for instance also for compatibility testing with regard to different browser versions and browser vendors and so on to also use something like B hat which is inspired by cucumber to have higher level tests that do something useful so we can test on many different levels and have something it looks like natural language while still let me see if I have something should have something in here just need to find it trip being here blower there for sombreros and so if you’ve never seen be hat or a cucumber which inspired it this is basically the idea behind it allows you to write what they call a scenario just plain text basically like given I have a fresh bank account then the balance should be zero yes this is behavior driven development and then you can just run that and it says okay I found these two steps in the document that you gave me no specifications we gave me it runs the code and for each sentence that is executed and successful you get the line highlighted in green and of course this does not work because there’s a crystal ball and artificial intelligence behind the scenes that just automatically figures out which portions of the code to invoke and how to verify it by just looking at the natural language you have to implement something called a B hat context that acts as a mapper between the natural language and the code that is involved and there’s something called mink which is a sister project to be had which just gives you a default implementation of such a B hat context for testing web applications so this gives you a lot of commands like go to this URL click here click there and verify that something is there and whatever and it has at least three backends like zombie JS for instance that to simulate real browsers so you can do the web based testing without having to use selenium because selenium tests are really really slow and expensive this is much faster than running selenium test because you don’t need to run start the browser and interact with your browser and verify in the browser it’s just native 8hp calls that go on from within the PHP process any other questions then I think I’m done I brought a couple of presents gifts whatever I have four books to give away that I here and also have one elephant if you don’t know the this elephant is the mascot of the PHP community actually a French guy designed them a french comic artist to started drawing comics around the PHP elephant a couple of years ago and then a guy from Paris picked this idea up and found the company that makes cash custom plush toys and just gave them some of the comics and the drawings and they came up with this this is a small one also the really big ones exist they’re really nice and they are I have one to give away so I think he should get one right okay thank you