Homepage/auto_build.sh

129 lines
2.5 KiB
Bash
Raw Permalink Normal View History

2021-07-08 19:01:40 +02:00
#!/usr/bin/env sh
#set -x # Display the value of PS4, followed by the command and its arguments.
2021-07-08 19:01:40 +02:00
set -e # Exit immediately if a command exits with a non-zero status.
set -u # Treat unset variables as an error when substituting.
FORCE=${FORCE="no"}
# change this to your setup
REMOTE_URL=${REMOTE_URL:="https://git.dezentrale.cloud/Frontend/Homepage.git"}
2021-07-08 19:01:40 +02:00
CLONE_DIR=${CLONE_DIR="$HOME/homepage"} # local checkout path
ORIGIN=${ORIGIN="origin"}
2021-07-08 19:01:40 +02:00
BRANCH=${BRANCH="main"}
HUGO_CMD=${HUGO_CMD="hugo"}
2021-07-08 19:01:40 +02:00
WWWDIR=${WWWDIR="/var/www/www"}
# bugs ahead
# delete repo if it is already there
purge_local () {
if [ -d "$CLONE_DIR" ]; then
rm -rf "$CLONE_DIR"
fi
}
# clone repo if not already there
clone_local () {
if [ ! -d "$CLONE_DIR" ]; then
git clone \
2021-07-08 19:01:40 +02:00
--quiet \
--branch "$BRANCH" \
--recurse-submodules \
"$REMOTE_URL" \
"$CLONE_DIR" \
|| exit 1
2021-07-08 19:01:40 +02:00
fi
}
# fetch repo updates from remote
fetch_local () {
if [ -d "$CLONE_DIR" ]; then
git -C "$CLONE_DIR" fetch \
--quiet \
--recurse-submodules \
--all \
|| exit 1
fi
}
2021-07-08 19:01:40 +02:00
# make sure repo is cloned, clean and on the correct branch
setup_local () {
if [ ! -d "$CLONE_DIR" ]; then
clone_local
return 1
fi
branch=$(git -C "$CLONE_DIR" symbolic-ref --short --quiet HEAD)
if [ "$branch" != $BRANCH ]; then
purge_local
clone_local
return 1
fi
stat=$(git -C "$CLONE_DIR" status --short)
if [ "$stat" != "" ]; then
purge_local
clone_local
return 1
fi
fetch_local
2021-07-08 19:01:40 +02:00
return 0
}
# get latest commit id of cloned repo
latest_local_sha () {
git -C "$CLONE_DIR" rev-parse --quiet "$BRANCH"
}
# get latest commit id of remote repo
2021-07-08 19:01:40 +02:00
latest_remote_sha () {
git -C "$CLONE_DIR" rev-parse --quiet "$ORIGIN/$BRANCH"
2021-07-08 19:01:40 +02:00
}
# do the build
build_page() {
# sync logo with spaceapi
2023-06-28 22:32:44 +02:00
./spaceapi.py "$CLONE_DIR"
2021-07-08 19:01:40 +02:00
TMPDIR=$(mktemp -d -t hugo_build_XXXXX)
$HUGO_CMD \
--quiet \
--enableGitInfo \
--source "$CLONE_DIR" \
--destination "$TMPDIR" \
|| exit 1
2021-07-08 19:01:40 +02:00
rm -rf "${WWWDIR:?}"/*
cp -r "$TMPDIR"/. "$WWWDIR"
rm -rf "$TMPDIR"
}
# public static void main
if ! setup_local; then
build_page
exit 0
fi
if [ $FORCE != "no" ]; then
build_page
exit 0
fi
if [ "$(latest_local_sha)" != "$(latest_remote_sha)" ]; then
purge_local
clone_local
build_page
2021-07-08 19:01:40 +02:00
fi
exit 0