Merge pull request 'Refactor the auto build script' (#41) from auto_build_again into main

Reviewed-on: #41
pull/42/head
sixtus 2022-05-14 17:55:17 +02:00
commit 246eaf7ca9
1 changed files with 45 additions and 32 deletions

View File

@ -7,11 +7,10 @@ set -u # Treat unset variables as an error when substituting.
FORCE=${FORCE="no"}
# change this to your setup
GIT_BASE=${GIT_BASE="https://git.dezentrale.cloud"} # no trailing slash please
REMOTE_URL=${REMOTE_URL:="https://git.dezentrale.cloud/Frontend/Homepage.git"}
CLONE_DIR=${CLONE_DIR="$HOME/homepage"} # local checkout path
OWNER=${OWNER="Frontend"}
REPO=${REPO="Homepage"}
ORIGIN=${ORIGIN="origin"}
BRANCH=${BRANCH="main"}
GOPATH=${GOPATH="$HOME/go/bin"}
@ -29,18 +28,29 @@ purge_local () {
# clone repo if not already there
clone_local () {
if [ ! -d "$CLONE_DIR" ]; then
if ! git clone \
git clone \
--quiet \
--branch "$BRANCH" \
--recurse-submodules \
"${GIT_BASE}/${OWNER}/${REPO}.git" \
"$CLONE_DIR"
then
exit 1
fi
"$REMOTE_URL" \
"$CLONE_DIR" \
|| exit 1
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
}
# make sure repo is cloned, clean and on the correct branch
setup_local () {
if [ ! -d "$CLONE_DIR" ]; then
@ -62,6 +72,7 @@ setup_local () {
return 1
fi
fetch_local
return 0
}
@ -71,33 +82,14 @@ latest_local_sha () {
git -C "$CLONE_DIR" rev-parse --quiet "$BRANCH"
}
# get latest commit id of server
# get latest commit id of remote repo
latest_remote_sha () {
if ! curl --silent --fail --show-error \
-X GET \
-H "accept: application/json" \
"${GIT_BASE}/api/v1/repos/${OWNER}/${REPO}/commits?sha=${BRANCH}&limit=1" \
| jq -M -r '.[0].sha'
then
exit 1
fi
git -C "$CLONE_DIR" rev-parse --quiet "$ORIGIN/$BRANCH"
}
# determine if build is required
REQUIRED="no"
if ! setup_local; then
REQUIRED="setup"
elif [ $FORCE != "no" ]; then
REQUIRED="forced"
elif [ "$(latest_local_sha)" != "$(latest_remote_sha)" ]; then
REQUIRED="changed"
purge_local
clone_local
fi
# do the build
if [ $REQUIRED != "no" ]; then
build_page() {
TMPDIR=$(mktemp -d -t hugo_build_XXXXX)
PATH=$PATH:$GOPATH \
@ -105,9 +97,30 @@ if [ $REQUIRED != "no" ]; then
--quiet \
--enableGitInfo \
--source "$CLONE_DIR" \
--destination "$TMPDIR"
--destination "$TMPDIR" \
|| exit 1
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
fi
exit 0