Automagic WordPress Plugin Deployment with Codeship

WordPress plugin development is succinct and powerful. Other than the plugin descriptor and a README, you can jump right into writing problem solving code.

Useful plugins with only one line of (non-comment) code exist and are in circulation, like Disable XML-RPC which has hundreds of thousands of downloads.

So it’s all fantastic, but if you want those downloads you’ll probably need to get your code into the WordPress plugin repository, and there’s a speed bump there:

WordPress forces us to use SVN and a specific directory structure to deploy to their repository.

As a Git user, I hate the idea of having to commit and push, then pull or copy the files into another couple of directories (yep, duplication…), and commit and push again, every time I want to deploy an update. I’ll also have to figure out SVN commands that I’m not familiar with and clone two repositories to get to work in a new environment. Redundancy sucks.

Enter Codeship: a plug ‘n’ play testing and deployment solution which doesn’t impose any changes to your environment or deployment process.

The only assumption is that you’re using Git (they may support other VCSs, you should check).

Pushing to master (or a branch of your choice) is deploying to the WordPress plugin repository.

Brilliantly elegant, automagic WordPress plugin deployment.

Codeship has preset deployment configurations so you can just fill in the blanks (i.e. for Heroku), but the WordPress plugin repository is not one of them. That being said, I had little trouble using their custom script functionality to get the job done, and that’s what this guide will take you through.

Edit: this guide has undergone a breaking change on 2015/08/30. If you worked through it around this time or up to a month earlier, consider going through it again to ensure smooth operation. It particularly regards the assumed directory structure, and involves a change in the deployment script.

 

Requirements and assumptions

Before getting started, ensure each of the following requirements are met:

  • You have an account on a Git host that is supported by Codeship. The supported hosts at the time of writing this post are: GitHub; Bitbucket; Cloud Foundry; and App Engine.
  • You have a Codeship account with at least one connected Git host. The free Codeship plan works perfectly.
  • Your WordPress plugin code is in a Git repository on a Git host that you’ve connected to your Codeship account.

Your particular development environment or practices may differ from that which this guide is assuming. As such you may need to adjust some parts of the solution, but if you understand what’s going on then that shouldn’t be difficult at all.

While bearing that in mind, know that this guide assumes:

  • You push releases to the master branch.
    • Codeship can use branches other than master, and can even use wildcards like release-*, so it should be able to fit your preference, but this guide is written assuming master is in use.
  • You only push to the aforementioned branch if you’re prepared to deploy that code to the WordPress plugin repository (i.e. no partial, untested or incomplete code commits).
  • Your plugin code lies inside the root of your directory.
  • You maintain the Stable Tag field of the plugin’s readme.txt file appropriately, as WordPress (and this guide) uses it as your plugin’s version number.

You can check out the simple Formataway WordPress plugin’s Git repository as an example that conforms to this guide’s assumptions.

Configurations can be made of course, but this guide can’t be written considering them. With due thought you should be able to adapt this guide to your needs easily, and you can ask me for help if you can’t find a way.

The Codeship project

Things get pretty simple from here on in.

  1. Create a new project, selecting the appropriate repository of course.
  2. We won’t be using any of their testing functionality as part of this guide, so for that section select custom commands and remove the initial “Test Pipeline” so it won’t try to test anything.
  3. Continue to the deployment section and select the custom script option. The deployment commands follow:
    PLUGIN_VERSION=`sed -n "s/.*Stable tag: \([0-9]*\.[0-9]*\(\.[0-9]*\)*\).*/\1/p" readme.txt`
    
    svn co http://plugins.svn.wordpress.org/$PLUGIN_NAME svn
    cd svn
    
    rm -rf trunk tags/$PLUGIN_VERSION
    mkdir trunk
    mv ../* trunk 2>/dev/null; true
    cp -r trunk tags/$PLUGIN_VERSION
    
    svn add --force * --auto-props --parents --depth infinity -q
    svn ci --no-auth-cache --username=$SVN_USERNAME --password=$SVN_PASSWORD -m "AUTOMATED COMMIT, PLUGIN VERSION: $PLUGIN_VERSION"
  4. Continue to the environment section and fill in the blanks from above using your WordPress plugin repository details:
    • PLUGIN_NAME (as explained in the assumptions section above);
    • SVN_USERNAME (as used to log into WordPress.org); and
    • SVN_PASSWORD.
  5. That’s all! Tinker with the project settings more if you like and give it a spin by pushing some changes to your deployment branch.
  6. Note that this only affects the trunk and tags directories of your SVN repository, as that’s the minimum that’s needed to operate on the plugin repository.
    • Beware that it will delete existing files within these two directories (however only for the version being updated in the tags directory – it won’t overwrite old versions (unless you use an old version in your Stable Tag for some reason)).
    • You’ll need to manage other directories like assets on your own, but with a bit of thought it shouldn’t be hard to extend this deployment script to consider specific folders. Ask me for help if you need some.

When GitHub propagates the news that your branch was updated, Codeship will automagically fire up and run the above script.

That script will find your plugin’s version number, update the SVN repository in agreement with WordPress’ required folder structure, commit and finally push.

WordPress will catch that update quickly (hopefully) and update your plugin’s listing on the plugin repository!

Beautiful. You can watch Codeship do it’s thing live right after you push – it’s fantastic.

Author: Bilal Akil

Husband of Joy. Programming hobbyist and professional.

Leave a Reply

Your email address will not be published. Required fields are marked *