Adding Travis CI support into your plugin

Adding Travis CI support into your plugin

by David Mudrák -
Number of replies: 23
Core developers колдонуучунун сүрөтү Documentation writers колдонуучунун сүрөтү Moodle HQ колдонуучунун сүрөтү Particularly helpful Moodlers колдонуучунун сүрөтү Peer reviewers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү Plugins guardians колдонуучунун сүрөтү Testers колдонуучунун сүрөтү Translators колдонуучунун сүрөтү
Build passing icon generated by travis

If you have seen our latest community development meeting you may remember Andrew Nicols talking about Travis CI integration with Moodle. I really liked the idea as I saw its great potential for Moodle plugins development.

Let us say you are maintaining a plugin, ideally with some unit tests and behat tests attached. Apart from those, there are other things that can be automatically checked too - such as following the Moodle coding style. Travis allows you to continuously run all tests and checks in sandbox environments, with various PHP versions, Moodle versions, databases etc. For open source projects, using Travis is free. For projects hosted on Github, the overall setup is incredibly simple. So it seems to be a perfect option for most Moodle plugins. Finally, using Travis CI for Moodle plugins development is not a new kid on the block - there are traces back to discussions from 2012.

Technically, adding Travis support to your Moodle plugin is as simple as adding a configuration file .travis.yml into the root directory of your plugin and allowing Travis builds to be triggered on certain events, such as pushing commits to the plugin's repository at Github. This configuration file contains all the information needed to set up and execute all wanted tests and checks. Somehow, you must describe what PHP version, what database type (such as MySQL or PostgreSQL) and what Moodle version (branch) you want to use as the testing environment and what tests should be executed. There are good tutorials on all these steps.

Luckily, no need to re-invent the wheel. There is a great tool developed by Mark Nielsen called moodle-plugin-ci that does all the magic for you. The tool provides a template of the travis configuration file - perfectly documented - as well as excellent usage information.

I successfully used Mark's tool to add Travis CI support into my Poster module easily. In fact, I was able to do that just in couple of minutes while my kids were cleaning their teeth before going to bed! I just made a trivial modification of the default template so that my plugin was tested against all 8 combinations of MySQL / PostgreSQL and Moodle 2.7 / 2.8 / 2.9 / 3.0. I am sure there is more elegant way to define such matrix in the YML file - but hey, how long do you brush your teeth for?

When I came back to the home office after having read the next chapter of The Magic Faraway Tree, I was just thrilled. It did not take long to travis to run all the builds and I could just enjoy browsing the detailed build logs.

Overview of build jobs at travis

I'll be honest with you. This is awesome. I am big fan of test driven development and I've been dreaming about this "build passing" icon for my Moodle plugins for ages. And I am really happy that I can now start adding Travis CI support for my other software projects. Big thanks to Mark for his great contribution!

Continuous integration is a vital practice in Moodle development. Having it adopted for plugins development is a good sign of the developers' responsibility and their focus on the code quality. Indeed, all these automated builds are only as good as the actual tests executed behind the scene. But still. This is just awesome smile

Average of ratings:Useful (15)
In reply to David Mudrák

Re: Adding Travis CI support into your plugin

by Mark Nielsen -

Usage / VersioningFirst, thanks for the kind words David!  And wow, your experience was exactly what I was hoping to achieve when I was designing this tool: easily and quickly setup a plugin in Travis CI without having to become a CI guru - plus, to do it in less time it takes for your kids to brush their teeth hah!

