Add branch deploy script

This commit is contained in:
2024-01-30 09:23:11 +00:00
parent 762efcec6f
commit b6303f291d

33
tools/branch-deploy.sh Executable file
View File

@@ -0,0 +1,33 @@
#!/bin/bash
# Deploys all branches of a Hugo repository to a structure of folders.
# Useful for deploying all PRs/branches of a Hugo installation to a test URL for viewing.
# Args: folder
TARGET_FOLDER="$1"
mkdir -p "${TARGET_FOLDER}"
# Track all remote branches locally
git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
# Pull the branches
git pull --all
git fetch --all
# Iterate each local branch, and check it out into tmp folder, then build into the destination folder
for BRANCH in $(git for-each-ref --format='%(refname:short)' refs/heads); do
echo "Building ${BRANCH}"
# Make folders
mkdir -p "${TARGET_FOLDER}/${BRANCH}"
TEMP_FOLDER=`mktemp -d`
# Checkout the branch
git --work-tree="${TEMP_FOLDER}" checkout "${BRANCH}" -- .
# Build to the destination folder
hugo --gc -b "${URL_BASE}/${BRANCH}" -s "${TEMP_FOLDER}" -d "${TARGET_FOLDER}/${BRANCH}"
# Cleanup the temp folder
rm -rf "${TEMP_FOLDER}"
done