Skip to main content
Building Robust WordPress Deployments with WP-CLI and GitHub Actions

Building Robust WordPress Deployments with WP-CLI and GitHub Actions

I've spent years wrestling with manual WordPress deployments, and frankly, it's a productivity killer.

I’ve spent years wrestling with manual WordPress deployments, and frankly, it’s a productivity killer. The fear of breaking a live site, the meticulous steps, the endless FTP transfers – it’s a recipe for burnout and errors. But what if we could automate the entire process, pushing code changes with confidence, knowing our sites are updated quickly and consistently? That’s where WP-CLI and GitHub Actions become an indispensable duo for any serious WordPress engineer.

At its core, WP-CLI gives us a powerful command-line interface to interact with WordPress installations. It allows us to manage plugins, themes, users, databases, and even the core itself, all without ever touching the admin dashboard. This capability is paramount for automation. Imagine needing to update a custom plugin or theme, flush caches, and run database migrations after a code push. Manually, that’s several steps, each prone to human error. With WP-CLI, these become single, scriptable commands. For instance, wp plugin update my-custom-plugin combined with wp cache flush handles a significant chunk of a typical deployment, ensuring your site is running the latest code and cleared of any stale data.

Now, how do we trigger these WP-CLI commands on our remote server automatically? Enter GitHub Actions. This powerful CI/CD platform lets us define workflows that execute when specific events occur in our GitHub repository – like pushing code to the main branch. Our goal is to craft a workflow that SSHes into our server, pulls the latest code, and then executes our sequence of WP-CLI commands. This creates a fully automated deployment pipeline, transforming a multi-step manual process into a single Git push.

Let’s walk through a concrete example. Consider a scenario where we’re deploying updates to a custom theme located in wp-content/themes/my-theme. Our GitHub Actions workflow, typically defined in a .github/workflows/deploy.yml file, would be triggered by a push to the main branch. Inside this YAML file, we would define a deploy job that runs on an ubuntu-latest runner. The steps would involve first checking out the code from our repository using actions/checkout@v3. The crucial next step uses an action like appleboy/[email protected] to establish an SSH connection to our production server.

The core of this workflow is the script parameter within the appleboy/ssh-action. Here, we define a sequence of bash commands to be executed on the remote server. First, cd /var/www/html/your-wordpress-site navigates to your WordPress installation directory, which you’d adjust to your server’s specific path. Then, git pull origin main fetches the latest code from your repository. If your project uses Composer for PHP dependency management, a subsequent composer install --no-dev --optimize-autoloader ensures your dependencies are current and optimized for production. Finally, we execute crucial WP-CLI commands: wp cache flush to clear any stale object, transient, or opcode caches, wp option update siteurl "https://your-production-domain.com" and wp option update home "https://your-production-domain.com" to set environment-specific URLs, and critically, wp db update to run any pending database migrations defined in your plugins or theme. Each command builds on the last, ensuring a systematic and complete update. Remember that your SSH_HOST, SSH_USERNAME, and SSH_PRIVATE_KEY for the SSH action would be securely configured as GitHub Secrets.

Automating WordPress deployments with WP-CLI and GitHub Actions transforms a tedious, error-prone task into a streamlined, reliable process. It frees up engineering time, significantly reduces the risk of human error, and ensures your production sites are always up-to-date with your latest code. By investing in a well-defined CI/CD pipeline, you’re not just deploying code; you’re deploying confidence and consistency across all your WordPress projects.