When setting up my blog, I decided that the only action I wanted to do to update the live version was to do git push
.
To implement this, I skipped setting up a webhook server because I didn’t want to install the webhook server directly on the host : this would have used a port and required manual configuration of my reverse proxy (I use traefik).
Instead, I implemented it with :
- a git hook
- SSH - Fire and forget command
- a remote script
- docker-compose
Git hook
There is no post-push
hook, that’s a point where using a Gitlab webhook would have been best. Instead I used the pre-push
hook (in .git/hooks/pre-push) :
#! /usr/bin/env bash
ssh host /src/docker-services/active/personal-website/update.sh
Remote script
#! /usr/bin/env bash
SCRIPT_FOLDER=${0%/*}
SCRIPT_NAME=${0##*/}
DEBUG=1
ERROR_LOG_FILE=$SCRIPT_FOLDER/error-log.txt
# Cleanup
rm -f $ERROR_LOG_FILE
if [[ $1 != "--async-update" ]]; then
cd $SCRIPT_FOLDER
screen -S personal-website-update -d -m ./$SCRIPT_NAME --async-update
else
sleep 10 # Wait for git push to complete
LOG="$LOG Starting git pull"
LOG+="\n $(git -C ./src pull 2>&1)"
if [[ $? != 0 ]]; then
LOG+="\n /!\ Error with git pull"
echo -e "$LOG" > $ERROR_LOG_FILE
exit 1
fi
# echo "Starting docker-compose up -d --build"
LOG+="\n $(docker-compose up -d --build)"
if [[ $? != 0 ]]; then
LOG+="\n /!\ Error with docker-compose"
echo -e "$LOG" > $SCRIPT_FOLDER/error-log.txt
exit 1
fi
[[ $DEBUG == 1 ]] && echo -e "$LOG" > $SCRIPT_FOLDER/log.txt
exit 0
fi
Blog docker image
As you can see in the script, I build an image and update the running container. Here are the Dockerfile
and the docker-compose
:
FROM klakegg/hugo:ext-alpine-onbuild AS hugo
FROM nginx
COPY --from=hugo /target /usr/share/nginx/html
services:
blog:
build:
context: .
args:
HUGO_ENV_ARG: "production"
HUGO_CMD: "-s /src/hugo-src"
ports:
- 8080:80
Note: This is note my actual docker-compose.yml
file, I use labels to expose it through Traefik