From e67e0bc453992ef959aa3598323c7d9263889c8b Mon Sep 17 00:00:00 2001 From: Oliver Giles Date: Fri, 30 Jul 2021 19:59:13 +1200 Subject: [PATCH] examples: add git post-receive hook example --- UserManual.md | 2 ++ examples/git-post-receive-hook-notes | 42 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 examples/git-post-receive-hook-notes diff --git a/UserManual.md b/UserManual.md index 062ddf1..aab8c7b 100644 --- a/UserManual.md +++ b/UserManual.md @@ -198,6 +198,8 @@ This is what [git hooks](https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hook LAMINAR_REASON="Push to git repository" laminarc queue example-build ``` +For a more advanced example, see [examples/git-post-receive-hook-notes](https://github.com/ohwgiles/laminar/blob/master/examples/git-post-receive-hook-notes) + What if your git server is not the same machine as the laminar instance? ## Triggering on a remote laminar instance diff --git a/examples/git-post-receive-hook-notes b/examples/git-post-receive-hook-notes new file mode 100755 index 0000000..d4002ef --- /dev/null +++ b/examples/git-post-receive-hook-notes @@ -0,0 +1,42 @@ +#!/bin/bash -e +# Simple post-receive hook that triggers a laminar run +# for every commit pushed to every branch, and annotates +# the commit with the run number using a git note. +# On the cloned repository, useful config is +# git config --add remote.origin.fetch "+refs/notes/*:refs/notes/*" +# to automatically fetch all notes from the origin, and +# git config --add notes.displayRef "refs/notes/*" +# to display all notes in the git log by default + +# The laminar job to trigger +LAMINAR_JOB=my-project + +# Default notes ref is refs/notes/commits +NOTES_REF=refs/notes/ci + +# For each ref pushed... +while read old new ref; do + # Skip tags, notes, etc. Only do heads. + # Extend this to only trigger on specific branches. + if [[ $ref != refs/heads/* ]]; then + continue + fi + # Otherwise, for each new commit in the ref... + # (to only trigger on the newest, set commit=$new and delete the loop) + git rev-list $([[ $old =~ ^0+$ ]] && echo $new || echo $old..$new) | while read commit; do + # Queue the laminar run + run=$(laminarc queue $LAMINAR_JOB commit=$commit ref=$ref) + echo "Started Laminar $run for commit $commit to ref $ref" + + # Add a git note about the run + blob=$(echo -n "Laminar-Run: $run" | git hash-object -w --stdin) + if last_note=$(git show-ref -s $NOTES_REF); then + git read-tree $last_note + p_arg=-p + fi + git update-index --add --cacheinfo 100644 $blob $commit + tree=$(git write-tree) + new_note=$(echo "Notes added by post-receive hook" | git commit-tree $tree $p_arg $last_note) + git update-ref $NOTES_REF $new_note $last_note + done +done