1. Code
  2. Coding Fundamentals
  3. Testing

How to Deploy a WordPress Plugin From TravisCI to WordPress.org

Scroll to top

Not everyone likes subversion. If you use Git to manage WordPress plugin development, keeping your Git repo and the WordPress.org SVN repo in sync is tedious. Luckily, we can use TravisCI deployment provider to automate SVN deployment after tests.

Prerequisites

You need these before moving on:

  1. GitHub account
  2. TravisCI account
  3. Plugin source code
  4. WordPress.org plugin SVN repo (You get this after plugin review approval.)

First Push to GitHub

In order to use TravisCI, we have to host the plugin repository on GitHub.

First, create a new repository on GitHub by going to this page and filling in the repository name.

create new GitHub repositorycreate new GitHub repositorycreate new GitHub repository
GitHub new repository pageGitHub new repository pageGitHub new repository page

Then, we are going to commit all plugin files into Git and push it to this GitHub repository. Remember to replace the remote URL with yours.

1
$ cd /path/to/plugin/directory
2
$ git init
3
$ git add -A
4
$ git commit -m "first commit"
5
$ git remote add origin https://github.com/TangRufus/tutsplus-dpl-demo.git
6
$ git push -u origin master

Connecting TravisCI

Connect your GitHub repository with TravisCI by going to your TravisCI account page and switching on your repository. Click Sync account in the upper right corner if your newly created repository doesn't show up in the list.

TravisCI account settingsTravisCI account settingsTravisCI account settings

You're all set. Every time you push new commits or someone makes pull requests to GitHub, it will trigger a build on TravisCI.

First Build on TravisCI

TravisCI uses a YAML file named .travis.yml in the root of your repository to customize the builds. Learn more from the build customizing document to see how you can control the build lifecycle.

This is a basic configuration file that instructs TravisCI to run builds on PHP 7.0 and 7.1. Since testing is out of the scope of this tutorial, I replaced the actual test commands with echo 'Tested'

1
# .travis.yml
2
3
language: php
4
5
sudo: false
6
7
php:
8
  - 7.0
9
  - 7.1
10
11
script:
12
  # Run your tests here.
13
  - echo 'Tested'

Commit this file and push it to GitHub. TravisCI builds will be triggered automatically.

1
$ git add .travis.yml
2
$ git ci -am "Add .travis.yml"
3
$ git push origin master
TravisCI current buildTravisCI current buildTravisCI current build

Adding Deployment Provider Configuration

Deployment providers run if the tests passed and pre-defined conditionals (the on section) are met. TravisCI provides nearly 40 official deployment providers. Sadly, none of them support WordPress.org subversion repositories. Thus, I made my own custom provider. You can find it on GitHub and its pull request.

To use it, add these lines to the end of .travis.yml.

1
before_deploy:
2
  - mkdir build
3
  - cp LICENSE build
4
  - cp README.txt build
5
  - cp remove-medium-cross-links.php build
6
7
deploy:
8
  - provider: wordpress-plugin
9
    edge:
10
      source: TypistTech/dpl
11
      branch: add-wordpress-plugin-deployment
12
    on:
13
      php: 7.1
14
      tags: true
15
      repo: TangRufus/tutsplus-dpl-demo
16
    slug: remove-medium-cross-links
17
    username: tangrufus
18
    password: $WORDPRESS_ORG_PASSWORD
19
    build_dir: build 

Configuration Explanation

before_deploy

The before_deploy section copies three files into the build directory which is to be checked into the WordPress.org subversion repository. In the real world, this is where you want to run gulp or grunt tasks to prepare the production-ready plugin. 

Don't copy test files or any unnecessary files into build as the whole build directory is released to users, and users don't need your test files.

provider and edge

We tell TravisCI to install my provider from the add-wordpress-plugin-deployment branch https://github.com/TypistTech/dpl. Once the pull request has been merged, the edge part is unnecessary.

on

The on section controls whether a deployment should be performed. Deployment is triggered only when all requirements are met.

In the above example, we deploy to WordPress.org only when:

  1. it's a PHP 7.1 build
  2. it's a tagged commit
  3. it's triggered by the TangRufus/tutsplus-dpl-demo repo (forks and pull requests would not trigger deployment)

slug

The plugin's slug. 

If your plugin's URL is https://wordpress.org/plugins/my-awesome-plugin/, then my-awesome-plugin is the plugin's slug. 

username

Your WordPress.org account username with which you submitted the plugin for review approval.

password

The password of the WordPress.org account. Never save this password in the .travis.yml in plain text!

In the above example, we use the $WORDPRESS_ORG_PASSWORD environment variable, which can be set on the TravisCI web dashboard. Choose Settings from the cog menu, and click on Add in the Environment Variables section. Never enable the "Display value in build log" option!

TravisCI-environment-variables-settingsTravisCI-environment-variables-settingsTravisCI-environment-variables-settings

build_dir

Everything in this directory will be committed to the WordPress.org subversion repository, i.e. will be included in the downloadable zip file. 

We set this to build because we have copied plugin files into build during before_deploy.

Tagging

