DrupalCon 2018: CI Summary
What is CI?
Continuous Integration (CI) is a collection of automated processes that helps developers catch unintended changes early in the development cycle through automatic build and testing of code changes.
Using CI tools developers can write code or apply settings that will test different aspects of their site increasing the confidence the developer has that any changes made will not adversely affect the site performance. A minimal instance of CI is developers spinning up a local environment where they can test the site to make sure that the entire project, including the new code changes, is working as expected. A more comprehensive process may clone the production environment to a temporary server every time a new code commit occurs and runs a whole host of tools that tests compliance to standards, functionality, usability, performance, security, etc.
When to use CI?
Many projects can benefit from CI implementations but a fully comprehensive setup might be far beyond that which is needed or advisable. XKCD asked this question about automation, “Is It Worth the Time?” and provided this chart to demonstrate their point.
The general principle is that you do not want to spend more time automating a task than you would spend performing the task yourself. This means for temporary or short lived projects, certain CI implementations might not be worth it. This chart shows a one for one time savings comparison, which does not include the cost savings of automation. Automation not only reduces the time it takes to perform the task but it also reduces the chance the task might be skipped accidentally.
The two key ideas of when to implement a level of automation are “Will this save me time in the long run?” including the time spent fixing issues that would have been caught if the automation was in place and “Will this reduce loss of service?” as a loss of service has its own cost.
How to implement CI?
Start small. Add tools that make sure that your code is syntactically correct (such as linter-php) and follows coding standards (such as Coder and Code Sniffer). This will ensure that small mistakes are caught before being committed and will improve the readability of your code changes.
Install some sort of unit testing (such as SimpleTest for Drupal 7 or PHPUnit for Drupal 8) and write tests that will verify that critical areas of your site are functioning as expected. Critical areas may include general access to the site, the key reason for the site’s existence, and any area that brings in money. Then write tests for problem areas. If you have to fix a problem more than once, write a test that will verify that it stays fixed, so that you don’t have to manually check it each time as you can rely on your automation to do that for you.
Using these simple tools, even just from the command line, can improve the speed of testing for unintentional changes. Adding a script to your repository that will run these tests before a new commit is accepted is the first step towards CI.
Taking CI further
The next big step is to automate deployment of code changes. Setting up an automated process that pushes code changes, applies database changes, and updates required services can greatly reduce the time it takes to run these steps, reduces the chance of accidentally missing a step, and thus as a result of both reduce the downtime to the site as a whole. Several tools like Jenkins, CircleCI, and Travis CI provide an intuitive, well documented approach to solving this issue.
Adding behavioral testing of site features, adding performance and stress testing on a clone of your production site, and requesting user testing from your client or end users are just a few more ways CI can continue to improve your development cycle and the quality of your site and its code.