Skip to content

Instantly share code, notes, and snippets.

@d00vy
Last active February 12, 2023 00:17
Show Gist options
  • Save d00vy/8dff02f2f40b9fb37670df8b06bc4aae to your computer and use it in GitHub Desktop.
Save d00vy/8dff02f2f40b9fb37670df8b06bc4aae to your computer and use it in GitHub Desktop.
Git hook post-receive for automated Hugo deployments
#!/bin/bash
## Set to exit immediately on errors
set -eu
## Feedback to user that post-receive script is running.
echo ""
echo " /==============================="
echo " | Push received ... Beginning Build and Deployment script ..."
echo " |"
TARGET="/path/to/deploy-folder" # Deployment location - Where Hugo should put the /public/ contents after build
BUILD="/path/to/build-folder" # Build location - Where the repo will be copied, and the source folder for Hugo to build
PUBLIC="/var/www/html/website.com" # Public location - Where you serve your http content `/var/www/html/PUBLIC`
GIT_DIR="/path/to/your/repo.git" # Git Repository location - bare git folder - `git init --bare /path/to/GIT_DIR`
BRANCH="main" # Only pushes to this branch will deploy
HUGO_VERSION=$(hugo version) # Get the current version of Hugo
NOW=$(date +"%Y%m%d-%H%M") # Get current date
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=$BUILD --git-dir=$GIT_DIR checkout -f $BRANCH # check out the master branch into the BUILD folder
git tag release_$NOW $BRANCH ## set a tag with the $NOW variable
echo " | Building with Hugo from $BUILD to $TARGET..."
echo "Running $HUGO_VERSION"
hugo -v --minify --enableGitInfo -s $BUILD -d $TARGET
echo " | Symlinking $TARGET to $PUBLIC"
ln -s $TARGET $PUBLIC
# Provide feedback
echo " |"
echo " | *DEPLOYMENT COMPLETED*"
echo " | Deployed branch : $BRANCH"
echo " | Built from : $BUILD"
echo " | Built to : $TARGET"
echo " | Symlinked to : $PUBLIC"
echo " | Tag name : release_$NOW"
echo " \==============================="
else
echo " | Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment