Automatic Git Deploy with Kinsta via SSH (original) (raw)

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.

git init --bare ~/private/{site-name}.git
cd ~/private/*.git
git symbolic-ref HEAD refs/heads/main

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

#!/bin/bash
TARGET="/www/sitename_123/public"
GIT_DIR="/www/sitename_123/private/sitename.git"

while read oldrev newrev ref
do
        # Neat trick to get the branch name of the reference just pushed:
        BRANCH=$(git rev-parse --symbolic --abbrev-ref $ref)

        if [[ $BRANCH == "main" ]];
        then
                echo "Ref <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>f</mi><mi>r</mi><mi>e</mi><mi>c</mi><mi>e</mi><mi>i</mi><mi>v</mi><mi>e</mi><mi>d</mi><mi mathvariant="normal">.</mi><mi>D</mi><mi>e</mi><mi>p</mi><mi>l</mi><mi>o</mi><mi>y</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">ref received. Deploying </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">re</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">rece</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord">.</span><span class="mord mathnormal">De</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal" style="margin-right:0.03588em;">oy</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>{BRANCH} branch to production..."
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
        else
                echo "Ref $ref received. Doing nothing: only the main branch may be deployed on this server."
        fi
done

I recommend creating this script directly on the server using SSH rather then attempting to upload via SFTP. That will prevent any issues with line endings that can happen between platforms. To create files directly, start by SSH into the Kinsta site then run nano ~/private/sitename.git/hooks/post-receive and paste contents of above script then tweak as needed. Press Ctrl + X then Y then Enter to save and quit Nano.

References

Previous Post A Week with Windows 10 and Linux Subsystem for WordPress Development Next Post All of the Small Things Kinsta Gets Right