From 89571e99dd3bca7141488fe41f3f1f01b76789a5 Mon Sep 17 00:00:00 2001 From: maride Date: Mon, 28 Aug 2023 23:40:36 +0200 Subject: [PATCH] Allow passthrough repos using bind mounts, work in temp directory, log to screen and file --- README.md | 15 ++++++++++++++- aux/pull-n-build.sh | 33 +++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b6a120e..4d34e82 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,18 @@ A nginx-powered container hosting a hugo-built blog regularly pulled off git. ## Run -`docker run -e REPO_URL=https://git.maride.cc/maride/sec.maride.cc.git -p 80:80 blogker` +### Auto-Pull Git +If you store your blog data in a git repository, run the container with env `REPO_URL` set to a Git repo: + +`docker run -e REPO_URL=https://git.maride.cc/maride/sec.maride.cc.git -p 80:80 -d blogker` + +The repository will be pulled and built every 10 minutes. + +### Passthrough via Bind + +To just use a specific directory as the hugo blog contents, run the container with the repository passed through as bind directory: + +`docker run -v /var/myblog:/repo -p 80:80 -d blogker` + +The repository will be built every 10 minutes, but you need to implement a mechanism to update the blog repository on your own. This setup can be handy in a testing/dev environment where you don't want to have all your blog posts published right away. diff --git a/aux/pull-n-build.sh b/aux/pull-n-build.sh index 43c872d..731ff7b 100755 --- a/aux/pull-n-build.sh +++ b/aux/pull-n-build.sh @@ -8,18 +8,35 @@ set -e -v date # check if initial git run - if [ ! -d /repo ]; then - # yes - repo needs to be cloned - git clone --recurse-submodules $REPO_URL /repo + if [ "$REPO_URL" == "" ]; then + # no repo URL set. + # most likely the repo is passed through as volume + # do not pull, treat the repo as-is + true else - # no - pull existing repo - cd /repo && git pull --recurse-submodules + # repo URL set + if [ ! -d /repo ]; then + # repo needs to be cloned + git clone --recurse-submodules $REPO_URL /repo + else + # no - pull existing repo + cd /repo && git pull --recurse-submodules + fi fi + # create temporary working directory + tmpdir=$(mktemp --directory) + # copy over into temporary directory + cp -r /repo/* $tmpdir + # build and copy over - cd /repo && \ + cd $tmpdir && \ hugo && \ rm -rf /usr/share/nginx/html/* && \ - cp -R /repo/public/* /usr/share/nginx/html + cp -R $tmpdir/public/* /usr/share/nginx/html && \ + chown nginx:nginx -R /usr/share/nginx/html -} 2>&1 > /var/log/blogker-pull-n-build.log + # cleanup + rm -rf $tmpdir + +} 2>&1 | tee /var/log/blogker-pull-n-build.log