Deploying to WP Engine from Travis

Comment

Deploying to WP Engine from Travis

We recently embarked on a Wordpress based project and as part of our CI process needed to deploy to WP Engine from Travis.

Sadly, at the time of writing Travis doesn't support WP Engine out of the box. Handily, WP Engine supports git deploys, so with a little bit of tinkering we were able to write a custom deploy script.

The Travis docs hint at a way to do a git push deploy, but we wanted our develop branch to deploy to our staging environment, and our master branch to deploy to production, which meant writing a small deploy script.

We created a file in $project_root/.travis/deploy that ended up looking like this:

#!/bin/bash

branch="$1"
wp_app_name="FILL ME IN"
if [ "$branch" == "master" ] || [ "$branch" == "develop" ]
then
deploy_env=$([ "$branch" == "master" ] && echo "production" || echo "staging")

echo "Adding SSH keys"

chmod 600 .travis/id_rsa
ssh-add .travis/id_rsa

echo "Deploying to ${deploy_env}..."

git remote add ${deploy_env} git@git.wpengine.com:${deploy_env}/${wp_app_name}.git
git push ${deploy_env} ${branch}

echo "Deployed!"
exit 0
fi

We modified our travis.yml to look like this:

language: php
script: 'true'
before_script:
- echo -e "Host git.wpengine.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
after_success:
- test $TRAVIS_PULL_REQUEST == "false" && bash .travis/deploy $TRAVIS_BRANCH

The before_script section ensures the script doesn't hang on SSH host verification. The after_success section first makes sure we're not in a pull request branch, then executes our deploy script.

It's as easy as that! Don't forget to generate a new SSH keypair and upload the public key to WP Engine, otherwise the deploy won't work.

Comment