Just some background information for the curious.  David Scotson pointed out to me the idea of running Moodle plugins in Travis CI.  I noticed everyone was making very clever .travis.yml files to accomplish this task.  But, as I was looking at all the various YAML files to implement our own, I quickly realized that the YAML files were doing far too much work to get things running.  There isn't a problem with that until you want to collaborate.  It ins't pratical to manage a recommended YAML file and then require potentially hundreds of projects to then update their YAML file whenever there is a change.  So, moodle-plugin-ci was created to house all the logic for installing the test site and running each test.  This results in a very generic YAML file that doesn't require a lot of maintenance.  Another trick for keeping the moodle-plugin-ci tool itself updated is by using Composer.  The moodle-plugin-ci is installed with composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1 - note the ^1.  This means that Composer will install the latest version of moodle-plugin-ci up until version 2.0.0.  This is good because you will automatically get bug fixes and minor features, but you will not get changes that break backwards compatibility.  This helps to ensure that your builds don't suddenly break because of an update in the moodle-plugin-ci tool.  When a new major version of moodle-plugin-ci is released, like 2.0.0, then you just need to modify your YAML file to be ^2 and then verify that no further changes are needed.  For more information about how the project is versioned, see Usage / Versioning.

Also, there is a known issue with the code checker command.  I'm running Moodle's coding standard through PHP_CodeSniffer 2 and it appeared that everything was working great.  Later, while I was trying to upgrade local_codechecker to run with PHP_CodeSniffer 2, I realized that not all the sniffs in the Moodle coding standard still work.  So, once local_codecheder is upgraded in CONTRIB-5732, then I'll update moodle-plugin-ci with the updated coding standard.  One of the main advantage with PHP_CodeSniffer 2 is that it comes with a new feature where sniffs can automatically fix themselves, which is very handy.

Hope this helps the plugin community and please feel free to file ideas or problems in GitHub.

Cheers!

Average of ratings:Useful (8)
In reply to Mark Nielsen

Re: Adding Travis CI support into your plugin

by Eoin Campbell -
Core developers колдонуучунун сүрөтү Particularly helpful Moodlers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү

I incorporated Travis checking in my plugin according to the instructions, and it works a treat! Many thanks for making it so simple.

However, I am getting an error message in some JavaScript code warning about the use of the string concatenation operator at the end of a line, cf. https://travis-ci.org/ecampbell/moodle-atto_wordimport/jobs/92241555. The lines in question are of the form:

IMAGETEMPLATE = '' +
        '<img src="{{url}}" alt="{{alt}}" ' +
...

The source file is at https://github.com/ecampbell/moodle-atto_wordimport/blob/master/yui/src/button/js/button.js#L37

Can I get codechecker to accept this usage, or if not, how should it be written instead? It seems to be quite common practice in the Atto editor plugin JavaScript files.


In reply to Eoin Campbell

Re: Adding Travis CI support into your plugin

by Mark Nielsen -

So that is coming from Moodle's coding standard in the local_codechecker plugin.  I believe your usage of the concatenation operator is valid, so you may want to file a bug for it in the CONTRIB project.  At the very least, you may get feedback on an alternative solution.

For a quick fix, you could reformat the code to single line statements where you continuously build on the IMAGETEMPLATE variable.  Or you can choose to ignore the file altogether.

Hope that helps, Cheers.

Average of ratings:Useful (1)
In reply to Mark Nielsen

Re: Adding Travis CI support into your plugin

by Brendan Heywood -
Core developers колдонуучунун сүрөтү Peer reviewers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү

I've had to ignore a few different warnings for different reasons, some in code standards check and some in the mess detector check. My philosophy is to make the exclusions as small as possible, ie a small chunk of code rather than the whole file. These are the one's I've used the most:


// @codingStandardsIgnoreStart

something odd

// @codingStandardsIgnoreEnd


And this one has come up a lot when extending moodle interfaces:

    /**

     * Some abstract method I'm implementing

     * @param string $param1 A param in the iterface which I don't use in the impl

     *

     * @SuppressWarnings("unused")

     */

See also:

http://phpmd.org/documentation/suppress-warnings.html



Average of ratings:Useful (2)
In reply to Brendan Heywood

Re: Adding Travis CI support into your plugin

by David Scotson -

I think the following is a slightly more specific way to suppress that Warning:


