How to Set Up CI/CD for WordPress Deployment

How to Set Up CI/CD for WordPress Deployment

1. The Strategy: Git-Based Infrastructure

To use CI/CD, your WordPress site must be treated as a code repository.

  • The Repository: You don’t usually track the WordPress core. Instead, you track your wp-content folder (themes and plugins) and your wp-config.php (using environment variables for secrets).
  • The Branches: Use a staging branch for testing and a main branch for production.

2. Environment Variables & Secrets

Never hardcode your server passwords or database credentials in your repository.

  • GitHub Secrets: Store your SSH Private Key, Server IP, and Database credentials in Settings > Secrets and Variables > Actions.
  • Access: Your pipeline will pull these securely during the build process to authenticate with your VPS.

3. Creating the GitHub Action Workflow

You define your pipeline in a YAML file located at .github/workflows/deploy.yml.

The Workflow Stages:

  1. Checkout: Pulls the latest code from your branch.
  2. Install Dependencies: Runs composer install for PHP packages and npm install && npm run build to compile your SASS/JavaScript assets.
  3. Security Scan: (Optional) Runs a tool like PHPCS (PHP Code Sniffer) to ensure code quality.
  4. Deploy: Uses rsync or SSH to push the compiled files to your server.

4. Zero-Downtime Deployment with RSYNC

Using rsync is superior to a standard copy command because it only transfers the files that have changed, making deployments incredibly fast.

  • Atomic Deploys: For advanced setups, you can deploy to a new “timestamped” folder and update a symbolic link (symlink) to point to the new version instantly. This ensures that if a deployment fails, the site stays on the old version.
  • Example Script Logic:Bashrsync -avz --delete -e "ssh -p [PORT]" ./dist/ user@host:/var/www/html/wp-content/themes/my-theme

5. Post-Deployment Hooks (WP-CLI)

After the files are moved, the server may need to perform application-level tasks.

  • Flushing Cache: Run wp cache flush via SSH to clear the object cache.
  • Database Migrations: If you are using a tool like WP Migrate, you can trigger a database pull/push through the pipeline.

Leave a Reply

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