WordPress Git deployments for Kinsta via SSH

I’ve interacted with a number of different WordPress hosts over the last few years, but Kinsta has been the best by far in terms of support, performance, and security. Since I’ve been recommending Kinsta for every new client, I’ve decided I needed an easy & fast process to develop new features on my local server and deploy them to Kinsta.

I’ve done some research and found few different options, including plugins and services. However, I decided to go with Git, because, you know! I want to be a good developer.

So without further introduction, here is the process:

On Server

  • Connect via SSH
  • Create a bare git repo under the private folder. This will be the deployment repo.
git init --bare /private/{sitename}.git
  • Access the public folder and initialize a new git repo.
cd public
git init
  • Access the private repo hooks folder and create a post-receive hook.
cd /www/{sitefolder_xxxxxxx}/private/{sitename}.git/hooks
nano post-receive
  • Add the following code inside the post-receive file, make the necessary changes, save and quit nano ( CTRL+X -> Y -> Enter ).
#!/bin/bash
TARGET="/www/{sitefolder_xxxxxxx}/public"
GIT_DIR="/www/{sitefolder_xxxxxxx}/private/{sitename}.git"
BRANCH="master"

while read oldrev newrev ref
do
        # only checking out the master (or whatever branch you would like to deploy)
        if [[ $ref = refs/heads/$BRANCH ]];
        then
                echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
        else
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
        fi
done
  • Assign execute permissions to post-receive file
chmod +x post-receive

On your local computer

  • Access the WordPress installation folder you’ll be working on and initialize a new repo.
cd /wordpress/path
git init
  • Add .gitignore file and make sure to ignore everything except the files/folders you’ll be working on. You can use this template:
# ignore everything in the root except the "wp-content" directory.
/*
!wp-content/

# ignore everything in the "wp-content" directory, except:
# "plugins", "themes" directory
wp-content/*
!wp-content/plugins/
!wp-content/themes/

# Ignore everything in the "plugins" directory, except the plugins you
# specify (see the commented-out example.)
wp-content/plugins/*
# !wp-content/plugins/example-plugin/

# Ignore everything in the "themes" directory, except the themes you
# specify (see the commented-out example.)
wp-content/themes/*
#!wp-content/themes/example-theme/
  • Add a remote
git remote add production ssh://{username}@{ip}:{port}/www/{sitefolder_xxxxxxx}/private/{sitename}.git
  • Make changes, add, commit and push. However, on the first time make sure to use `git push -u production master` instead of `git push` to specify which branch to use.
git add .
git commit -m "message"
git push

That’s it, your life as a WordPress developer just got a bit easier, you’re free from copying and pasting files over FTP.

Leave a comment