B.nana Technology Log in

Git post-receive hook

The following post-receive hook works with MarkdownUp:

#!/bin/bash
TARGET="/path/to/checkout"
GIT_DIR="/path/to/bare/git"
BRANCH="main"

while read oldrev newrev ref
do
        if [ "$ref" = "refs/heads/$BRANCH" ];
        then
                echo "Stopping MarkdownUp service"
                sudo systemctl stop markdownup-docs
                echo "Removing old docs"
                find $TARGET -mindepth 1 -maxdepth 1 -exec rm -r {} \;
                echo "Deploying new docs..."
                umask 002
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
                echo "Restarting MarkdownUp..."
                sudo systemctl start markdownup-docs
                echo "Done"
        fi
done

Add the following lines with visudo to allow the relevant group (e.g. %markdownup instead of %group) to run the systemctl start and stop commands:

%group ALL=(ALL) NOPASSWD: /bin/systemctl start markdownup-docs
%group ALL=(ALL) NOPASSWD: /bin/systemctl stop markdownup-docs