I'm setting up git on a server to be able to compile a hugo website and publish it to /var/www/html ready to be served on the internet.
What I do in short is:
- create a temporary hugo site
- checkout the repository and its submodule (hugo theme) inside the same directory
- build with hugo deploying the static files directly to /var/www/html
What I don't understand is why git --work-tree=$TARGET_DIR --git-dir=./ checkout -f --recurse-submodules
doesn't pull the submodule as expected.
Can anyone shed some light as to what I'm doing wrong? Here's the script:
#!/bin/bash
# Directory where to work on our site
TARGET_DIR=/tmp/site-compile
# Public dir where to push the site once compiled
PUBLIC_WWW="/var/www/html"
BACKUP_WWW=$HOME/site-backup
SITE_DOMAIN=https://site.ext
# repository logical name
REPO="site.ext"
# Branch that is going to be deployed to server
BRANCH="master"
# date to be appended to latest tag
NOW=$(date +"%d%m%Y-%H%M")
set -xe
# delete the working directory first
rm -rf $TARGET_DIR
# create new temporary site
/usr/local/bin/hugo new site $TARGET_DIR
# backup public www directory first then setup trap
rsync -avz --no-t $PUBLIC_WWW/ $BACKUP_WWW
trap "echo 'A problem occurred. Reverting to backup.'; rsync -avz --no-t --del $BACKUP_WWW/ $PUBLIC_WWW; rm -rf $TARGET_DIR" EXIT
while read oldrev newrev ref
do
# if TARGET_DIR is empty we don't want deploy for this project
if [[ ! $TARGET_DIR == "" ]]; then
if [[ "$GL_REPO" == "$REPO" ]]; then
# let's check that we are deploying to the correct branch
if [[ $ref = refs/heads/${BRANCH} ]]; then
echo "Ref $ref received. Deploying '${BRANCH}' branch to production..."
git --work-tree=$TARGET_DIR --git-dir=./ checkout -f --recurse-submodules
rm ${TARGET_DIR}/config.toml
rm -rf $PUBLIC_WWW/*
/usr/local/bin/hugo -s $TARGET_DIR -d $PUBLIC_WWW -b "${SITE_DOMAIN}" -t "dagreynix" --noTimes --minify
git tag release_$NOW $BRANCH
echo " /==============================="
echo " | DEPLOYMENT COMPLETED - ${REPO}"
echo " | Target branch: ${BRANCH}"
echo " | Target folder: ${PUBLIC_WWW}"
echo " | Tag name : release_${NOW}"
echo " \=============================="
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
fi
else
echo "Target directory not declared. Skipping deploy to server."
fi
done
rm -rf $TARGET_DIR
trap - EXIT
The site.ext repository contains the content/
directory, hugo.toml
settings file and static/
directory, the submodule is a hugo theme.
In short the post-receive script should update the submodule in themes/theme-folder and pull the relative files, otherwise hugo can't properly build the site.
Thanks in advance
[EDIT]
I've solved thanks to @VonC by following this comment