@SuppressWarnings(PHPMD.UnusedFormalParameter)


using just "unused" will block a few other warnings that contain the string "unused".


It looks like they might in future let you specify exactly which parameter you don't intend to use, which would be an improvement, and possibly using @inheritdoc might also work (since it assumes that as you are overriding a method, you have no control over the number of parameters).


Average of ratings:Useful (1)
In reply to David Scotson

Re: Adding Travis CI support into your plugin

by Gareth J Barnard -
Core developers колдонуучунун сүрөтү Particularly helpful Moodlers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү

After lots of work and suppression of bitwise operator checking for Code Checker, I've finally got Collapsed Topics passing.  Thank you to everybody involved with this.  It has made me really think about the code and as a direct result, the CSS is simpler.

Attachment 2015-12-18 14_49_24-gjb2048_moodle-format_topcoll - Travis CI.png
Average of ratings:Useful (4)
In reply to Eoin Campbell

Re: Adding Travis CI support into your plugin

by Paul Nicholls -

In addition to the string concatenation operator issue, I'm also getting errors from codechecker for RegExp literals (demanding spaces surrounding the / at the start and end of the RegExp literal, as it sees the slash as an operator).

I'd rather keep the compile-on-load optimisation from using a literal rather than the RegExp constructor (small though it may be), so for now I'll just ignore it - but does anyone know how feasible it is to make it recognise that the slashes are being used in RegExp literals rather than as operators, and thus allow them to not have surrounding whitespace?  I'll probably report it as a CONTRIB issue either way, just so that it's documented somewhere other than the forums, but it'd be good to know whether it's likely to be fixable.

In reply to Paul Nicholls

Re: Adding Travis CI support into your plugin

by Mark Nielsen -

Don't have a fix for you, but if you do make an issue, you should probably link it to MDL-52127 which would probably fix a lot of problems with enforcing coding styles in JS.

In reply to Mark Nielsen

Re: Adding Travis CI support into your plugin

by Mike Churchward -
Core developers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү Testers колдонуучунун сүрөтү

Hey Mark -

This is awesome. Can I use this with a non-standard development branch though? I am trying it on the questionnaire plugin (for example), using a current work in progress branch named "CONTRIB-5900_30".

I am getting the error:

[InvalidArgumentException]
Invalid Moodle branch: CONTRIB-5900_30

Is this because it is not one of the standard Moodle stable branches? Or something else?

mike

In reply to Mike Churchward

Re: Adding Travis CI support into your plugin

by Mike Churchward -
Core developers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү Testers колдонуучунун сүрөтү

I may have answered my own question...

It looks like the branch I specify in the ".travis.yaml" file is not the one I'm testing in the plug-in branch? I changed it to "MOODLE_30_STABLE", and pushed my "CONTRIB-5900_30" branch and it ran the tests on that branch.

mike

In reply to Mike Churchward

Re: Adding Travis CI support into your plugin

by Mark Nielsen -

Yes, that's correct.  In the YAML file you want to specify the Moodle branch in which to test your plugin.

This might be helpful: several plugin repositories mimic Moodle's branching strategy, in that they have a master, MOODLE_30_STABLE, MOODLE_29_STABLE, etc branches.  So, in each branch, you can add a .travis.yaml file and set the branch to the appropriate Moodle version.  Example: in the plugin's MOODLE_29_STABLE branch, I would edit the YAML file to use Moodle's MOODLE_29_STABLE branch to make sure that my plugin is compatible with Moodle 2.9.  Then, if you need fix something in 2.9, you can branch from your MOODLE_29_STABLE branch, make the fix, push the branch, and Travis CI will test your fix against Moodle 2.9.  This will also work if someone forks your repository and sends you a pull request.

Cheers!

In reply to Mark Nielsen

Re: Adding Travis CI support into your plugin

