Kevinleary.net, LLC.

Kinsta Git Push  Deployment

Deploy updates to Kinsta with git push, utilizing the same approach as WPEngine under the hood.

Kinsta provides good WordPress hosting that’s comparable to WPEngine in many ways, but it’s lacking one important feature that many developers can’t live without: git push deployment.

It allows for fast, zero downtime deployments that can be rolled back instantly when needed. It’s the best way to deploy updates to a professional WordPress site, and unfortunately it can’t be done out of the box with a Kinsta hosted WordPress site. Kinsta does have git available for pulling down a site from an external remote like GitHub, but there’s remote that can be pushed to for deployments.

It can be done though…

Initialize Two Git Repositories

We’ll need to create two git repositories to make this work:

  1. The ~/private directory repo will be the git repository that we push to
  2. The ~/public directory repo will be in the WordPress root directory

When we push to Kinsta the private repository will receive the updates files and then trigger a post-receive script that will sync the files to the WordPress directory safely.

SSH into your Kinsta server:

ssh {SITENAME}@{SSH-IP-ADDRESS} -p {SSH-PORT}

If you don’t know this information you can find it within the my.kinsta.com hosting panel.

# Initialize a repository in your site root
cd ~/public;
git init;

# Create a private repository to deploy to
git init --bare ~/private/$LOGNAME.git

# Add worktree to the private repo pointing to the public repo
git --work-tree=$HOME/public --git-dir=$HOME/private/$LOGNAME.git checkout -f $BRANCH --progress;
cd ~/private/$LOGNAME.git;
git worktree add -B master ~/public/master master;

Add a Git post-receive Hook

Once we have two git repositories setup we can add a post-receive Git hook to our private (deploy to) repo:

# Setup a post-receive Git hook
nano ~/private/$LOGNAME.git/hooks/post-receive

This will open the post-receive file in a blank nano editor. Copy and paste the following script into the editor, then press CTRL + X to close and save, and Y when prompted to confirm the save.

#!/bin/bash

TARGET="$HOME/public";
GIT_DIR="$HOME/private/$LOGNAME.git";
BRANCH="master";

while read oldrev newrev ref
do
  if [ "$ref" = "refs/heads/$BRANCH" ];
  then
    echo "Deploying ${BRANCH} branch to Kinsta...";
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH --progress;
  else
    echo "Only the ${BRANCH} branch may be deployed on this server. Received: $ref";
  fi
done

Once the script is in place it will need write permissions:

chmod +x ~/private/$LOGNAME.git/hooks/post-receive;

Local Machine Setup

The Kinsta server is ready to receive git pushes at this point, so the final step is to add the new remote git repository to your local machines git repo as a new remote:

git remote add production ssh://{site-name}@{ssh-ip-address}:{site-port}/www/{site-folder}/private/{site-name}.git

The following values can be found on your my.kinsta.com dashboard under SFTP/SSH > SSH terminal command:

  • site-name
  • ssh-ip-address
  • site-port
  • site-folder is found under Environment details

You’re now ready to deploy to your Kinsta hosted site using git push deployment: git push production master.

Import Notice

ALWAYS go through this process on a staging site first to avoid breaking a live site.

Git Worktree’s

The secret sauce in this approach is the use of git worktree’s. To learn more reference the git documentation or watch this video that covers the topic really well.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a freelance web developer and WordPress consultant in Boston, MA with 16 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.