Deploying WordPress Plugins with Travis CI

Travis CI and WordPress

Ever since I built my first WordPress plugin, the process of deploying the code to the WordPress.org Subversion repository has been a painful one. With Git as the widespread VCS flavor of choice, switching to SVN for plugin deployment has always been jarring and felt alien to my workflow. In this post I’m going to show you how I recently leveraged one of our existing tools to automatically deploy a plugin, and thus meaning I never have to touch SVN again!

There are lots of scripts out there to take care of the heavy lifting at deploy time, and we actually use a custom build script for our plugins, based on a specific repo structure, that also handles the deployment to WordPress.org. However, I’ve recently been working on our Mergebot application which has automatic deployments to the server when code is pushed to the master branch, I’ve gotten used to not having to physically do anything to deploy the code!

The Mergebot WordPress plugin has been having releases almost daily in the run up to opening up the beta, and this has meant performing the manual deployment each time. Sure, running a single command-line script is not much effort, but hey, I’m a lazy streamlined developer!

I had a eureka moment the other week whilst listening to a talk on deployments by Michael Heap. He was going through different solutions for deploying applications with varying levels of complexity, and started to talk about Continuous Integration with a tool like Jenkins or Travis CI. We use Travis CI only to run unit tests on our plugins during development and before releases. I realised we were missing a trick and only utilizing part of Travis’ potential!

What is Continuous Integration?

This is the process of developers maintaining a shared repository of code, which is committed to frequently (typically daily), and is then automatically built and tested to ensure everything is working and to catch issues early. A CI server is used to do the following:

  • Check out the code
  • Build the code
  • Run tests
  • Automate deployment

For the most part this is how we use Travis for our plugins. If you haven’t seen Travis before then I recommend taking a look. It’s free for open source projects, so public repositories on GitHub. The simplest use case, and one we do for all our plugins, is getting Travis to run our unit tests for every push and every pull request.

Setting up PHPUnit for unit testing a plugin is relatively simple but probably deserves its own post. As responsible developers we should be running these tests during development to ensure new code doesn’t affect existing code. Having Travis run these tests automatically so we don’t have to remember is awesome! Adding deployment to the list of tasks Travis performs turns it into a continuous delivery service as well, automation FTW!

Deployments with Travis

Travis has many service integrations to deploy to, such as S3, GitHub (in the form of release tags), Heroku, and lots more. However, there is nothing specific for the WordPress plugin repository, which is understandable. But luckily Travis provides the ability to run a deployment script as a post-build task so we can accomplish our own bespoke deployments.

The following part of this post is a tutorial to get you up and running using Travis to automatically deploy your plugin to the WordPress repo. I’m going to make a few assumptions for this tutorial to work:

  • You have a WordPress plugin on the repo, but also hosted on GitHub
  • You are already using Travis
  • Travis runs some form of build script or your GitHub repo is in a deployable state

Deployment Script

First things first, let’s add a new script to your project which will eventually perform the deployment. Our GitHub repos are set up with the actual plugin code inside a src directory, here’s an abbreviated example:

|-- src
|   |-- mergebot
|   |   |-- mergebot.php
|   |   |-- readme.txt
|-- tests
|-- composer.json
|-- .travis.yml

So I’ll just create a new scripts directory and a new file inside it, with the correct permissions so the Travis user can execute it:

mkdir scripts
touch scripts/deploy.sh
chmod ugo+x ./scripts/deploy.sh

Travis Configuration

Then we need to tell Travis to actually run the script as part of the deployment process, so we need to make some edits to our .travis.yml file, adding this section to the bottom:

deploy:
  #
  # Auto-deploys the built plugin to WordPress.org on push to master branch
  #
  provider: script
  skip_cleanup: true
  script: ./scripts/deploy.sh
  on:
    branch: master
    php: 7.0

The provider option is set to script as we are doing a custom deployment using our script we provide with the script option path.

I have enabled the skip_cleanup option, which just tells Travis to not bother cleaning up files and assets that might have been created during testing. This is because our plugin build script produces a zip file that we will use as our deployment source, so there is no need to get Travis to clean up files required for deployment.

