Files
staticzolaserver-docker/git2zola.sh
T

197 lines
4.2 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
if [ -z $IDLE_TIME ]
then
# one day
IDLE_TIME=86400
fi
if [ -z ${SWSPORT} ]
then
# port over 1024 for normal user
SWSPORT=8970
fi
if [ -z ${GITPROJECTNAME} ]
then
# projectname of the git repository with the data and conf for zola
GITPROJECTNAME='serinty-website'
fi
if [ -z ${GITTHEMENAME} ]
then
# projectname of the git repository with the theme for zola
GITTHEMENAME="serinty-zola-theme"
fi
if [ -z ${THEME_NAME} ]
then
# the name of the theme, which is configed in the zola project, can be diffrent to the GITTHEMENAME
THEME_NAME=${GITTHEMENAME}
fi
if [ -z ${OUTPUT_DIR} ]
then
# Zola outpu build and sws path
OUTPUT_DIR="~/${GITPROJECTNAME}/public"
fi
if [ -z ${BASE_URL} ]
then
# Domain with root path of the url
BASE_URL='docker.local'
fi
if [ -z ${GITPROJECTUSER} ]
then
# User or Organisation name of the repository
GITPROJECTUSER='Serinty'
fi
if [ -z ${GITSERVER} ]
then
# name or ip of the git server
GITSERVER='gogs.serinty.net'
fi
if [ -z ${GITSERVERPORT} ]
then
# ssh port of the git server
GITSERVERPORT=22104
fi
if [ -z ${GITUSER} ]
then
# ssh git user
GITUSER='gogs'
fi
if [ -z ${GITPASSWD} ]
then
# password of the https login, not used because https isn't used
GITPASSWD='gogspw'
fi
if [ -z ${GITCLONEURL} ]
then
# url to clone zola data and config
GITCLONEURL="ssh://${GITUSER}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITPROJECTNAME}.git"
fi
if [ -z ${GITTHEMECLONEURL} ]
then
# url to clone zola data and config
GITTHEMECLONEURL="ssh://${GITUSER}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITTHEMENAME}.git"
fi
# 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 "starting webserver"
static-web-server --port ${SWSPORT} --log-level warn --root ~/${GITPROJECTNAME}/public/ &
fi
}
# pull new data an build site
pullAndBuild () {
echo "getting new data"
git pull --ff-only
echo "buid website"
zola build --base-url $BASE_URL --output-dir $OUTPUT_DIR
}
# clone or pull the theme
getTheme () {
if [ ! -d ./themes ]
then
echo "make themes directory"
mkdir themes
fi
cd ./themes
if [ ! -d ./${THEME_NAME} ]
then
echo "clone theme with ${GITTHEMECLONEURL} ${THEME_NAME}"
git clone ${GITTHEMECLONEURL} ${THEME_NAME}
fi
if [ -d ./${GITTHEMENAME} ]
then
cd ./${GITTHEMENAME}
gitpull=$(git pull --dry-run --ff-only 2>&1)
if [ ! -z ${gitpull} ]
then
echo "getting new design"
git pull --ff-only
cd ~/${GITPROJECTNAME}
echo "buid website"
zola build --base-url $BASE_URL --output-dir $OUTPUT_DIR
fi
else
echo "no theme ${GITTHEMENAME} found"
fi
cd ~/${GITPROJECTNAME}
}
ssh-keyscan -p ${GITSERVERPORT} -H ${GITSERVER} >> ~/.ssh/known_hosts
while true
do
git status --porcelain
gitstatus=$?
echo "data status is ${gitstatus}"
if [ ${gitstatus} -eq 128 ]
then
echo "no data, cloning from ${GITCLONEURL}"
git clone ${GITCLONEURL}
echo "change into project path ${GITPROJECTNAME}"
cd ${GITPROJECTNAME}
getTheme
pullAndBuild
git status --porcelain
gitstatus=$?
fi
if [ ${gitstatus} -eq 0 ]
then
echo "testing for new data"
# all outputs from git goes to error output
gitpull=$(git pull --dry-run --ff-only 2>&1)
if [ ! -z ${gitpull} ]
then
getTheme
pullAndBuild
fi
fi
if [ -d ~/${GITPROJECTNAME} ]
then
startSWSifNotRunning
else
echo "please instert public ssh key into git repository to access ${GITCLONEURL} and restart this container:"
cat ~/.ssh/id_ed25519.pub
exit
fi
sleep ${IDLE_TIME}
done