4. Automated Deployment with WP-CLI and
Aliases
SENIOR SALES ENGINEER
WP ENGINE
@SPICECADET
Edmund Turbin
5. 5
Understand the problem
Define success
Think creatively
Sometimes solutions are not apparent
Can involve 3rd Parties or combinations of technologies
How can I overcome a challenge?
Sales Engineering = Finding Solutions.
7. 7
Developing WordPress locally
Create Aliases to access WordPress remotely
Connecting to the remotes via SSH
Using WP-CLI commands to execute MySQL queries
Deploy DB Changes from Local Development to Production/Staging/Dev server
What’s Involved?
9. 9
Local Dev Stack - MAMP/WAMP
Virtual Machines
Docker - Containers
Local Server (running on your laptop)
Local dev solution from web host
Many ways to work locally
Developers generally choose what works for them
Local Development Approach
10. Local Development Virtual Machine Configuration
Local Development Tools.
10
https://github.com/Varying-Vagrant-
Vagrants/VVV
https://www.vagrantup.com/https://www.virtualbox.org
12. VirtualBox
Virtualization.
12
Multiple virtual machines can run at the same time
Guest operating systems can be different - Mac,
Windows, Linux, etc.
VMs can be pre-built with commonly used apps -
Apache, Nginx, email server
Save state - start/stop, freeze, back up, copy
13. Host
Local machine where virtual instances run
Hypervisor
Thin layer on host that allocates CPU, memory and resources to a virtual
instance
Virtual Machine.
13
HOST OS
HYPERVISOR
GUEST VM 1
APPS
OS
GUEST VM 2
APPS
OS
14. Vagrant
Vagrant.
14
Automation tool for building and managing VMs
Uses VirtualBox as a base
Uses a provisioning tool for automation
Automatically installs and configures software on the
VM
15. WP-CLI.
15
Access commonly functionality in WordPress from the
command line.
Do something quickly in WordPress without menu-
diving
Automate repetitive tasks
Bundle several tasks together to create repeatable
scripts
WordPress Command Line Interface
https://wp-cli.org/
16. Bourne-Again Shell
BASH - Shell Scripting.
16
Unix shell and scripting language
Released in 1989 as free software
Common on macOS, Linux, Windows 10, etc.
Can execute commands from text and from file - shell
script
Allows control structures and variables
https://en.wikipedia.org/wiki/Bash_(Unix_shell)
17. 17
Built on Ubuntu 14.04 LTS (Trusty) base VM
Provisions the following software packages
VVV - What’s under the hood
Tools
DatabaseServer
19. 19
VVV is seen as a separate instance
Even though the guest runs on the host machine, it
acts like a remote
SSH is required to access the command line of the
guest machine
Secure Shell
Why do we need SSH?
> SSH
20. 20
Public Key Authentication
SSH Intro
> SSHRemote
Public Key
Client
Private Key
https://www.ssh.com/ssh/public-key-authentication
21. 21
Remote connection between two systems
Secured by public/private key authentication
client has an encrypted private key
server has a public key
connection checks to make sure keys are valid
Replaces password authentication for quicker access
Secure Shell
SSH Intro
> SSH
22. 22
secure connection over port 22
ssh user_name@hostname
add key to list of hosts
-i option will allow public/private key
authentication to be used
SSH Command.
> SSH
Secure Shell
24. 24
vagrant ssh-config
Host your_site
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /path_to_private_key
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes
Generate Vagrant SSH.
25. 25
vagrant ssh-config
Host your_site
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /path_to_private_key
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes
Port Number Can Change - Regenerate.
28. 28
Shortcuts that you register in a .yml file
Allow you to run commands against a remote
WordPress instance
Can be grouped together
Bypass remote login, change directories to get to a
WordPress install
WP-CLI Features which requires some quick config
WP-CLI Aliases
ALIASES
29. 29
Lesser known feature of WP-CLI
Aliases can be globally set or on a site by site basis
What’s the benefit?
time saved
WP-CLI Features which requires some quick config
WP-CLI Aliases
ALIASES
36. 36
# check if a plugin is installed
wp @both plugin is-installed hello
# return value for last command - 0 = true
echo $?
# install plugin if $? false
if [ $? -eq 0 ]; then wp @prod plugin install hello ——
activate; fi
Simple Logical Plugin Example.
37. 37
#! /bin/bash
# check if a plugin is installed
wp @dev plugin is-installed hello
# install plugin if $? false
if [ $? -eq 0 ]; then wp @dev plugin install hello ——activate;
fi
We Can Save Commands in a Script.
38. 38
# create a variable and save a string
NAME=edmund
# Print the value of the variable
echo $NAME
edmund
We Can Save Values in a Variable.
39. 39
# create a variable and save a string
BLOG_NAME=${wp option get blogname}
# Print the value of the variable
echo $BLOG_NAME
Edmund Turbin’s Blog
We Can Save Commands in Variables.
40. 40
# Save command output as a variable
PROD_SITEURL=$(wp @prod option get siteurl);
DEV_SITEURL=$(wp @dev option get siteurl);
Get URLs from dev and production.
41. 41
# Get db from remote WP site and download it as a local
file
wp @prod db export - > prod.sql
- option prints to screen
> option redirects output to a local file
Export the WordPress DB.
42. 42
# Import the downloaded .sql file
wp @dev db import /srv/www/edmund/prod.sql
Full path used to point to the .sql file
Import the WordPress DB on Dev.
43. 43
# Search and replace siteurl option
wp @dev search-replace $prod_siteurl $dev_siteurl
Change production siteurl to dev.
47. 47
#!/bin/bash
#get siteurl option from prod and dev
PROD_SITEURL=$(wp @prod option get siteurl);
DEV_SITEURL=$(wp @dev option get siteurl);
#Export database from prod and import to dev
wp @prod db export - > prod.sql
wp @dev db import /srv/www/edmund/prod.sql
#Update siteurl
wp @dev search-replace $PROD_SITEURL $DEV_SITEURL
#Remove .sql file
rm prod.sql
Complete Script.
https://gist.github.com/spicecadet/71d65fab0480812bc66b09b69d056253