Automatic Git Deploy with Kinsta via SSH


Kinsta is my preferred WordPress host provider. This year I’ve switched all my new customers to Kinsta. Coming from WP Engine, one feature I miss is a per site git URL which will automatically deploy updates whenever a git push is run locally. This isn’t something Kinsta currently offers.

There are plenty of workarounds. In fact I’ve previously mentioned a solution using a 3rd party service called Beanstalk. That said I’ve decided to see how hard it would be to handle the Git deployments using just Kinsta.

With some upfront setup, Kinsta can totally handle an automatic deploy from a local git push.

  • On local computer cd to a WordPress site and run git init
  • Add a .gitignore file with common WordPress exclusions (plugins/themes/core files).
  • On server add a bare git repo under private folder. This will act as the deployment repo similar to WP Engine’s built in deployment repo.
    git init --bare ~/private/{site-name}.git
  • Create a post-receive hook stored within {site-name}.git/hooks/ and named post-receive (see below example). This will deploy from the bare git to the ~/public/ directory whenever a git push is received.
  • Assign execute permissions. 
    chmod +x {site-name}.git/hooks/post-receive
  • On local computer add the remote.
    git remote add production ssh://{site-name}@{site-ip}:{site-port}/www/{site-folder}/private/
    {site-name}.git
  • On local computer make file changes, git commit and deploy as normal with a git push. This will deploy via SSH to the live site. On the first push git you may need to specific which branch to use on the remote like so git push --set-upstream production master.

Example post-receive file: ~/private/sitename.git/hooks/post-receive

#!/bin/bash
TRAGET="/www/sitename_123/public"
GIT_DIR="/www/sitename_123/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=$TRAGET --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

References