Skip to content

Instantly share code, notes, and snippets.

@wottpal
Last active July 22, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wottpal/e134da40f7d023ede93f to your computer and use it in GitHub Desktop.
Save wottpal/e134da40f7d023ede93f to your computer and use it in GitHub Desktop.
Kirby Git Deploy Hook
#!/bin/bash
# IMPORTANT REQUIREMENTS
# * Make sure the git-user is allowed to perform operations on behalf of
# www-data (without password) by adding this line to `/etc/sudoers`:
# `git ALL=(www-data) NOPASSWD: /usr/bin/git, /bin/mkdir`
# * Make sure `var/www` is owned by www-data:www-data by executing
# `sudo chown -R www-data:www-data /var/www`
# * Add www-data to the git-group by executing `sudo usermod -a -G git www-data`
# * Make sure this file is executable by executing
# `chmod +x post-receive`
# * Make sure to have a `.gitconfig` file in place in `WWW_USER`s
# home directory (`/var/www` for `www-data` by default) where
# at least `user.name` and `user.email` are specified.
# Location of the Git Bare-Repository
unset GIT_DIR # Reset GITs environment variables (Source: https://goo.gl/bzXaBg)
GIT_DIR=/home/git/website-chiva.git
# Directories to deploy to & their respective branches
declare -A SITE_DIRS
SITE_DIRS=( ["/var/www/chiva.info"]=master )
# Prefix to execute commands with another user (replace `www-data` with your nginx/apache user)
# IMPORTANT: Add this line to `/etc/sudoers` to allow specific commands without
# password-prompt: `git ALL=(www-data) NOPASSWD: /usr/bin/git, /bin/mkdir`.
WWW_USER=www-data
# If the push is done by the local `WWW_USER` the hook shouldn't be executed
if [[ $(whoami) == $WWW_USER ]]; then
echo "-> Pushed by $WWW_USER. No deployment needed."
exit
fi
# Iterate over all paths to deploy to
for SITE_DIR in "${!SITE_DIRS[@]}"
do
echo "-> Deploy to $SITE_DIR.."
# Check whether the site-directory exists or not
if [ -d "$SITE_DIR" ]; then
# Discard local changes and pull updates from repository
echo "-> Directory exists already.."
cd $SITE_DIR
# Be sure the repository ignores file-permissions & select simple push-mode
echo "-> (Re-)Configure Repository.."
sudo -u $WWW_USER git --work-tree=./ config core.fileMode false
sudo -u $WWW_USER git --work-tree=./ config push.default simple
echo "-> Revert all local changes.."
sudo -u $WWW_USER git --work-tree=./ clean -ffd
sudo -u $WWW_USER git --work-tree=./ checkout -f ${SITE_DIRS[$SITE_DIR]}
echo "-> Pull changes.."
sudo -u $WWW_USER git --work-tree=./ pull
echo "-> (Re-)Initialize Submodules.."
sudo -u $WWW_USER git --wotk-tree=./ submodule sync
sudo -u $WWW_USER git --work-tree=./ submodule update --init --recursive
else
# Completely clone the repository if the directory doesn't exist yet
echo "-> Directory doesn't exist yet.."
echo "-> Recursive Cloning.."
sudo -u $WWW_USER git clone --recursive -b ${SITE_DIRS[$SITE_DIR]} --single-branch $GIT_DIR $SITE_DIR
cd $SITE_DIR
echo "-> Create some necessary directories for Kirby.."
sudo -u $WWW_USER mkdir -p thumbs
sudo -u $WWW_USER mkdir -p site/accounts
sudo -u $WWW_USER mkdir -p site/cache
# Be sure the repository ignores file-permissions & select simple push-mode
echo "-> (Re-)Configure Repository.."
sudo -u $WWW_USER git --work-tree=./ config core.fileMode false
sudo -u $WWW_USER git --work-tree=./ config push.default simple
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment