Hugo deploy using git post-receive hook on self hosted server

Hello everyone,
I’m cross posting this question from stackoverflow because I wanted to reach a broader audience. I hope I’m not doing something wrong here.

Here’s the stackoverflow question:

So basically I have this 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

Which is my post-receive hook on my git bare repository.

What I’m trying to achieve is to push changes to my content to this repository, git then builds the static files using hugo on the server and outputs everything to my public directory exposed by my webserver.

The problem I’m facing is that I can’t make git update the submodule for my theme directory, and of course I can’t build the site without the theme.

The setup is as follows:
In the content repository (the one that’s running the script above) I have content/, static/ and hugo.toml as well as a .gitignore to ignore everything else and a .gitmodules where I’ve defined a submodule for my theme.

Can you guys help me figure out what am I doing wrong?
Thanks a lot in advance, and sorry if I went offtopic on this thread.

Cheers