Integrating Travis CI with WordPress Theme

February 16, 2017

Travis CI

A tool for automating tests for your Open Source projects. Travis CI is used to build and test the software projects at GitHub. Its written on Ruby and freely available under MIT Licence. Which means we can use the Software without the Restrictions.

So, Here the question is “What we can do with this?. Is it really can be helpful in developing any kind of Software/Application?”.

Yesterday, In a discussion about Open Source with Yathartha, he told about his Open Source Contribution to a Python Module. When he contributed his first code in the repository he got an error of _“Failure: Travis CI build Failed”. _ So then he read about the “Travis CI” that is a kind of Bot that can help you making cool thing faster and better with testing.

The owner of the Repository can set the rules for pushing the code or can create a set of rules. In every commit/push the code is tested with that particular data set.

In the discussion i was thinking of adding the Travis CI to the WordPress Theme Creation. There is a lot of rules for writing the good code in WordPress. There is a GitHub Repository dedicated for WordPress Coding Standards :P. So, Now the Integration part comes.

Integration with WordPress

There is a lot of documentation and tutorial/examples are available in their website on integrating with each language. Lots of languages are supported by Travis CI. Tutorials are available for each of the supported languages eg. PHP integration.

Creating a rule set that check the code with all the coding standards is not an easy task. So I looked over the themes that were created by the WordPress.org itself. So I have looked over the theme “Twenty Seventeen”. I have also looked over the WordPress starter theme _S”. The YAML file is just like a bouncer. I have no idea what is written in the file. But after a lot of struggle I deciphered all the content 😉 .

The YAML file is more like a script. You have to add the details of your tests. Here are some values we have to add in the YAML file:

  • language :  Declaring the Project Language
  • **php : **Declaring the php version to use (if language is PHP)
  • **before_script : **All the dependencies or requirements for the tests are listed in this section, It is more like the “Making a Web Development Environment” .
  • **script : **This section is for the tests commands. If any test return with code 0(zero) then it is passed the test otherwise the Build will show Failure.
# Travis CI (MIT License) configuration file for the solitude WordPress theme.
# @link https://travis-ci.org/
# For use with the solitude WordPress theme.
# @link https://github.com/shubham9411/solitude
# Ditch sudo and use containers.
# @link http://docs.travis-ci.com/user/migrating-from-legacy/#Why-migrate-to-container-based-infrastructure%3F
# @link http://docs.travis-ci.com/user/workers/container-based-infrastructure/#Routing-your-build-to-container-based-infrastructure
sudo: false
# Declare project language.
# @link http://about.travis-ci.org/docs/user/languages/php/
language: php
# Declare versions of PHP to use. Use one decimal max.
# @link http://docs.travis-ci.com/user/build-configuration/
matrix:
fast_finish: true
include:
# Current $required_php_version for WordPress: 5.2.4
# aliased to 5.2.17
- php: '5.2'
# aliased to a recent 5.6.x version
- php: '5.6'
env: SNIFF=1
# aliased to a recent 7.0.x version
- php: '7.0'
# aliased to a recent 7.1.x version
- php: '7.1'
# aliased to a recent hhvm version
- php: 'hhvm'
allow_failures:
- php: 'hhvm'
# Use this to prepare the system to install prerequisites or dependencies.
# e.g. sudo apt-get update.
# Failures in this section will result in build status 'errored'.
# before_install:
# Use this to prepare your build for testing.
# e.g. copy database configurations, environment variables, etc.
# Failures in this section will result in build status 'errored'.
before_script:
- export PHPCS_DIR=/tmp/phpcs
- export SNIFFS_DIR=/tmp/sniffs
# Install CodeSniffer for WordPress Coding Standards checks.
- if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git $PHPCS_DIR; fi
# Install WordPress Coding Standards.
- if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git $SNIFFS_DIR; fi
# Install PHP Compatibility sniffs.
- if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/wimg/PHPCompatibility.git $SNIFFS_DIR/PHPCompatibility; fi
# Set install path for PHPCS sniffs.
# @link https://github.com/squizlabs/PHP_CodeSniffer/blob/4237c2fc98cc838730b76ee9cee316f99286a2a7/CodeSniffer.php#L1941
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs --config-set installed_paths $SNIFFS_DIR; fi
# After CodeSniffer install you should refresh your path.
- if [[ "$SNIFF" == "1" ]]; then phpenv rehash; fi
# Install JSCS: JavaScript Code Style checker.
# @link http://jscs.info/
- if [[ "$SNIFF" == "1" ]]; then npm install -g jscs; fi
# Install JSHint, a JavaScript Code Quality Tool.
# @link http://jshint.com/docs/
- if [[ "$SNIFF" == "1" ]]; then npm install -g jshint; fi
# Pull in the WP Core jshint rules.
- if [[ "$SNIFF" == "1" ]]; then wget https://develop.svn.wordpress.org/trunk/.jshintrc; fi
# Run test script commands.
# Default is specific to project language.
# All commands must exit with code 0 on success. Anything else is considered failure.
script:
# Search for PHP syntax errors.
- find -L . -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l
# Run the theme through JSHint.
- if [[ "$SNIFF" == "1" ]]; then jshint .; fi
# Run the theme through JavaScript Code Style checker.
- if [[ "$SNIFF" == "1" ]]; then jscs .; fi
# WordPress Coding Standards.
# @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards
# @link http://pear.php.net/package/PHP_CodeSniffer/
# -p flag: Show progress of the run.
# -s flag: Show sniff codes in all reports.
# -v flag: Print verbose output.
# -n flag: Do not print warnings. (shortcut for --warning-severity=0)
# --standard: Use WordPress as the standard.
# --extensions: Only sniff PHP files.
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs -p -s -v -n . --standard=./codesniffer.ruleset.xml --extensions=php; fi
view raw .travis.yml hosted with ❤ by GitHub

It take me total of 9 commits to get my build successful pass after adding the .travis.yml file to my project. Means I have not following many rules from the WordPress Coding Standards. BTW the all checks failed were all about adding the function definition comment and the spaces before and after the parentheses 😉 .

Travis Build History
Build Passing Badge

So, Now I can add a badge to my github repository Readme for the last commit. It made me happy today the Badge 🙂 .

 

“Stop stressing. Press publish.”

Thanks for reading if you have any comments or feedback you can reach out to me on twitter.

Up Next