blogcontent/deployBlog.sh
Jack Jackson abab21e297 Self-host upload to Google Analytics
Some client configurations (Browsers with tracking-blocking enabled, or
networks with [Pi-holes](https://pi-hole.net/)) will block any calls
to `www.google-analytics.com`. This makes me sad! I'd like to know
how many folks are reading my blog!

This commit represents a workaround. Instead of telling the client to
send data directly to Google (which would trigger Tracking-blockers), I
instead tell the client to send it to the `/analytics` endpoint on my
domain, which will then get forwarded to Google. Neat!

But! If a reader has blocked tracking, we should respect that! So I'm
not actually enabling this in my blog until I have also built an
"opt-in" mechanism, where readers can decide if they want to send me
tracking information or not.

"Why didn't you make tracking opt-in to begin with, Jack?" - well, I
kinda addressed that
[here](https://blog.scubbo.org/posts/commenting-enabled/), but you're
right that that would have been a little more ethical. Still - enabling
Google Analytics didn't feel _too_ bad (since anyone who cared about
detecting and blocking it would still be able to with standard methods -
and I assume that anyone reading my blog will know how to), whereas this
new approach feels like a circumvention of their agency.
2022-05-04 14:45:20 -07:00

86 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# Usage:
# <script> -path <path> [-noGit]
# * <path>, if present, should point to the path to Hugo root
# * -noGit will disable git operation
set -e
# https://stackoverflow.com/a/52156612/1040915
declare -A flags
declare -A booleans
args=()
while [ "$1" ];
do
arg=$1
if [ "${1:0:1}" == "-" ]
then
shift
rev=$(echo "$arg" | rev)
if [ -z "$1" ] || [ "${1:0:1}" == "-" ] || [ "${rev:0:1}" == ":" ]
then
bool=$(echo ${arg:1} | sed s/://g)
booleans[$bool]=true
# echo \"$bool\" is boolean
else
value=$1
flags[${arg:1}]=$value
shift
# echo \"$arg\" is flag with value \"$value\"
fi
else
args+=("$arg")
shift
# echo \"$arg\" is an arg
fi
done
if [ -z ${flags["path"]} ]; then
path="blog";
else
path=${flags["path"]};
fi
# https://stackoverflow.com/a/56841359/1040915
if [[ -z "${booleans['noGit']:-}" ]]; then
# This assumes that the blog content is within the Git repo which contains the script location.
# https://unix.stackexchange.com/a/155077/30828
if [ -n "$(git status --porcelain)" ]; then
echo "Working directory not clean - aborting";
exit
fi
git push
fi
hugo --quiet --source $path
docker_image_tag="scubbo/blog_nginx"
docker_instance_name="blog_nginx"
cp -r $path/public ./builtContent
docker build -t $docker_image_tag . -f-<<EOF
FROM nginxinc/nginx-unprivileged
COPY builtContent /usr/share/nginx/html
EOF
if [[ $(docker ps --filter "name=$docker_instance_name" | wc -l) -lt 2 ]]; then
echo "No currently running blog"
else
docker kill $docker_instance_name
docker rm $docker_instance_name
fi
docker run --name $docker_instance_name -p 8108:8080 \
--mount type=bind,source="$(pwd)"/default.conf,target=/etc/nginx/conf.d/default.conf \
-d $docker_image_tag
# TODO - call Cloudflare's CDN API to explicitly purge cache on the index page
# TODO - (more of a stretch) and parse the `git push` output to purge cache on updated pages, too
# TODO - do the "docker kill and restart" more idiomatically - there must be a "proper" way to do it!
# ...it's probably k8s, isn't it :)
rm -rf builtContent