by Eric Merrill -
Core developers колдонуучунун сүрөтү Moodle HQ колдонуучунун сүрөтү Peer reviewers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү Testers колдонуучунун сүрөтү
I managed to get coveralls.io code coverage analysis working with an example Moodle plugin. You can see the result here:

https://coveralls.io/github/merrill-oakland/moodle-local_coverage

You can look at the project with the yml below:
https://github.com/merrill-oakland/moodle-local_coverage


It requires quite a bit of hackery in the travis.yml, but I would be interested in working with Mark and others to see if maybe we can incorporate this capability into the moodle-plugin-ci tool.

Average of ratings:Useful (3)
In reply to David Mudrák

Re: Adding Travis CI support into your plugin

by Antonello Moro -
Hallo,
My plugin built successfyully on Travis-CI, but plugin-bot found phpdoc errors in my code. I'm fixing these problems, but it would be nice and quicker if Travis-CI was able to detect those phpdocs as well. Should I just modify my yml template in some way? I naively tried adding the line "- moodle-plugin-ci phpdoc" but it did not work smile
In reply to David Mudrák

Re: Adding Travis CI support into your plugin

by Christopher Sangwin -
Particularly helpful Moodlers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү

This looks very useful indeed.  Thank you.

I've made a start on qtype_stack, but fear I have a long way to go!  I shall persevere....

Great tool, and much appreciated.

Chris


In reply to Christopher Sangwin

Re: Adding Travis CI support into your plugin

by Christopher Sangwin -
Particularly helpful Moodlers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү


I've reached a block in adding Travis CI support into my plugin, qtype_stack.

The STACK question type calls the external application "maxima" which must be installed on the server.  

If I use the option sudo: false (as supplied in the travis.default.yml) then I can't issue a commands

before_install:
  - sudo apt-get update
  - sudo apt-get install maxima maxima-share texinfo

If I use sudo: true, or sudo: required, then the apt-get lines are fine, but then I get an error from composer

Symfony\Component\Process\Exception\ProcessFailedException

See this URL for the full output: https://travis-ci.org/maths/moodle-qtype_stack/jobs/135825916

Can anyone help with this?

My travis.yml is online at https://github.com/maths/moodle-qtype_stack/blob/master/.travis.yml

Thanks,

Chris


In reply to Christopher Sangwin

Re: Adding Travis CI support into your plugin

by Mark Nielsen -

Sorry, not sure why the composer install is failing.  Maybe try re-building it a couple of times to see if you get a successful build.  After a successful one, the composer dependencies are cached so likely you wont see the error again.

In reply to Christopher Sangwin

Re: Adding Travis CI support into your plugin

by Eloy Lafuente (stronk7) -
Core developers колдонуучунун сүрөтү Documentation writers колдонуучунун сүрөтү Moodle HQ колдонуучунун сүрөтү Particularly helpful Moodlers колдонуучунун сүрөтү Peer reviewers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү Testers колдонуучунун сүрөтү

Hi,

just to comment that I also have been experiencing some composer errors today (some hours after your job above).

Maybe it could be related with some problems reported today within travis infrastructure @ https://www.traviscistatus.com

In any case, right now the builds are working again here, so I assumed it was just a temp problem somewhere in travis/github. They happen from time to time.

Ciao smile

Average of ratings:Useful (1)
In reply to Eloy Lafuente (stronk7)

Re: Adding Travis CI support into your plugin

by Christopher Sangwin -
Particularly helpful Moodlers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү


Thanks for responding.

I followed your advice Mark: keep doing the same thing and the behaviour will change. wink  It did.  Thanks.

Chris


In reply to David Mudrák

Re: Adding Travis CI support into your plugin

by Luuk Verhoeven -
Core developers колдонуучунун сүрөтү Plugin developers колдонуучунун сүрөтү

Someone any idea how I can fix this error? It looks like shifter is failing to build:

Attachment 2016-06-21_16-44-20.png