Updating plugins using Git and WP-CLI

Now that you know how I deploy WordPress sites and how I configure WordPress environments, what about the maintenance of keeping a WordPress site’s plugins up-to-date?

Since I’m using Git, I cannot use WordPress built-in plugin updater on the live site (and I wouldn’t want to — if a plugin update goes wrong, my live site could be in trouble!)

The simple way to update all your plugins from a staging or local development site is to use WP-CLI:

wp plugin update-all
git commit -am 'update all plugins' wp-content/plugins

That works. I used to do that.

I don’t do that anymore.

Why? Granularity.

One of the benefits of using version control like Git is that when things go wrong, you can pinpoint when they went wrong, and identify what code caused the issue.

Git has a great tool called bisect that takes a known good state in the past and a current broken state, and then jumps around between revisions, efficiently, asking you to report whether that revision is good or bad. Then it tells you what revision broke your site.

If you lump all your plugin updates into one commit, you won’t get that granularity. You’ll likely get the git bisect result of “great… one of EIGHTEEN PLUGINS I updated was the issue”. That doesn’t help.

Here’s how you do it with granularity:

for plugin in $(wp plugin list --update=available --field=name);
    wp plugin update $plugin &&
    git add -A wp-content/plugins/$plugin &&
    git commit -m 'update $plugin plugin'

This code loops through plugins with updates available, updates each one, and commits it with a message that references the plugin being updated. Great! Now git bisect will be able to tell you which plugin update broke your site.

And what if you can only run WP-CLI commands from within a VM, and Git commands from your local machine? For instance, if you’re using my favorite tool, Local by Flywheel, you have to SSH into the site’s container to issue WP-CLI commands, but from within that container, you might not have Git configured like it is on your host machine.

So what you can do is break the process into two steps.

On the VM, run this:

wp plugin list --update=available --field=name > plugins.txt
wp plugin update-all

That grabs a list of plugins with updates and writes them to a file plugins.txt, and then updates all the plugins.

And then on your local machine, run this:

while read plugin;
    git add -A wp-content/plugins/$plugin &&
    git commit -m 'update $plugin plugin'
done; < plugins.txt

That slurps in that list of updated plugins and does a distinct git add and git commit for each.

When that’s done, remove plugins.txt.

All your plugins are quickly updated with WP-CLI, but you get nice granular Git commits and messages.

Do you need WordPress services?

Mark runs Covered Web Services which specializes in custom WordPress solutions with focuses on security, speed optimization, plugin development and customization, and complex migrations.

Please reach out to start a conversation!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s