DEV Community

Cover image for Kinsta Git Push Deployment
Kevinleary.net
Kevinleary.net

Posted on Originally published at kevinleary.net

Kinsta Git Push Deployment

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 Remote Repository on Kinsta

First create a git repository on your Kinsta server for receiving pushed updates.

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}
Enter fullscreen mode Exit fullscreen mode

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

Next, create a bare repository in the ~/private directory, which will be the remote repo we pushed to when we want to deploy. Adding a symbolic-ref will mirror the HEAD of this private repository in the ~/public git repo that’s built-in to Kinsta.

git init --bare ~/private/$LOGNAME.git;
cd ~/private/$LOGNAME.git;
git symbolic-ref HEAD refs/heads/main;
git config --global receive.denyCurrentBranch 'ignore';
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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="main";

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;
    cd $TARGET && wp kinsta cache purge --all;
  else
    echo "Only the ${BRANCH} branch may be deployed on this server. Received: $ref";
  fi
done
Enter fullscreen mode Exit fullscreen mode

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

chmod +x ~/private/$LOGNAME.git/hooks/post-receive;
Enter fullscreen mode Exit fullscreen mode

Now when you push to the production remote it will deploy all committed updates, then clear all Kinsta cache.

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
Enter fullscreen mode Exit fullscreen mode

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 main.

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.

https://www.youtube.com/embed/s4BTvj1ZVLM?feature=oembed

Top comments (0)