Assume that we have fixed some bugs and are ready to publish a new version to WordPress.org.

These commands commit the changes to the master branch but do not trigger a deployment:

1
$ git add -A
2
$ git commit -am "Bug fix"
3
$ git push origin master

Only tagged commits trigger deployment:

1
$ git tag -a 1.0.17 -m "Version bump 1.0.17"
2
$ git push origin master --tags

Results

Head back to TravisCI and open the PHP 7.1 build log. You should see a deployment is logged.

1
Building dpl gem locally with source TypistTech/dpl and branch add-wordpress-plugin-deployment
2
Installing deploy dependencies
3
4
!!! WordPress Plugin support is experimental !!!
5
6
7
Preparing deploy
8
Finding configuration for WordPress plugin deployment...
9
Slug: remove-medium-cross-links
10
Username: tangrufus
11
Password found
12
Build Directory: build
13
Assets Directory: not found
14
Validating configuration for WordPress plugin deployment...
15
Configuration looks good
16
Going to deloy tag: 1.0.17
17
Cleaning up git repository with `git stash --all`. If you need build artifacts for deployment, set `deploy.skip_cleanup: true`. See https://docs.travis-ci.com/user/deployment/#Uploading-Files.
18
/usr/lib/git-core/git-stash: 186: /usr/lib/git-core/git-stash: cannot create /home/travis/build/TangRufus/tutsplus-dpl-demo/.git/logs/refs/stash: Directory nonexistent
19
20
Deploying application
21
Checking out https://plugins.svn.wordpress.org/remove-medium-cross-links
22
Clearing /tmp/d20170513-3291-1yh7vqo/trunk...
23
Removing deleted files from subversion...
24
D         /tmp/d20170513-3291-1yh7vqo/trunk
25
Temporary removing trunk and assets(if assets_dir is set)
26
Deleting       tmp/d20170513-3291-1yh7vqo/trunk
27
28
Committed revision 1656616.
29
Copying build to /tmp/d20170513-3291-1yh7vqo/trunk...
30
Copying /tmp/d20170513-3291-1yh7vqo/trunk to /tmp/d20170513-3291-1yh7vqo/tags/1.0.17...
31
Adding new files to subversion...
32
A         /tmp/d20170513-3291-1yh7vqo/trunk
33
A         /tmp/d20170513-3291-1yh7vqo/trunk/LICENSE
34
A         /tmp/d20170513-3291-1yh7vqo/trunk/README.txt
35
A         /tmp/d20170513-3291-1yh7vqo/trunk/remove-medium-cross-links.php
36
A         /tmp/d20170513-3291-1yh7vqo/tags/1.0.17
37
A         /tmp/d20170513-3291-1yh7vqo/tags/1.0.17/LICENSE
38
A         /tmp/d20170513-3291-1yh7vqo/tags/1.0.17/README.txt
39
A         /tmp/d20170513-3291-1yh7vqo/tags/1.0.17/remove-medium-cross-links.php
40
Committing 1.0.17
41
Adding         tmp/d20170513-3291-1yh7vqo/tags/1.0.17
42
Adding         tmp/d20170513-3291-1yh7vqo/tags/1.0.17/LICENSE
43
Adding         tmp/d20170513-3291-1yh7vqo/tags/1.0.17/README.txt
44
Adding         tmp/d20170513-3291-1yh7vqo/tags/1.0.17/remove-medium-cross-links.php
45
Adding         tmp/d20170513-3291-1yh7vqo/trunk
46
Adding         tmp/d20170513-3291-1yh7vqo/trunk/LICENSE
47
Adding         tmp/d20170513-3291-1yh7vqo/trunk/README.txt
48
Adding         tmp/d20170513-3291-1yh7vqo/trunk/remove-medium-cross-links.php
49
Transmitting file data ......
50
Committed revision 1656617.

And on the WordPress.org trac (https://plugins.trac.wordpress.org/browser/<your-plugin-slug>):

WordPress plugin subversion tracWordPress plugin subversion tracWordPress plugin subversion trac

Caveats

svn commit Twice

Since TravisCI ships with an old version of subversion which doesn't play well with subdirectories, I do svn commit twice.

The first svn commit removes everything inside trunk. The second svn commit copies everything from build_dir to trunk.

Experimental

This provider is still experimental and is not merged to TravisCI's official repo yet. You can keep track of TravisCI's feedback on its pull request.

Using edge doesn't merge my branch into the upstream master. There is a chance that my branch is behind the official repo. When it happens, you can fork my branch and rebase it, and then change source in .travis.yml to your GitHub repository.

Wrapping Up

To use this deployment provider:

  1. Host the plugin repository on GitHub.
  2. Connect GitHub and TravisCI.
  3. Add the configuration to .travis.yml.
  4. Push a tag.

I hope all the above helps you deploy plugins to WordPress.org faster. 

WordPress has an incredibly active economy. There are themes, plugins, libraries, and many other products that help you build out your site and project. The open-source nature of the platform also makes it a great option for you to improve your programming skills. Whatever the case, you can see everything we have available in the Envato Marketplace.

Thanks for reading!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.