Skip to content

Instantly share code, notes, and snippets.

@otakupahp
Last active January 29, 2022 01:49
Show Gist options
  • Save otakupahp/2a48a3ce3b475921207b740c804d1313 to your computer and use it in GitHub Desktop.
Save otakupahp/2a48a3ce3b475921207b740c804d1313 to your computer and use it in GitHub Desktop.
Automatic deploy using GIT
#!/bin/bash
TARGET="/www/{site-folder}/public/wp-content/themes"
GIT_DIR="/www/{site-folder}/private/warp.git"
BRANCH="main"
while read oldrev newrev ref
do
# only checking out the master
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 $BRANCH
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
@otakupahp
Copy link
Author

otakupahp commented Jan 4, 2022

NOTE: The article used offers configurations for Kinsta, but could be used anywhere

This is an updated file from https://anchor.host/automatic-git-deploy-with-kinsta-via-ssh/

The steps are:

  • 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. git init --bare ~/private/{site-name}.git
  • On server, add a new git repo under the public folder. cd ~/public; git init
  • Create a post-receive hook stored within ~/private/{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 ~/private/{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 specify which branch to use on the remote like so git push -u production master.

The code and paths must be updates as needed.

The original article had issues if you use a branch different of master, so I used this https://stackoverflow.com/questions/31147389/git-remote-fatal-you-are-on-a-branch-yet-to-be-born to fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment