Travis CI with PHP and NodeJS done right

Some time ago the sixth version of NodeJS has been released which supports about 95% of all ES2015 features what makes the usage of transpiling tools like BabelJS or TraceurJS obsolete when using NodeJS on the server side.

Although I’m using node for frontend purposes in projects such as Sententiaregum which means that I don’t really benefit from this enhancement, it might save a lot of work and time for my mocha test suite. As NodeJS 5 lacked a lot of ES2015 related features, I’m doing something like this in my JS projects with code written in ES2015:

Actually I’m quite happy with this approach. The only downside is that it is quite SLOOOW. This is due to the fact that babel transpiles all the ES6 code back into ES5 during every test run. As NodeJs 6 supports most of those language features, the speed can be improved massively by stopping the usage of babel during the test when using a node-side framework like mocha.

The approach looks and works well locally, but on TravisCI the continuous integration service I use for all of my OSS projects it doesn’t work properly on PHP builds as TravisCI uses an ancient ubuntu precise which ships the LTS release 0.10 of NodeJS. Since TravisCI recommends to use the container-based infrastructure in order to speed up the build, we were using it at Sententiaregum as we didn’t need sudo for anything.

As our build setup with a container-based infrastructure and certain PHP versions doesn’t provide the ability to decide about the NodeJS setup, TravisCI uses the software which is shipped by default on a precise: An ancient setup with NodeJS 0.10 and NPM 1.x. The current 3.x series of NPM is much more stable than the ancient NPM 1.x and fixed a lot of misbehavior which has been implemented in the older versions of NPM. Especially code which has been written for the browser will experience issues due to some implementation details of NPM releases prior to the 3.x series.

In order to be able to build the project continuously, I had to find a solution to use a current version of PHP and NodeJS in TravisCI. It seemed as TravisCI used the NVM tool in order to manage NodeJS versions, but the tool itself wasn’t installed properly. The corresponding builds can be found here:

The first build showed what happens when using the nvm command directly inside the build. In the second build I’ve tried to use a generic script in order to use the nvm tool, but according to the build result the legacy environment with NodeJS 0.10 and NPM 1.x was still in use although it told me that NodeJs 6 is in use now.

As the precise isn’t a good environment to work on IMHO and I was tired of trial and error with a pre-installed NVM environment, I tried to use the trusty build environment. The downside of this approach is that the builds are much slower than in container-based infrastructure as containers can’t be used due to the usage of sudo in the environment, but a lot more stuff can be done during the build.

In order to use a trusty based environment, the distribution must be changed and the use of sudo must be declared as obligatory:

In order to be able to use the appropriate version of NodeJS it is the better solution to work with a package manager and override the pre-installed version (which is a NodeJS 4.x btw, but this release is still missing lots of ES2015 features).

This could look like this:

This tiny script removes the pre-installed setup and installs a NodeJS 6 environment which is shipped with a current release of NPM 3.x in order to ensure a current NodeJS version on a PHP build.

In the end the whole config might look like this if you’d like to test against multiple NodeJS versions:

And now you’re able to configure NodeJS and PHP in one build!