From d52b809c91a8cf2bf2c95d49bee51180470667d1 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sun, 13 May 2018 18:21:52 -0400 Subject: [PATCH] Add a script to help deploy nanoc sites. This git post-receive hook helps automatically build and deploy nanoc sites when pushes are complete. --- nanoc-post-receive | 111 +++++++++++++++++++++++++++++++++++++++++++++ runcron.sh | 24 ++++++++++ runcron.zsh | 24 ---------- 3 files changed, 135 insertions(+), 24 deletions(-) create mode 100755 nanoc-post-receive create mode 100755 runcron.sh delete mode 100755 runcron.zsh diff --git a/nanoc-post-receive b/nanoc-post-receive new file mode 100755 index 0000000..8b3a31a --- /dev/null +++ b/nanoc-post-receive @@ -0,0 +1,111 @@ +#!/bin/sh +# +# Copyright © 2018 Nick Bowler +# +# Simple git post-receive hook to deploy a nanoc site +# +# License WTFPL2: Do What The Fuck You Want To Public License, version 2. +# This is free software: you are free to do what the fuck you want to. +# There is NO WARRANTY, to the extent permitted by law. + +GIT=/usr/bin/git +worktreedir=/var/cache/git-worktrees + +master_update= +while read old new ref x; do + case $ref in + refs/heads/master) master_update=$new ;; + esac +done + +worktree_remove() { + # welp, git doesn't appear to have a way to ONLY remove worktree tracking + # files, it removes the work tree itself which we don't want to do. So do + # this manually. + + for f in worktrees/*/gitdir; do + exec 3<"$f" || continue + read l <&3 || continue + + if test x"$l" = x"$1/.git"; then + rm -f "$f" + $GIT worktree prune -v + break + fi + done + + exec 3<&- +} + +cd_to_worktree_ () { + tmpfile=`mktemp` + exec 3>"$tmpfile" 4<"$tmpfile" + rm -f "$tmpfile" + + branch=${1##*/} + + repodir=`pwd` + reponame=${repodir##*/} + worktree=/no/where + + # Remove any stale worktrees... + $GIT worktree prune -v + + # First just try to add a new worktree + tmp_worktree=`mktemp -d "${worktreedir%/}/$reponame-$branch-XXXXXXXX"` + $GIT worktree add "$tmp_worktree" "$branch" 2>/dev/null || + rmdir "$tmp_worktree" + + $GIT worktree list --porcelain >&3 + exec 3<&- + + while read a b x <&4; do + case $a in + worktree) worktree=${b:-/no/where} ;; + branch) if test x"$b" = x"refs/heads/$branch"; then + if cd "$worktree"; then + test x"`pwd -P`" != x"$worktree" || { exec 4<&-; return 0; } + cd - + fi + + # Clean up + worktree_remove "$worktree" + fi ;; + esac + done + + # Failed? + exec 4<&- + return 1 +} + +# Usage: cd_to_worktree branch +# +# Change the current working directory to the worktree for the specified +# branch, creating it if necessary. +cd_to_worktree () { + # Try twice: stale entries might be removed by first pass and 2nd will work + if cd_to_worktree_ "$@" || cd_to_worktree_ "$@"; then + printf 'using worktree %s\n' "`pwd -P`" + unset GIT_DIR + fi +} + +deploy_branch () { + printf 'Deploying %s\n' "$1" + ( cd_to_worktree $1 + { flock -w 60 9 || exit + git reset --hard || exit + git clean -qfd -e gitlock || exit + nanoc || exit + nanoc deploy master || exit + } 9>gitlock + ) +} + +ret=true +if test ${master_update:+y}; then + deploy_branch master || ret=false +fi + +$ret diff --git a/runcron.sh b/runcron.sh new file mode 100755 index 0000000..f626cd7 --- /dev/null +++ b/runcron.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Copyright © 2011, 2015 Nick Bowler +# +# Simple script to dispatch jobs in /etc/cron.whatever. +# +# License WTFPL2: Do What The Fuck You Want To Public License, version 2. +# This is free software: you are free to do what the fuck you want to. +# There is NO WARRANTY, to the extent permitted by law. + +crondir=/etc/cron.$1 + +if test ! -d "$crondir"; then + printf '%s: argument must be one of:' "$0" + for i in /etc/cron.*; do + if test -d "$i"; then + printf ' %s' ${i#/etc/cron.} + fi + done + printf '.\n' + exit 1 +fi + +run-parts --verbose --regex='^[^.][[:alnum:]_.-]+$' "$crondir" diff --git a/runcron.zsh b/runcron.zsh deleted file mode 100755 index 8248728..0000000 --- a/runcron.zsh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env zsh -# -# Copyright © 2011 Nick Bowler -# -# Simple script to dispatch jobs in /etc/cron.whatever. -# -# License WTFPL2: Do What The Fuck You Want To Public License, version 2. -# This is free software: you are free to do what the fuck you want to. -# There is NO WARRANTY, to the extent permitted by law. - -crondir=/etc/cron.$1 - -if ! [[ -d $crondir ]]; then - printf '%s: argument must be one of:' $0 - for i in /etc/cron.*; do - if ! [[ -d $i ]]; then continue; fi - printf ' %s' ${i#/etc/cron.} - done - echo . - - exit 1 -fi - -run-parts --verbose --regex='^[^.][[:alnum:]_.-]+$' $crondir -- 2.43.0