Refactor the auto build script
- Replace lookup to gitea api with "git fetch" - Drops dependency of "curl" and "jq" \o/ - Make sure commands do bail out on error - 'if ! command; then exit 1; fi' -> does not work reliably - 'command || exit 1' -> does work \o/ - Bail out if hugo fails - Replace main 'if … elif … fi' cascade with single "if … fi" blocks
This commit is contained in:
parent
a9d3a83108
commit
58f24dfa7c
|
@ -7,11 +7,10 @@ set -u # Treat unset variables as an error when substituting.
|
||||||
FORCE=${FORCE="no"}
|
FORCE=${FORCE="no"}
|
||||||
|
|
||||||
# change this to your setup
|
# 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
|
CLONE_DIR=${CLONE_DIR="$HOME/homepage"} # local checkout path
|
||||||
|
|
||||||
OWNER=${OWNER="Frontend"}
|
ORIGIN=${ORIGIN="origin"}
|
||||||
REPO=${REPO="Homepage"}
|
|
||||||
BRANCH=${BRANCH="main"}
|
BRANCH=${BRANCH="main"}
|
||||||
|
|
||||||
GOPATH=${GOPATH="$HOME/go/bin"}
|
GOPATH=${GOPATH="$HOME/go/bin"}
|
||||||
|
@ -29,18 +28,29 @@ purge_local () {
|
||||||
# clone repo if not already there
|
# clone repo if not already there
|
||||||
clone_local () {
|
clone_local () {
|
||||||
if [ ! -d "$CLONE_DIR" ]; then
|
if [ ! -d "$CLONE_DIR" ]; then
|
||||||
if ! git clone \
|
git clone \
|
||||||
--quiet \
|
--quiet \
|
||||||
--branch "$BRANCH" \
|
--branch "$BRANCH" \
|
||||||
--recurse-submodules \
|
--recurse-submodules \
|
||||||
"${GIT_BASE}/${OWNER}/${REPO}.git" \
|
"$REMOTE_URL" \
|
||||||
"$CLONE_DIR"
|
"$CLONE_DIR" \
|
||||||
then
|
|| exit 1
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
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
|
# make sure repo is cloned, clean and on the correct branch
|
||||||
setup_local () {
|
setup_local () {
|
||||||
if [ ! -d "$CLONE_DIR" ]; then
|
if [ ! -d "$CLONE_DIR" ]; then
|
||||||
|
@ -62,6 +72,7 @@ setup_local () {
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
fetch_local
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,33 +82,14 @@ latest_local_sha () {
|
||||||
git -C "$CLONE_DIR" rev-parse --quiet "$BRANCH"
|
git -C "$CLONE_DIR" rev-parse --quiet "$BRANCH"
|
||||||
}
|
}
|
||||||
|
|
||||||
# get latest commit id of server
|
# get latest commit id of remote repo
|
||||||
latest_remote_sha () {
|
latest_remote_sha () {
|
||||||
if ! curl --silent --fail --show-error \
|
git -C "$CLONE_DIR" rev-parse --quiet "$ORIGIN/$BRANCH"
|
||||||
-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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# 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
|
# do the build
|
||||||
if [ $REQUIRED != "no" ]; then
|
build_page() {
|
||||||
TMPDIR=$(mktemp -d -t hugo_build_XXXXX)
|
TMPDIR=$(mktemp -d -t hugo_build_XXXXX)
|
||||||
|
|
||||||
PATH=$PATH:$GOPATH \
|
PATH=$PATH:$GOPATH \
|
||||||
|
@ -105,9 +97,30 @@ if [ $REQUIRED != "no" ]; then
|
||||||
--quiet \
|
--quiet \
|
||||||
--enableGitInfo \
|
--enableGitInfo \
|
||||||
--source "$CLONE_DIR" \
|
--source "$CLONE_DIR" \
|
||||||
--destination "$TMPDIR"
|
--destination "$TMPDIR" \
|
||||||
|
|| exit 1
|
||||||
|
|
||||||
rm -rf "${WWWDIR:?}"/*
|
rm -rf "${WWWDIR:?}"/*
|
||||||
cp -r "$TMPDIR"/. "$WWWDIR"
|
cp -r "$TMPDIR"/. "$WWWDIR"
|
||||||
rm -rf "$TMPDIR"
|
rm -rf "$TMPDIR"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# public static void main
|
||||||
|
if ! setup_local; then
|
||||||
|
build_page
|
||||||
|
exit 0
|
||||||
fi
|
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
|
||||||
|
|
Reference in New Issue