Deploying a WordPress site via Git Hooks on a ServerPilot Server

This guide looks at setting up a git deployment using hooks to push local commits from a local development to a remote development branch which then published those changes live, stored on a staging domain using a ServerPilot set up.

First up is to set up a ServerPilot app, add your system user and create a MySQL database via the ServerPilot control panel. I am using a Vultr droplet but any other like Linode or Digital Ocean is the same. This is a development staging site that I can share with the client before migrating to live.

The article works backwards – first you set up the git hook and tell git where the git repo is stored and what destination to push the changes to.

Copy Your SSH Keys to ServerPilot

First up copy your SSH keys from your local to your ServerPilot account, so from your local development…

ssh-copy-id [email protected]

Above I am using the a fictitious account sysuser account, use which one your domain belongs to, if you only have the free serverpilot account then that’s your only option. In reality I have a few accounts on the droplet and I use individual accounts per user for better security.

Set Up a Git Repo on the Staging Site

In a Terminal SSH session, navigate to the home and create a directory wpstaging.git

cd

cd to get home – check with…

pwd

And you should see…

/srv/users/sysuser

Make a directory

mkdir wpstaging.git

Initialise a bare git repo

git init --bare

Create a post-receive hook

nano hooks/post-receive

Add in the hook where git needs to deploy the data from and to destinations

#!/bin/sh
 git --work-tree=/srv/users/sysuser/apps/domain-app-name/public --git-dir=/srv/users/sysuser/wpstaging.git checkout -f

So above change the app name and system user name to your settings and save the file.

Make sure the hook is executable…

chmod +x ~/wpstaging.git/hooks/post-receive

That’s it on the remote side of things.

Create the remote branch on the local site

So now on your local development and its taken that you have a local repo already established, so now add in the remote branch.

git remote add staging ssh://sysuser@dev.somedomain.com/srv/users/sysuser/wpstaging.git
 git push staging master
 git push --set-upstream staging master

Now you commits will be pushed to the wpstaging.git directory on the remote server and the post-receive hook will then push the changes into the public webroot folder.