Browse Source

Improved generate.sh

master
Chris 6 years ago
parent
commit
6dae15cb92
  1. 122
      admin/generate.sh

122
admin/generate.sh

@ -1,38 +1,41 @@
#!/bin/sh #!/bin/sh
# Execute Hugo webiste generation and log all output into log file # Execute Hugo webiste generation and log all output into log file
# In error case, existing blog content will not be touched # In error case, existing blog content will not be touched
# If this script ist not stored in ~/hugogentmp/ change $DIR
# Path to hugo executable # Path to hugo executable
# HUGO=~/bin/hugo HUGO=~/bin/hugo
HUGO=~/kred/hugo/hugo # HUGO=~/kred/hugo/hugo
# Path to output destination. Folder must exists # Path to output destination. Folder must exists
#PUBLIC_WWW=/var/www/virtual/$USER/html/kollegenrunde PUBLIC_WWW=/var/www/virtual/$USER/html/kollegenrunde
PUBLIC_WWW=~/hugotest/public #PUBLIC_WWW=~/hugotest/public
# Path to temp dir # Working dir: here is the script and git's working dirs
# TMPDIR=$(mktemp -d) TMPDIR=~/hugogentmp
TMPDIR=.
TMPPUBLIC=$TMPDIR/public
# Path to persisted log file, e.g. in web folder # Path to persisted log file, e.g. in web folder
LOG=$TMPDIR/log.txt LOG=$TMPDIR/log.txt
# Path to a temp log file for hugo's stdout # Path to a temp log file for hugo's stdout
HUGO_LOG=$TMPDIR/hugolog.txt TMPLOG=$TMPDIR/tmplog.txt
# Path to a temp log file for hugo's errout # Path to a temp log file for hugo's errout
HUGO_ERR=$TMPDIR/hugoerr.txt TMPERR=$TMPDIR/tmperr.txt
# Tmp file with timestamp string # Tmp file with timestamp string
DATE=$TMPDIR/date.txt TMPDATE=$TMPDIR/tmpdate.txt
# Paths to the local gits
TMPGIT_HUGO=$TMPDIR/kollegen-hugo
TMPGIT_POSTS=$TMPDIR/kollegen-posts
# Path to our repositories # Path to our repositories
GIT_HUGO=https://kollegen.uber.space/gitea/kollegenrunde/kollegen-hugo.git GIT_HUGO=https://kollegen.uber.space/gitea/kollegenrunde/kollegen-hugo.git
GIT_POSTS=https://kollegen.uber.space/gitea/kollegenrunde/kollegen-posts.git GIT_POSTS=https://kollegen.uber.space/gitea/kollegenrunde/kollegen-posts.git
# Paths to the local gits
SUBGIT_HUGO=kollegen-hugo
SUBGIT_POSTS=kollegen-posts
# Log a text file content # Log a text file content
log_file () { log_file () {
@ -44,104 +47,109 @@ log_file () {
# log a string argument # log a string argument
log () { log () {
echo $1 echo $1
echo $1 >> "$LOG" echo $1 >> $LOG
} }
# Always produce a log file in the destination folder # Always produce a log file in the destination folder
finish () { finish () {
date > $DATE date > $TMPDATE
log_file $DATE log_file $TMPDATE
if [ -f $LOG ] if [ -f $LOG ]
then then
mv $LOG $PUBLIC_WWW echo "Moving log file to $LOG $PUBLIC_WWW"
mv -v $LOG $PUBLIC_WWW
fi
if [ -f $TMPLOG ]
then
rm -v $TMPLOG
fi
if [ -f $TMPERR ]
then
rm -v $TMPERR
fi fi
rm -r -f $TMPDIR if [ -f $TMPDATE ]
then
rm -v $TMPDATE
fi
exit $1 exit $1
} }
### MAIN ############################################### ### MAIN ###############################################
if [ ! -d $TMPDIR ]
if [ -d $TMPPUBLIC ]
then then
mkdir $TMPDIR rm -r -f $TMPPUBLIC
log "Created $TMPDIR"
fi fi
if [ -f $LOG ] if [ -f $LOG ]
then then
log "Old log found: remove $LOG" rm -v $LOG
rm $LOG touch $LOG
fi fi
log "Using tmp dir $TMPDIR"
# Document actual time into log file # Document actual time into log file
date >> $DATE date > $TMPDATE
log_file $DATE log_file $TMPDATE
if [ -f $HUGO_ERR ] log "Using tmp dir $TMPDIR"
then
log "Old err log found: remove $HUGO_ERR"
rm $HUGO_ERR
fi
if [ ! -d $SUBGIT_POSTS ] if [ ! -d $TMPGIT_POSTS ]
then then
# jetzt clonen wir das Repository in den temporären Ordner # jetzt clonen wir das Repository in den temporären Ordner
echo "Clone $GIT_POSTS" mkdir -v $TMPGIT_POSTS 2>&1 | tee -a $LOG
git clone $GIT_POSTS $SUBGIT_POSTS git clone $GIT_POSTS $TMPGIT_POSTS 2>&1 | tee -a $LOG
else else
echo "Pulling from $GIT_POSTS" cd $TMPGIT_POSTS && git pull $GIT_POSTS 2>&1 | tee -a $LOG
cd $SUBGIT_POSTS
git pull $GIT_POSTS
cd ..
fi fi
if [ ! -d $SUBGIT_HUGO ]
if [ ! -d $TMPGIT_HUGO ]
then then
# jetzt clonen wir das Repository in den temporären Ordner # jetzt clonen wir das Repository in den temporären Ordner
log "Clone $GIT_HUGO incl. Theme into $SUBGIT_HUGO" mkdir -v $TMPGIT_HUGO 2>&1 | tee -a $LOG
git clone --recursive $GIT_HUGO $SUBGIT_HUGO git clone $GIT_POSTS $TMPGIT_HUGO 2>&1 | tee -a $LOG
else else
log "Pulling from $GIT_HUGO" cd $TMPGIT_HUGO && git pull $GIT_HUGO 2>&1 | tee -a $LOG
cd $SUBGIT_HUGO
git pull $GIT_HUGO
fi fi
# Hugo anschmeißen # Hugo anschmeißen
log "Starting hugo" log "Starting hugo"
$HUGO --destination $TMPDIR > $HUGO_LOG 2> $HUGO_ERR cd $TMPGIT_HUGO && $HUGO --destination $TMPPUBLIC > $TMPLOG 2> $TMPERR
if [ -f $HUGO_LOG ] if [ -f $TMPLOG ]
then then
log_file $HUGO_LOG log_file $TMPLOG
fi fi
if [ -f $HUGO_ERR ] if [ -f $TMPERR ]
then then
if grep -Fq "Error" $HUGO_ERR if grep -Fq "Error" $TMPERR
then then
log "Hugo failed" log "Hugo failed"
log_file $HUGO_ERR log_file $TMPERR
log "Generation canceled. Check the error and try to fix it. Then push again." log "Generation canceled. Check the error and try to fix it. Then push again."
rm $HUGO_ERR
finish 1 finish 1
fi fi
fi fi
if [ -d $PUBLIC_WWW ] if [ -d $PUBLIC_WWW ]
then then
log "Remove $PUBLIC_WWW" log "Remove $PUBLIC_WWW"
rm -r $PUBLIC_WWW rm -r -f $PUBLIC_WWW 2>&1 | tee -a $LOG
fi fi
# Publish now # Publish now
log "Delete old $PUBLIC_WWW" log "Copy $TMPPUBLIC to $PUBLIC_WWW"
rm -r $PUBLIC_WWW mv -v $TMPPUBLIC $PUBLIC_WWW 2>&1 | tee -a $LOG
log "Copy $TMPDIR to $PUBLIC_WWW"
cp -r $TMPDIR $PUBLIC_WWW
log "Ready, Blog created in $PUBLIC_WWW!" log "Ready, Blog created in $PUBLIC_WWW!"

Loading…
Cancel
Save