Discover innovative tools, design features, unique websites, and code structure curated by the GoWeb community.

View the resources by category or by monthly GeekOut! Compilations. Check out the latest compilation below.

Continuous Deployment with Codeship and WP Engine

Visit Continuous Deployment with Codeship and WP Engine


The purpose of this article is to share our approach to continuous deployment for WordPress plugins and themes on our servers at WP Engine. Continuous deployment is the automated testing and delivery of code to development and production environments. Knowing your web host’s requirements is essential to setting up a continuous delivery process. Once you know this, then you can evaluate a service and later develop a solution that meets your needs. I have implemented this for more than a dozen WordPress themes and plugins, and I believe that effort was worth the time it is saving us now.

Video tutorial

https://www.youtube.com/embed/iNOaUyDPHDQ?rel=0

Our approach

Codeship automates the process of testing, building, and deploying code to your server, and was the first such product I found which would integrate with our web host (WP Engine). It watches a repository, clones an updated branch to a virtual machine, runs your commands on it, and then deploys the result to your servers. Services like Codeship allow us to automate our existing development and deployment workflow so that changes to the code in our Git repositories are automatically made on our servers.

We use three custom bash scripts in our Codeship projects: setup, build, and deploy. The setup script runs first and is mostly identical for all branches:

# Get around shallow update restriction
if [ -f ${HOME}/clone/.git/shallow ]; then git fetch --unshallow; fi
# Add User Data
git config --global user.name "codeship-shortreponame"
git config --global user.email "youremail@email.com"
git config --global github.token RELEASE_KEY
# Combine remote git servers
git remote add servers $SERVERNAME_ENVIRONMENT
git remote set-url --add --push servers $SERVERNAME_ENVIRONMENT
git remote set-url --add --push servers $SERVERNAME2_ENVIRONMENT 
# Install needed modules
rvm use 2.2.6 --install
npm install -g sass
# Move repo files to a named folder
mkdir $FOLDERNAME
shopt -s extglob
mv !($FOLDERNAME) $FOLDERNAME
# Move repo files whose name begins with a period
mv .sass-lint.yml $FOLDERNAME/.sass-lint.yml
# Exclude development-only files from commit
rm .gitignore
mv .codeshipignore $FOLDERNAME/.gitignore
# Move named folder into a structure identical to the root directory of a WordPress server
mkdir -p $DIRECTORY
mv $FOLDERNAME $DIRECTORY
cd $DIRECTORY/$FOLDERNAME/

It adds git user data, defines a list of remote server repositories, and installs command line modules on the virtual machine. The staging servers are listed as remote git repositories in the staging branch’s setup script, and our production servers are listed as remotes in the master branch’s script. Then it replaces the repository’s gitignore file with our .codeshipignore file and moves all files into a folder structure relative to the root directory of our servers.

Our typical build script imports Node and composer modules and uses Grunt to compile files:

# Build
composer install
npm install
grunt develop
# Check PHP against WordPress VIP standards
npm run checkwp

It uses the repo’s gruntfile (example) to perform a different set of tasks for the staging and production servers.

Note that we use Sass expanded output with sourcemaps for developing Sass files and a compressed output for production. A web browser’s inspection tool can detect sourcemaps and use them to show you where a CSS rule is located in your Sass files. This is essential for troubleshooting or modifying Sass as quickly as possible.

Once the build script has finished, the deploy script commits changes to the cloned repository and pushes them to our WP Engine servers:

# Release
grunt release
# Deploy
git add --all :/
git commit -m "DEPLOYMENT" 
git push servers HEAD:refs/heads/master --force

We can only send code to WP Engine servers from Codeship using their git push feature. It replaces user input authorization with an SSH key. The .codeshipignore file allows us to exclude unneeded files from the commit and reduce upload times. Since our projects are authorized git push users with the WP Engine servers we push to, the last command pushes our final commit to the remote server repositories we defined during setup.

The video tutorial demonstrates how to implement this process. What follows are the same steps I take to connect each of our repositories to our WP Engine servers using Codeship.

Using Codeship to deploy Github repos to WP Engine

  1. Create a new Codeship project and connect it to a Git repo URL
  2. On the Codeship General Project Settings page, copy the SSH public key
  3. On each WP Engine install you want connected, use the SSH public key to add a new Git Push developer named codeship-shortreponame.
  4. On the Codeship project’s Environment settings page, create a new variable named SERVERNAME_PRODUCTION and SERVERNAME_STAGING for each WP Engine install you want connected.
  5. On the same page, define a FOLDERNAME variable as the name of the folder your repo’s files should be placed within. Also define a DIRECTORY variable as either wp-content/plugins or wp-content/themes.
  6. On the Codeship project’s Deployment settings page, create one pipeline for the repo’s master branch and another for the staging branch.
  7. Create separate custom scripts in each pipeline for the setup, build (optional), and deploy portions of the process.Create separate custom scripts in each pipeline for the setup, build (optional), and deploy portions of the process.
    • The setup script only has a few variables that need to be changed for each project: branchname, shortreponame, youremail@email.com, and SERVERNAME_ENVIRONMENT.
    • The build script contains your existing build process, such as importing NPM modules or preprocessing css and js files.
    • The deploy script has no variables.
  8. Ensure your Git repo has a master and staging branch.
  9. Checkout the staging branch of your repo and add a .codeshipignore file to its root directory using this template as a guide.
  10. Commit the change and push it to your repository. This will trigger the Codeship build process, which you can watch on the project’s main page as it moves through each line of your custom scripts.
  11. Once the build has finished, it will send the code to the staging server if successful or stop on the first line that results in an error. If it is successful, it will be pushed to the server! Whether or not it is successful, you can click on any line of the script to see its output, so if it fails you can use this to troubleshoot that command.
  12. When your build is successful, you can merge the staging branch to the master branch on your computer and then push it to the repository. This will trigger the master pipeline, which will either deploy it to your production server or result in an error.

And that’s it! Now you have an automated deployment process integrated with your existing Git repository, and all of your servers can have the latest, tested version of your plugins and themes each time you push to it. If you found this article helpful, please leave a comment. You can view the public repositories of Agrilife Communications on Github for more information about our build processes. I can be reached by email at zachary.watkins@ag.tamu.edu.

Resources