Skip to content

Sync and deploy your wordpress website between environments πŸ”€

License

Notifications You must be signed in to change notification settings

hirasso/wp-sync-deploy

Repository files navigation

WordPress Sync & Deploy

A bash script that helps you

  • sync your WordPress database from production or staging to your local dev environment
  • deploy your local core, plugins, mu-plugins and theme to production or staging
  • run common tasks through wp-cli on the remote server to ensure your deploy works as expected
  • tested on OSX and Linux systems

Note

For less technical users I recommend to use a plugin instead. For database migrations, I'd recommend WP Migrate. For deployment, I'd recommend good old (S)FTP.

Prerequesites

  • WP-CLI installed on your local machine. On the remote server, wp-sync-deploy takes care of installing WP-CLI automatically.
  • A WordPress directory structure similar to this (adjustable through a .env.wp-sync-deploy file):
.
β”œβ”€β”€ content # your WordPress content folder (equivalent to the standard wp-content)
β”‚  β”œβ”€β”€ plugins
β”‚  β”œβ”€β”€ themes
β”‚  β”œβ”€β”€ ...
β”œβ”€β”€ core # your WordPress core folder (wp-admin, wp-includes, ...)
β”œβ”€β”€ index.php # main WordPress entry file
└── wp-config.php # your wp-config file

Tip

While it's easy to setup the custom directory structure yourself, I'd recommend to use a framework like Bedrock, WPStarter or wordplate. All of these provide amazing convencience features for modern WordPress development.

Installation

# CD into your project's root folder
cd /path/to/your/root

# Clone this repo
git clone [email protected]:hirasso/wp-sync-deploy.git

# Make sure the scripts are exectutable
chmod +x ./wp-sync-deploy/*.sh

Alternatively, you can install this script as submodule:

git submodule add [email protected]:hirasso/wp-sync-deploy.git

If you want to clone your main repo and already have wp-sync-deploy as a submodule, use this command:

git clone --recurse-submodules [email protected]:yourname/your-repo.git

Setup

Run this script:

./wp-sync-deploy/setup.sh

This will move the required configuration files to your current working directory and remove the .example part. You should now have these two files in your working directory:

.env.wp-sync-deploy

This file holds all information about your various environments (local, staging, production). Make sure you add .env.wp-sync-deploy to your .gitignore file! Otherwise, it's possible that sensitive information makes it into your repo.

VSCode can syntax highlight the env file for you.

wp-sync-deploy.tasks.php

This file is being used to run automated tasks after deployment. You can adjust this file as you wish or delete it if you don't want it to be executed.

Remote server preparation

wp-sync-deploy performs a few security checks before proceeding with a deploy:

  • Do all directories marked for deployment actually exist in both environments (locally and remotely)?
  • Does a hidden file .allow-deployment exist on the remote environment's web root?
  • Does the local command-line PHP version match the one on the remote environment?
  • Does the local web-facing PHP version match the one on the remote environment?

So when you are starting, you will need to

  • Perform the first deployment manually
  • Add an empty file .allow-deployment to your remote web root
  • Make sure that your local and remote server are set to use the same PHP version

Usage

Synchronise the database between environments

# sync the database from your production server
./wp-sync-deploy/sync.sh production

# sync the database from your staging server
./wp-sync-deploy/sync.sh staging

# push your local database to your staging server
./wp-sync-deploy/sync.sh staging push

# Backup the remote database and store it locally
./wp-sync-deploy/sync.sh <production|staging> backup

Note

Syncing your local database is only possible to the staging server by default. If you are sure you know what you are doing, you can also enable syncing to the production server.

Deploy your local files to remote environments

# deploy your files to your production server (dry)
./wp-sync-deploy/deploy.sh production

# deploy your files to your staging server (dry)
./wp-sync-deploy/deploy.sh staging

# deploy your files to your production server (non-dry)
./wp-sync-deploy/deploy.sh production run

# deploy your files to your staging server (non-dry)
./wp-sync-deploy/deploy.sh staging run

Run automated tasks after each deploy / sync ✨

wp-sync-deploy will automatically run tasks on the target server when you sync or deploy. Modify the wp-sync-deploy.tasks.php file created by the setup script, to customize which tasks should be executed.

Default tasks defined in the file are:

  • Optionally delete all transients
  • Optionally delete your static cache
  • When deploying: Optionally update the rewrite rules

Other notes

wp-sync-deploy has a default list of files and directories that will be ignored during a deploy. If you wish to customize this list, you can do so by modifying the file .deployignore.

About

Sync and deploy your wordpress website between environments πŸ”€

Resources

License

Stars

Watchers

Forks

Packages

No packages published