Files
staticzolaserver-docker/git2zola.sh
T

191 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# bash deamon to check git for new content
# make a pull from repository
# - with ssh keys ( problem with private key in this repository )
# - with https user login ( problrm with TLS error with own gogs
# - with auth token (not implemented in gogs yet)
# ssh://gogs@gogs.serinty.net:22104/Serinty/serinty-website.git
#GITCLONEURL=https://${GITUSER}:${GITPASSWD}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITPROJECTNAME}.git
# shellcheck source=./getOrCreateSshKeys.sh
source getOrCreateSshKeys.sh
# Set default values for variables
# one day
IDLE_TIME=${IDLE_TIME:-86400}
# port over 1024 for normal user
SWSPORT=${SWSPORT:=8970}
# projectname of the git repository with the data and conf for zola
GITPROJECTNAME=${GITPROJECTNAME:-serinty-website}
# projectname of the git repository with the theme for zola
GITTHEMENAME=${GITTHEMENAME:-serinty-zola-theme}
# the name of the theme, which is configed in the zola project, can be diffrent to the GITTHEMENAME
THEME_NAME=${THEME_NAME:-$GITTHEMENAME}
# User or Organisation name of the repository
GITPROJECTUSER=${GITPROJECTUSER:-Serinty}
# name or ip of the git server
GITSERVER=${GITSERVER:-gogs.serinty.net}
# ssh port of the git server
GITSERVERPORT=${GITSERVERPORT:-22104}
# ssh git user
GITUSER=${GITUSER:-gogs}
# password of the https login, not used because https isn't used
GITPASSWD=${GITPASSWD:-gogspw}
# url to clone zola data and config
GITCLONEURL=${GITCLONEURL:-"ssh://$GITUSER@$GITSERVER:$GITSERVERPORT/$GITPROJECTUSER/$GITPROJECTNAME.git"}
# url to clone zola data and config
GITTHEMECLONEURL=${GITTHEMECLONEURL:-"ssh://$GITUSER@$GITSERVER:$GITSERVERPORT/$GITPROJECTUSER/$GITTHEMENAME.git"}
BASE_DOMAIN=${BASE_DOMAIN:-"docker.local"}
BASE_PROTOCOL=${BASE_PROTOCOL:-"http"}
BASE_URL=${BASE_URL:-"${BASE_PROTOCOL}://${BASE_DOMAIN}:${SWSPORT}"}
# Zola output build and sws path
OUTPUT_DIR=${OUTPUT_DIR:-$HOME/$GITPROJECTUSER/$GITPROJECTNAME/public}
GITBRANCHNAME=${GITBRANCHNAME:-"master"}
# set trap to exit this process
# at this time only exit this process if it get a exit code
trap "exit" EXIT
startSWSifNotRunning () {
#static-web-server is /usr/local/bin/static-web-server and in path
if pgrep -x "static-web-server" >/dev/null
then
echo "webserver is running"
pgrep -x "static-web-server"
else
echo "try to start webserver:"
if [ -f "$OUTPUT_DIR/index.html" ]; then
echo "starting webserver on port $SWSPORT with content:"
ls -la "$OUTPUT_DIR" && static-web-server --port "$SWSPORT" --log-level warn --root "$OUTPUT_DIR" &
else
echo "ERROR: index.html not found in $OUTPUT_DIR"
exit 2
fi
fi
}
# pull new data an build site
pullAndBuild () {
echo "getting new data"
git pull --ff-only || errorAndExit "can't pull content repository" 1
echo "build website with newer data to ${OUTPUT_DIR}"
zola build --force --base-url "${BASE_URL}" --output-dir "${OUTPUT_DIR}" || errorAndExit "can't build website" 2
}
goProjectPath () {
echo "change into project directory ${HOME}/${GITPROJECTNAME}"
cd "${HOME}/${GITPROJECTNAME}" || exit 5
}
# write error text into err console and exit
# param1: error text
# param2: exit code
errorAndExit () {
error_text=$1
# if error text is empty, set default text
if [ -z "$error_text" ]
then
error_text="unknown error"
fi
error_code=$2
#if error code is empty, set default code
if [ -z "$error_code" ]
then
error_code=1
fi
echo "ERROR: $error_text"
exit "$error_code"
}
# clone or pull the theme
getTheme () {
if [ ! -d ./themes ]
then
echo "make themes directory"
mkdir themes || errorAndExit "can't make themes directory" 1
fi
echo "change into themes directory"
cd ./themes || errorAndExit "can't change into themes directory" 2
if [ ! -d ./"${THEME_NAME}" ]
then
echo "clone theme with ${GITTHEMECLONEURL} ${THEME_NAME}"
git clone -b "${GITBRANCHNAME}" "${GITTHEMECLONEURL}" "${THEME_NAME}" || errorAndExit "can't clone theme" 3
fi
if [ -d ./"${THEME_NAME}" ]
then
echo "change into theme directory ${THEME_NAME}"
cd ./"${THEME_NAME}" || errorAndExit "can't change into theme directory" 2
# check if there are changes in the theme repository
echo "check if there are changes in the theme repository"
git fetch || errorAndExit "can't fetch theme repository" 4
if git diff --quiet FETCH_HEAD;
then
echo "getting updated design"
git pull --ff-only || errorAndExit "can't pull theme repository" 4
echo "buid website with newer theme"
goProjectPath
pullAndBuild
else
goProjectPath
fi
else
errorAndExit "no theme ${THEME_NAME} found" 5
fi
}
getContent () {
goProjectPath
# check if there are changes in the content repository
echo "check if there are changes in the content repository"
git fetch || errorAndExit "can't fetch content repository" 4
if git diff --quiet FETCH_HEAD;
then
echo "getting updated content"
pullAndBuild
fi
}
echo "Starting $_"
getOrCreateSshKeys
echo "please instert public ssh key into git repository to access ${GITCLONEURL} and restart this container:"
cat ~/.ssh/id_ed25519.pub
ssh-keygen -lf ~/.ssh/id_ed25519.pub
echo "Using git command: $GIT_SSH_COMMAND"
echo "Working directory:"
pwd || echo "Error: pwd failed"
echo "put git server ${GITSERVER} to known hosts"
ssh-keyscan -p "${GITSERVERPORT}" -H "${GITSERVER}" >> ~/.ssh/known_hosts
zola --version
echo "checking local repository directory"
if [ ! -d "${GITPROJECTNAME}" ]; then
echo "no local repository directory found, cloning ${GITPROJECTNAME} from ${GITCLONEURL}"
git clone -b "${GITBRANCHNAME}" "${GITCLONEURL}" || exit 9
echo "change into project path ${GITPROJECTNAME}"
cd "${GITPROJECTNAME}" || exit 6
getTheme
getContent
fi
while true
do
getTheme
getContent
startSWSifNotRunning
echo "Sleeping for ${IDLE_TIME} seconds"
sleep "${IDLE_TIME}"
done