We then tell Travis to only run this deployment after a build on the master branch, and as we have multiple configurations in our build matrix, we tell Travis to only deploy on PHP 7.0, so the deployment only happens once.

Deployment

The script.sh is quite specific to our plugin in places, but I’ve stripped it back and will run through what it does and the places you will want to tweak:

There are three conditionals at the start of the script that ensure the script is being run by Travis, the WP_ORG_PASSWORD variable is set, and we are on the correct master branch. The password is set as a secure environment variable in Travis so we don’t have to store it in Git. It also doesn’t get displayed in the Travis build log. You set the variable from the settings screen in Travis:

Travis environment setting

Lines 18-24 set a bunch of variables that are used throughout the script, some of which will need to be set specific to your build process:

  • WP_ORG_USERNAME – our WordPress.org username (could be set as an environment variable)
  • PLUGIN – the plugin slug on WordPress.org and the same in the repo
  • PROJECT_ROOT – the root path of the checked out repo in the Travis container
  • PLUGIN_BUILDS_PATH – the directory where our built plugin zip is stored
  • PLUGIN_BUILD_CONFIG_PATH – the build config directory
  • VERSION – the built plugin version, derived from the version in the src/ dir
  • ZIP_FILE – the path to the built zip file, generated earlier on by Travis

The script then checks that the zip file exists, and also ensures that the version we want to deploy doesn’t already exist as a tagged version in the WordPress plugin SVN repository, and therefore has already been deployed.

The zip file is then unzipped, the SVN repository checked out to a local directory, and the SVN fun begins! First of all we move the checked out trunk directory to another location so we can use it later. We then recreate the directory and copy all of our built plugin files into it.

At this point, we really just want to move the checked out svn\trunk\.svn SVN special directory into our new trunk so SVN can work out what has changed. However, Travis containers by default have installed the official Ubuntu packages (precise) of Subversion, which is currently 1.6.17. Before Subversion 1.7, all directories in a working copy have a .svn config directory, which means that we can’t just move back the directory from the root of trunk. Lines 67 – 77 goes through all of the directories inside the checked out trunk, and copies the svn directory to the new trunk, if the path still exists. Nasty, huh?

Once that’s done, we create a new tag directory for the version we are deploying, and run some grep magic with svn stat a couple of times, basically to add new files to working copy, and remove others that have been deleted. Then we commit back up to the repository, passing in our WordPress.org credentials for authentication. We also pass the --no-auth-cache flag to stop SVN asking for us to store our credentials, as there will be no user to respond in the Travis container, and the build will stall and effectively never complete.

A little bit of cleanup to finish, and we are done: Travis deploying our plugin to WordPress.org, awesome!

Debugging Travis

I found working on this quite challenging, as even though you could test running deploy.sh locally (most of the time with the svn ci line commented out!), the Travis environment sometimes did things I wasn’t expecting, and you have to trigger builds and wait for them to run to test things out. However, a great way to debug this more easily is running Travis locally with Docker. This was invaluable in helping identify issues, such as the SVN 1.6 version issue.

After I had installed Docker and downloaded the travis-php image (this takes a while!), I used this set of commands to recreate my Travis build:

If your GitHub repo is private, just add in your username and password as basic auth credentials to the clone URL. Again, some of the commands are specific to Mergebot and our build process, but they give you an idea of what you need to do to recreate the process.

Dependency Management

Two of the main problems I faced during the development were around dependencies. Mergebot uses a couple of third party packages that we inject into the built plugin’s vendor dir with Composer and a custom directory installer package. But it turned out our build process was running composer install too late, therefore the vendor didn’t exist in the built plugin. This wasn’t an issue when we were just running our unit tests with Travis or building the plugin locally for deployment. But running through the process with Travis highlighted the issue and our build process needed to be tweaked.

As the script was almost complete and ready to go, I was investigating the output of svn stat to see if the changed files were exactly the same as if I ran it locally. It turned out that some asset files which were generated by Grunt at build time were flagged as modified. I didn’t want to start deploying code built on Travis that resulted in unexpected file changes.

