1

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

danix
  • 23
  • 5

1 Answers1

0

Just in case the submodule was not initialized first, try a

git submodule init

Then your git checkout -f --recurse-submodules should have a chance to update the submodule content according to the commit recorded in the superproject.

The OP confirms however that, as illustrated in this answer, you need to be in the working tree for git submodule init to work.

So instead of:

git --work-tree=$TARGET_DIR --git-dir=./

Do:

GIT_DIR=$(pwd)
cd "${TARGET_DIR}"
git --work-tree=./ --git-dir="${GIT_DIR}"/ submodule init
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks @VonC, but it doesn't work, I've modified the line like this ```git --work-tree=$TARGET_DIR --git-dir=./ checkout -f git --work-tree=$TARGET_DIR --git-dir=./ submodule init git --work-tree=$TARGET_DIR --git-dir=./ submodule update --init``` but submodule init complains that it can't be used without a working tree I don't get it. – danix Feb 18 '23 at 07:23
  • Just to integrate, I've tried explicitly declaring the GIT_DIR directory and `checkout -f` works, but `submodule init` still complains that git-submodule can't be used without a working tree. I don't understand why one git subcommand works and the other doesn't. – danix Feb 18 '23 at 08:20
  • @danix is `$TARGET_DIR` empty? – VonC Feb 18 '23 at 08:57
  • no, it is populated by hugo before the `git checkout -f`, but the checkout command is not complaining and the files are checked out correctly, `git submodule init` is complaining even though the destination folder of the submodule is empty and the work tree is populated before I run the command – danix Feb 18 '23 at 09:52
  • this is the output from the remote ``` remote: Ref refs/heads/master received. Deploying 'master' branch to production... remote: + git --work-tree=/tmp/danix.xyz-compile checkout -f remote: + git --work-tree=/tmp/danix.xyz-compile submodule init remote: fatal: /usr/lib/git-core/git-submodule cannot be used without a working tree. ``` – danix Feb 18 '23 at 09:55
  • 1
    @danix Maybe cd to the worktree, and reference your gitdir path instead of the current command which does the opposite. See [this answer](https://stackoverflow.com/a/64621032/6309) as an example. – VonC Feb 18 '23 at 17:12
  • @danix I have seen your edit, and I have edited my answer to reflect the proper setup for `git submodule init` to work. – VonC Feb 18 '23 at 20:22