#!/bin/sh # Execute Hugo webiste generation and log all output into log file # In error case, existing blog content will not be touched # Path to hugo executable # HUGO=~/bin/hugo HUGO=~/kred/hugo/hugo # Path to output destination. Folder must exists #PUBLIC_WWW=/var/www/virtual/$USER/html/kollegenrunde PUBLIC_WWW=~/hugotest/public # Path to temp dir # TMPDIR=$(mktemp -d) TMPDIR=. # Path to persisted log file, e.g. in web folder LOG=$TMPDIR/log.txt # Path to a temp log file for hugo's stdout HUGO_LOG=$TMPDIR/hugolog.txt # Path to a temp log file for hugo's errout HUGO_ERR=$TMPDIR/hugoerr.txt # Tmp file with timestamp string DATE=$TMPDIR/date.txt # Path to our repositories GIT_HUGO=https://kollegen.uber.space/gitea/kollegenrunde/kollegen-hugo.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_file () { cat $1 cat $1 >> $LOG } # log a string argument log () { echo $1 echo $1 >> "$LOG" } # Always produce a log file in the destination folder finish () { date > $DATE log_file $DATE if [ -f $LOG ] then mv $LOG $PUBLIC_WWW fi rm -r -f $TMPDIR exit $1 } ### MAIN ############################################### if [ ! -d $TMPDIR ] then mkdir $TMPDIR log "Created $TMPDIR" fi if [ -f $LOG ] then log "Old log found: remove $LOG" rm $LOG fi log "Using tmp dir $TMPDIR" # Document actual time into log file date >> $DATE log_file $DATE if [ -f $HUGO_ERR ] then log "Old err log found: remove $HUGO_ERR" rm $HUGO_ERR fi if [ ! -d $SUBGIT_POSTS ] then # jetzt clonen wir das Repository in den temporären Ordner echo "Clone $GIT_POSTS" git clone $GIT_POSTS $SUBGIT_POSTS else echo "Pulling from $GIT_POSTS" cd $SUBGIT_POSTS git pull $GIT_POSTS cd .. fi if [ ! -d $SUBGIT_HUGO ] then # jetzt clonen wir das Repository in den temporären Ordner log "Clone $GIT_HUGO incl. Theme into $SUBGIT_HUGO" git clone --recursive $GIT_HUGO $SUBGIT_HUGO else log "Pulling from $GIT_HUGO" cd $SUBGIT_HUGO git pull $GIT_HUGO fi # Hugo anschmeißen log "Starting hugo" $HUGO --destination $TMPDIR > $HUGO_LOG 2> $HUGO_ERR if [ -f $HUGO_LOG ] then log_file $HUGO_LOG fi if [ -f $HUGO_ERR ] then if grep -Fq "Error" $HUGO_ERR then log "Hugo failed" log_file $HUGO_ERR log "Generation canceled. Check the error and try to fix it. Then push again." rm $HUGO_ERR finish 1 fi fi if [ -d $PUBLIC_WWW ] then log "Remove $PUBLIC_WWW" rm -r $PUBLIC_WWW fi # Publish now log "Delete old $PUBLIC_WWW" rm -r $PUBLIC_WWW log "Copy $TMPDIR to $PUBLIC_WWW" cp -r $TMPDIR $PUBLIC_WWW log "Ready, Blog created in $PUBLIC_WWW!" finish 0