So after a bit of digging I got to the bottom of it. I fully expected the results of npm install on any server to be the same, as per the versions set in package.json. However, unlike Composer, npm has no lock file (unless you use shrinkwrap), and therefore dependencies of your required packages can vary from install to install. Yarn to the rescue! Yarn is easy to swap in, and creates a yarn.lock which ensures complete version parity for the dependencies anywhere they are installed.

Post Deployment

An automated task isn’t complete without a notification, and as we are Slack users here at Delicious Brains HQ, I added a message after a successful deployment. Slack makes it super easy to add incoming data to channels.

You will need to add a new Slack WebHook to get your team’s Slack URL via Apps & Integrations > Manage > Custom Integrations > Incoming WebHooks, and adding a new configuration. Here is the code I added to the end of the deployment script:

curl -X POST \
-H 'Content-type: application/json' \
--data '{"username": "Travis CI","icon_url": "https://a.slack-edge.com/0180/img/services/travis_48.png","text": "Mergebot plugin version '"$VERSION"' deployed to <https://wordpress.org/plugins/mergebot/|wp.org> :tada:"}' \
https://hooks.slack.com/services/XXXXXXXXXXXXXXXX/xxxxxxxxxxxxxxxxxxxx

Travis Slack deployment notification

I hope that has proved useful. How do you handle deploying WordPress plugins, have you gone fully automated yet? I’d be interested to know if people are using other CI or deployment solutions for this problem. Let me know in the comments below.

About the Author

Iain Poulson

Iain is a WordPress and PHP developer from England. He builds free and premium plugins, as well as occasionally blogging about WordPress. Moonlights as a PhpStorm evangelist.

  • Looks like fun, thanks for sharing 🙂 Using Travis for this makes a lot of sense.

    Since the deploy requires a tag, you might consider changing branch: master to tags: true in your Travis deploy config so the deploy script is only triggered on new releases rather than any commit to the master branch. You could also use the TRAVIS_TAG environment variable for the version instead of the utility script to parse it.

    This could speed up the build some if Travis is installing extra dependencies for the deploy, as is the case when deploying to S3, for example. In this case it might not make such a big difference as it is a script deploy.

    Also, it would be nice if the WP_ORG_USERNAME and PLUGIN vars were set in the environment as well (although no need to be secret of course), so that the same script could be used for other plugins without the need for modification 😉

    • Hey Evan, thanks for commenting and good ideas!

      Great point about using tags, I will definitely look to implement that up and update the post.

  • Jon

    Iain thanks so much for sharing this!

    Ironically this seems exponentially easier than remembering (ie. relearning) how to do git>svn every time we need to push an update.

  • It took me 8 hours of searching to find this, but I’m so glad I did. Thanks, hoss.

    • Hey Josh, no problem, glad we could help!

      Out of curiosity, what were you searching for when you found it?

      • managing wordpress plugins git which actually landed me on a different page, and I believe I came here through a link in the Disqus comments.

        • After beginning to setup my own build with Travis I continued searching and eventually landed on a slide deck which pointed to a repo by the one and only Ben Balter, which then lead me to https://github.com/wp-cli/sample-plugin, which uses a blessed CLI to automate the process of getting a plugin setup for unit testing, including integration with Travis, Circle, example tests, a file blacklist for WordPress.org deployments, what looks to be Composer support and is regularly updated. Wonder where I’ll end up next… Haha.

          • Cheers Josh!

          • After setting up my test rig using the WP-CLI scaffold I saw they were using “extends” in their sample tests so I’ve started customizing to provide a more appropriate test rig using Kahlah and Patchwork. My codebase has one hard JS dependency and is written using procedural programming with mostly pure functions and namespaces for encapsulation.

          • Sample plugin unit test using “describe-it” syntax a la Jasmine and friends:


            describe('hyperdrive', function () {
            describe('enter_hyperspace()', function () {
            it('echos empty script given empty string', function () {
            $closure = function () { enter_hyperspace(''); };
            expect($closure)->toEcho('');
            });
            });
            });

  • We need to use something like this for Pods, do you have a full example of a github repo using this whole process all built? Or is that only in your private repos?

  • strarsis

    Managing WordPress plugins and themes using composer:
    Bedrock (https://roots.io/bedrock/) does exactly this!
    Also see this post for more details: https://roots.io/wordpress-plugins-with-composer/ .