From 79d8799e6a3f315de94f6b0d39575e47b88fd994 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Sat, 30 May 2020 17:52:05 +0100 Subject: [PATCH 01/15] Support more than one step in a pipeline This fixes the case where you have two steps each with different content in their own metahook for a hook, and the case where there are multiple agents per machine --- .buildkite/local-pipeline.yml | 12 +++++- Makefile | 2 +- contributing.md | 72 +++++++++++++++++++++++++++++++++++ hooks/checkout | 4 ++ hooks/command | 4 ++ hooks/environment | 50 ++++-------------------- hooks/post-artifact | 4 ++ hooks/post-checkout | 4 ++ hooks/post-command | 4 ++ hooks/pre-artifact | 4 ++ hooks/pre-command | 4 ++ hooks/pre-exit | 9 +++++ lib/stdlib.bash | 16 ++++++++ 13 files changed, 143 insertions(+), 46 deletions(-) create mode 100755 hooks/checkout create mode 100755 hooks/command create mode 100755 hooks/post-artifact create mode 100755 hooks/post-checkout create mode 100755 hooks/post-command create mode 100755 hooks/pre-artifact create mode 100755 hooks/pre-command create mode 100755 hooks/pre-exit create mode 100644 lib/stdlib.bash diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index a07bb1c..fc47fae 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -6,7 +6,15 @@ steps: pre-command: | echo "pre-command hook" echo "with two commands" - command.bat: echo "command hook .bat" post-command: echo "post-command hook" - pre-artifact.cmd: echo "pre-artifact hook .cmd" pre-exit: echo "pre-exit hook" + + - label: "Like we did last summer" + command: echo "Hello Plague World" + plugins: + - ./.buildkite/plugins/metahook: + pre-command: | + echo "plague hook" + echo "with two commands" + post-command: echo "post-command summer hook" + pre-exit: echo "pre-exit summer hook" diff --git a/Makefile b/Makefile index 08a8e6f..3285294 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ local_run: vendorize vendorize: rm -rf .buildkite/plugins/metahook mkdir -p .buildkite/plugins/metahook - cp -rf hooks plugin.yml .buildkite/plugins/metahook/ + cp -rf hooks lib plugin.yml .buildkite/plugins/metahook/ diff --git a/contributing.md b/contributing.md index e6c20ba..1562a2c 100644 --- a/contributing.md +++ b/contributing.md @@ -24,3 +24,75 @@ post-command hook $ .buildkite/plugins/metahook/hooks/pre-exit pre-exit hook ``` + +On Windows, the output should contain + +```txt +>>> Starting local agent � +>>> Starting build � +>>> Executing initial command: buildkite-agent pipeline upload ".buildkite/local-pipeline.yml" +>>> Executing command step Test + +~~~ Preparing plugins +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin + +~~~ Running plugin metahook environment hook +> .buildkite\plugins\metahook\hooks\environment +# BUILDKITE_METAHOOK_VARS changed +# BUILDKITE_METAHOOK_HOOKS_PATH changed + +~~~ Running plugin metahook pre-command hook +> .buildkite\plugins\metahook\hooks\pre-command +BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "pre-command hook" +Executing pre-command metahook saved to /tmp/tmp.16gn7V1NYx/pre-command ... +pre-command hook +with two commands + +~~~ Running plugin metahook command hook +> .buildkite\plugins\metahook\hooks\command + +~~~ Running plugin metahook post-command hook +> .buildkite\plugins\metahook\hooks\post-command +BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command hook" +Executing post-command metahook saved to /tmp/tmp.16gn7V1NYx/post-command ... +post-command hook + +~~~ Running plugin metahook pre-exit hook +> .buildkite\plugins\metahook\hooks\pre-exit +BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit hook" +Executing pre-exit metahook saved to /tmp/tmp.16gn7V1NYx/pre-exit ... +pre-exit hook + +>>> Command succeeded in 3.0925061s +>>> Executing command step Like we did last summer + +~~~ Preparing plugins +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin + +~~~ Running plugin metahook environment hook +> .buildkite\plugins\metahook\hooks\environment +# BUILDKITE_METAHOOK_VARS changed +# BUILDKITE_METAHOOK_HOOKS_PATH changed + +~~~ Running plugin metahook pre-command hook +> .buildkite\plugins\metahook\hooks\pre-command +BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "plague hook" +Executing pre-command metahook saved to /tmp/tmp.nRdUtA2FGe/pre-command ... +plague hook +with two commands + +~~~ Running plugin metahook command hook +> .buildkite\plugins\metahook\hooks\command + +~~~ Running plugin metahook post-command hook +> .buildkite\plugins\metahook\hooks\post-command +BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command summer hook" +Executing post-command metahook saved to /tmp/tmp.nRdUtA2FGe/post-command ... +post-command summer hook + +~~~ Running plugin metahook pre-exit hook +> .buildkite\plugins\metahook\hooks\pre-exit +BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit summer hook" +Executing pre-exit metahook saved to /tmp/tmp.nRdUtA2FGe/pre-exit ... +pre-exit summer hook +``` diff --git a/hooks/checkout b/hooks/checkout new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/checkout @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/command b/hooks/command new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/command @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/environment b/hooks/environment index 8b20749..cddeb30 100644 --- a/hooks/environment +++ b/hooks/environment @@ -1,46 +1,10 @@ #!/usr/bin/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 -# The list of possible hooks to implement. -declare -a hooks=( - # "environment" # not allowed - "pre-checkout" - "checkout" - "post-checkout" - "pre-command" - "command" - "post-command" - "pre-artifact" - "post-artifact" - "pre-exit" -) +BUILDKITE_METAHOOK_HOOKS_PATH="$(mktemp -d)" +export BUILDKITE_METAHOOK_HOOKS_PATH +BUILDKITE_METAHOOK_VARS="${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" +export BUILDKITE_METAHOOK_VARS +env | sort | grep "METAHOOK" | uniq >"${BUILDKITE_METAHOOK_VARS}" -# The list of possible file extensions for hooks. -declare -a extensions=( - "" - ".bat" - ".cmd" -) - -hooks_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -vars="$(mktemp)" -cleanup() { - rm -rf "${vars}" -} -trap cleanup EXIT - -env | sort | grep "METAHOOK" | uniq >"${vars}" - -for hook_name in "${hooks[@]}"; do - for ext in "${extensions[@]}"; do - hook_file="${hooks_dir}/${hook_name}${ext}" - rm -f "${hook_file}" - upperd="$(echo "${hook_name}${ext}" | tr "[:lower:]" "[:upper:]")" - var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd//-/_}" - if grep "${var_name}" <"${vars}"; then - # Note: ! syntax here is dynamic variable expansion. That is - # use the var_name string's variable value. - echo "${!var_name}" >"${hook_file}" - fi - done -done +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-artifact b/hooks/post-artifact new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/post-artifact @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-checkout b/hooks/post-checkout new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/post-checkout @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-command b/hooks/post-command new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/post-command @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/pre-artifact b/hooks/pre-artifact new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/pre-artifact @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/pre-command b/hooks/pre-command new file mode 100755 index 0000000..cd6439e --- /dev/null +++ b/hooks/pre-command @@ -0,0 +1,4 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/pre-exit b/hooks/pre-exit new file mode 100755 index 0000000..8d66a75 --- /dev/null +++ b/hooks/pre-exit @@ -0,0 +1,9 @@ +#!/usr/local/env bash +source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 + +cleanup() { + rm -rf "${BUILDKITE_METAHOOK_HOOKS_PATH}" +} +trap cleanup EXIT + +run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" diff --git a/lib/stdlib.bash b/lib/stdlib.bash new file mode 100644 index 0000000..0aff2ee --- /dev/null +++ b/lib/stdlib.bash @@ -0,0 +1,16 @@ +echoerr() { + echo "$@" 1>&2; +} + +run_hook_when_needed() { + hook_name="${1:?1st arg needs to be hook name}" + + upperd="$(echo "${hook_name}" | tr "[:lower:]" "[:upper:]")" + var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd//-/_}" + if grep "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then + hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" + echo "${!var_name}" >"${hook_file}" + chmod +x "${hook_file}" + source "${hook_file}" + fi +} From dbf8bc439cf6946f43666b8a0e093ca833dd8c6f Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Sat, 30 May 2020 17:56:01 +0100 Subject: [PATCH 02/15] unnecessary debugging --- contributing.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/contributing.md b/contributing.md index 1562a2c..3b0d962 100644 --- a/contributing.md +++ b/contributing.md @@ -44,7 +44,6 @@ On Windows, the output should contain ~~~ Running plugin metahook pre-command hook > .buildkite\plugins\metahook\hooks\pre-command BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "pre-command hook" -Executing pre-command metahook saved to /tmp/tmp.16gn7V1NYx/pre-command ... pre-command hook with two commands @@ -54,13 +53,11 @@ with two commands ~~~ Running plugin metahook post-command hook > .buildkite\plugins\metahook\hooks\post-command BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command hook" -Executing post-command metahook saved to /tmp/tmp.16gn7V1NYx/post-command ... post-command hook ~~~ Running plugin metahook pre-exit hook > .buildkite\plugins\metahook\hooks\pre-exit BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit hook" -Executing pre-exit metahook saved to /tmp/tmp.16gn7V1NYx/pre-exit ... pre-exit hook >>> Command succeeded in 3.0925061s @@ -77,7 +74,6 @@ pre-exit hook ~~~ Running plugin metahook pre-command hook > .buildkite\plugins\metahook\hooks\pre-command BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "plague hook" -Executing pre-command metahook saved to /tmp/tmp.nRdUtA2FGe/pre-command ... plague hook with two commands @@ -87,12 +83,10 @@ with two commands ~~~ Running plugin metahook post-command hook > .buildkite\plugins\metahook\hooks\post-command BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command summer hook" -Executing post-command metahook saved to /tmp/tmp.nRdUtA2FGe/post-command ... post-command summer hook ~~~ Running plugin metahook pre-exit hook > .buildkite\plugins\metahook\hooks\pre-exit BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit summer hook" -Executing pre-exit metahook saved to /tmp/tmp.nRdUtA2FGe/pre-exit ... pre-exit summer hook ``` From eea5088456818ffead1c62e515ed0c65c24520bf Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Sat, 30 May 2020 18:04:58 +0100 Subject: [PATCH 03/15] tweaks --- .buildkite/local-pipeline.yml | 4 ++-- contributing.md | 34 +++++++++++++++++++++++----------- lib/stdlib.bash | 2 +- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index fc47fae..2fbe5a1 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -7,7 +7,7 @@ steps: echo "pre-command hook" echo "with two commands" post-command: echo "post-command hook" - pre-exit: echo "pre-exit hook" + pre-exit: echo "pre-exit hook" - label: "Like we did last summer" command: echo "Hello Plague World" @@ -17,4 +17,4 @@ steps: echo "plague hook" echo "with two commands" post-command: echo "post-command summer hook" - pre-exit: echo "pre-exit summer hook" + pre-exit: echo "pre-exit summer hook" diff --git a/contributing.md b/contributing.md index 3b0d962..03889d7 100644 --- a/contributing.md +++ b/contributing.md @@ -4,7 +4,9 @@ make vendorize local_run ``` -On macOS & linux, the output should contain +## Expected output + +### On macOS & linux, the output should contain ```txt ~~~ Running plugin metahook pre-command hook @@ -25,19 +27,17 @@ $ .buildkite/plugins/metahook/hooks/pre-exit pre-exit hook ``` -On Windows, the output should contain +### On Windows, the output should contain ```txt ->>> Starting local agent � ->>> Starting build � ->>> Executing initial command: buildkite-agent pipeline upload ".buildkite/local-pipeline.yml" >>> Executing command step Test ~~~ Preparing plugins -> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin +> cd C:\metahook-buildkite-plugin ~~~ Running plugin metahook environment hook > .buildkite\plugins\metahook\hooks\environment +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin # BUILDKITE_METAHOOK_VARS changed # BUILDKITE_METAHOOK_HOOKS_PATH changed @@ -46,47 +46,59 @@ On Windows, the output should contain BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "pre-command hook" pre-command hook with two commands +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ~~~ Running plugin metahook command hook > .buildkite\plugins\metahook\hooks\command +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ~~~ Running plugin metahook post-command hook > .buildkite\plugins\metahook\hooks\post-command BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command hook" post-command hook +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ~~~ Running plugin metahook pre-exit hook > .buildkite\plugins\metahook\hooks\pre-exit -BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit hook" +BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit hook" pre-exit hook +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ->>> Command succeeded in 3.0925061s +>>> Command succeeded in 3.1585511s >>> Executing command step Like we did last summer ~~~ Preparing plugins -> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin +> cd C:\metahook-buildkite-plugin ~~~ Running plugin metahook environment hook > .buildkite\plugins\metahook\hooks\environment -# BUILDKITE_METAHOOK_VARS changed +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin # BUILDKITE_METAHOOK_HOOKS_PATH changed +# BUILDKITE_METAHOOK_VARS changed ~~~ Running plugin metahook pre-command hook > .buildkite\plugins\metahook\hooks\pre-command BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "plague hook" plague hook with two commands +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ~~~ Running plugin metahook command hook > .buildkite\plugins\metahook\hooks\command +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ~~~ Running plugin metahook post-command hook > .buildkite\plugins\metahook\hooks\post-command BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command summer hook" post-command summer hook +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin ~~~ Running plugin metahook pre-exit hook > .buildkite\plugins\metahook\hooks\pre-exit -BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit summer hook" +BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit summer hook" pre-exit summer hook +> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin + +>>> Command succeeded in 5.0973937s +>>> Build finished in 5.1013827s ``` diff --git a/lib/stdlib.bash b/lib/stdlib.bash index 0aff2ee..c5673c3 100644 --- a/lib/stdlib.bash +++ b/lib/stdlib.bash @@ -11,6 +11,6 @@ run_hook_when_needed() { hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" echo "${!var_name}" >"${hook_file}" chmod +x "${hook_file}" - source "${hook_file}" + "${hook_file}" fi } From ef29222114d858c301807041e78da7d5ef225377 Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Sat, 30 May 2020 20:26:39 +0100 Subject: [PATCH 04/15] feedback --- .buildkite/local-pipeline.yml | 10 ---------- Makefile | 2 +- hooks/checkout | 6 ++---- hooks/command | 6 ++---- hooks/environment | 5 ++--- hooks/post-artifact | 6 ++---- hooks/post-checkout | 6 ++---- hooks/post-command | 6 ++---- hooks/pre-artifact | 6 ++---- hooks/pre-command | 6 ++---- hooks/pre-exit | 6 ++---- hooks/run-hook.sh | 12 ++++++++++++ lib/stdlib.bash | 16 ---------------- 13 files changed, 31 insertions(+), 62 deletions(-) create mode 100755 hooks/run-hook.sh delete mode 100644 lib/stdlib.bash diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index 2fbe5a1..59385bc 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -8,13 +8,3 @@ steps: echo "with two commands" post-command: echo "post-command hook" pre-exit: echo "pre-exit hook" - - - label: "Like we did last summer" - command: echo "Hello Plague World" - plugins: - - ./.buildkite/plugins/metahook: - pre-command: | - echo "plague hook" - echo "with two commands" - post-command: echo "post-command summer hook" - pre-exit: echo "pre-exit summer hook" diff --git a/Makefile b/Makefile index 3285294..08a8e6f 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ local_run: vendorize vendorize: rm -rf .buildkite/plugins/metahook mkdir -p .buildkite/plugins/metahook - cp -rf hooks lib plugin.yml .buildkite/plugins/metahook/ + cp -rf hooks plugin.yml .buildkite/plugins/metahook/ diff --git a/hooks/checkout b/hooks/checkout index cd6439e..9cac5c4 100755 --- a/hooks/checkout +++ b/hooks/checkout @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/command b/hooks/command index cd6439e..9cac5c4 100755 --- a/hooks/command +++ b/hooks/command @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/environment b/hooks/environment index cddeb30..1d67b5f 100644 --- a/hooks/environment +++ b/hooks/environment @@ -1,10 +1,9 @@ #!/usr/bin/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - BUILDKITE_METAHOOK_HOOKS_PATH="$(mktemp -d)" export BUILDKITE_METAHOOK_HOOKS_PATH BUILDKITE_METAHOOK_VARS="${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" export BUILDKITE_METAHOOK_VARS env | sort | grep "METAHOOK" | uniq >"${BUILDKITE_METAHOOK_VARS}" -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" + +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-artifact b/hooks/post-artifact index cd6439e..51a0717 100755 --- a/hooks/post-artifact +++ b/hooks/post-artifact @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file diff --git a/hooks/post-checkout b/hooks/post-checkout index cd6439e..51a0717 100755 --- a/hooks/post-checkout +++ b/hooks/post-checkout @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file diff --git a/hooks/post-command b/hooks/post-command index cd6439e..51a0717 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file diff --git a/hooks/pre-artifact b/hooks/pre-artifact index cd6439e..51a0717 100755 --- a/hooks/pre-artifact +++ b/hooks/pre-artifact @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file diff --git a/hooks/pre-command b/hooks/pre-command index cd6439e..51a0717 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -1,4 +1,2 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +#!/usr/bin/env bash +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file diff --git a/hooks/pre-exit b/hooks/pre-exit index 8d66a75..fa340c3 100755 --- a/hooks/pre-exit +++ b/hooks/pre-exit @@ -1,9 +1,7 @@ -#!/usr/local/env bash -source "$(dirname "${BASH_SOURCE[0]}")/../lib/stdlib.bash" || exit 67 - +#!/usr/bin/env bash cleanup() { rm -rf "${BUILDKITE_METAHOOK_HOOKS_PATH}" } trap cleanup EXIT -run_hook_when_needed "$(basename "${BASH_SOURCE[0]}")" +"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh new file mode 100755 index 0000000..b758080 --- /dev/null +++ b/hooks/run-hook.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +hook_name="${1:?1st arg needs to be hook name}" + +upperd="$(echo "${hook_name}" | tr "[:lower:]" "[:upper:]")" +var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd//-/_}" +if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then + hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" + echo "${!var_name}" >"${hook_file}" + chmod +x "${hook_file}" + "${hook_file}" +fi diff --git a/lib/stdlib.bash b/lib/stdlib.bash deleted file mode 100644 index c5673c3..0000000 --- a/lib/stdlib.bash +++ /dev/null @@ -1,16 +0,0 @@ -echoerr() { - echo "$@" 1>&2; -} - -run_hook_when_needed() { - hook_name="${1:?1st arg needs to be hook name}" - - upperd="$(echo "${hook_name}" | tr "[:lower:]" "[:upper:]")" - var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd//-/_}" - if grep "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then - hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" - echo "${!var_name}" >"${hook_file}" - chmod +x "${hook_file}" - "${hook_file}" - fi -} From fbc750f82df4dbeda6632caf4a5049f272988ca0 Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Mon, 1 Jun 2020 11:39:36 +0100 Subject: [PATCH 05/15] win --- hooks/run-hook.sh | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh index b758080..5494208 100755 --- a/hooks/run-hook.sh +++ b/hooks/run-hook.sh @@ -2,11 +2,27 @@ hook_name="${1:?1st arg needs to be hook name}" -upperd="$(echo "${hook_name}" | tr "[:lower:]" "[:upper:]")" -var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd//-/_}" -if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then - hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" - echo "${!var_name}" >"${hook_file}" - chmod +x "${hook_file}" - "${hook_file}" -fi +# The list of possible file extensions for hooks. +declare -a extensions=( + "" + ".bat" + ".cmd" +) + +for ext in "${extensions[@]}"; do + upperd="$(echo "${hook_name}${ext}" | tr "[:lower:]" "[:upper:]" | tr .- _)" + var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" + + if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then + cat ${BUILDKITE_METAHOOK_VARS} + + hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}${ext}" + echo "${!var_name}" >"${hook_file}" + chmod +x "${hook_file}" + if [[ ${ext} == "" ]]; then + "${hook_file}" + else + cmd.exe /c "${hook_file}" # .bat or .cmd + fi + fi +done From abbec2f3eb85f3b09ce3c8ed6c7d874b87c9dfb8 Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Mon, 1 Jun 2020 11:53:32 +0100 Subject: [PATCH 06/15] stray cat --- hooks/run-hook.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh index 5494208..16e1580 100755 --- a/hooks/run-hook.sh +++ b/hooks/run-hook.sh @@ -14,8 +14,6 @@ for ext in "${extensions[@]}"; do var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then - cat ${BUILDKITE_METAHOOK_VARS} - hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}${ext}" echo "${!var_name}" >"${hook_file}" chmod +x "${hook_file}" From 4ac00fa1871551b67bcd21e74aa1e97c44c1f0ee Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Mon, 1 Jun 2020 21:12:48 +0100 Subject: [PATCH 07/15] Simplify by removing a variable, check on macOS --- .buildkite/local-pipeline.yml | 4 +++- contributing.md | 9 ++++++--- hooks/environment | 5 +---- hooks/run-hook.sh | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index 59385bc..a07bb1c 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -6,5 +6,7 @@ steps: pre-command: | echo "pre-command hook" echo "with two commands" + command.bat: echo "command hook .bat" post-command: echo "post-command hook" - pre-exit: echo "pre-exit hook" + pre-artifact.cmd: echo "pre-artifact hook .cmd" + pre-exit: echo "pre-exit hook" diff --git a/contributing.md b/contributing.md index 03889d7..8a6e80f 100644 --- a/contributing.md +++ b/contributing.md @@ -9,14 +9,17 @@ make vendorize local_run ### On macOS & linux, the output should contain ```txt +~~~ Running plugin metahook environment hook +$ .buildkite/plugins/metahook/hooks/environment +# BUILDKITE_METAHOOK_HOOKS_PATH changed + ~~~ Running plugin metahook pre-command hook $ .buildkite/plugins/metahook/hooks/pre-command pre-command hook with two commands -~~~ Running commands -$ trap 'kill -- $$' INT TERM QUIT; echo "Hello World" -Hello World +~~~ Running plugin metahook command hook +$ .buildkite/plugins/metahook/hooks/command ~~~ Running plugin metahook post-command hook $ .buildkite/plugins/metahook/hooks/post-command diff --git a/hooks/environment b/hooks/environment index 1d67b5f..2403c47 100644 --- a/hooks/environment +++ b/hooks/environment @@ -1,9 +1,6 @@ #!/usr/bin/env bash BUILDKITE_METAHOOK_HOOKS_PATH="$(mktemp -d)" export BUILDKITE_METAHOOK_HOOKS_PATH -BUILDKITE_METAHOOK_VARS="${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" -export BUILDKITE_METAHOOK_VARS -env | sort | grep "METAHOOK" | uniq >"${BUILDKITE_METAHOOK_VARS}" - +env | sort | grep "METAHOOK" | uniq >"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" "${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh index 16e1580..f525c3e 100755 --- a/hooks/run-hook.sh +++ b/hooks/run-hook.sh @@ -13,7 +13,7 @@ for ext in "${extensions[@]}"; do upperd="$(echo "${hook_name}${ext}" | tr "[:lower:]" "[:upper:]" | tr .- _)" var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" - if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_VARS}"; then + if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}${ext}" echo "${!var_name}" >"${hook_file}" chmod +x "${hook_file}" From 42c999e4dc6d58758a48015d4943ec0264dac8e2 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 00:34:39 +0100 Subject: [PATCH 08/15] tweak --- hooks/environment | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/environment b/hooks/environment index 2403c47..bea8e88 100644 --- a/hooks/environment +++ b/hooks/environment @@ -1,6 +1,6 @@ #!/usr/bin/env bash BUILDKITE_METAHOOK_HOOKS_PATH="$(mktemp -d)" export BUILDKITE_METAHOOK_HOOKS_PATH -env | sort | grep "METAHOOK" | uniq >"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" +env | sort | grep "BUILDKITE_PLUGIN_METAHOOK" | uniq >"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" From afed30d370502479b15c953479c49eea450bd327 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 09:57:22 +0100 Subject: [PATCH 09/15] whitespace --- .buildkite/local-pipeline.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index a07bb1c..2097e89 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -9,4 +9,4 @@ steps: command.bat: echo "command hook .bat" post-command: echo "post-command hook" pre-artifact.cmd: echo "pre-artifact hook .cmd" - pre-exit: echo "pre-exit hook" + pre-exit: echo "pre-exit hook" From 5afb050a8787e8113c094560a77e8540ca79ea65 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 12:05:03 +0100 Subject: [PATCH 10/15] almost there --- .buildkite/local-pipeline.yml | 4 +-- changelog.md | 25 ++++++++++++++++ contributing.md | 55 +++++------------------------------ hooks/checkout | 3 +- hooks/command | 3 +- hooks/environment | 9 ++++++ hooks/post-artifact | 3 +- hooks/post-checkout | 3 +- hooks/post-command | 3 +- hooks/pre-artifact | 3 +- hooks/pre-command | 3 +- hooks/pre-exit | 4 ++- hooks/run-hook.sh | 30 ++++++------------- 13 files changed, 70 insertions(+), 78 deletions(-) create mode 100644 changelog.md diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index 2097e89..10438e3 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -6,7 +6,7 @@ steps: pre-command: | echo "pre-command hook" echo "with two commands" - command.bat: echo "command hook .bat" + # command.bat: echo "command hook .bat" post-command: echo "post-command hook" - pre-artifact.cmd: echo "pre-artifact hook .cmd" + # pre-artifact.cmd: echo "pre-artifact hook .cmd" pre-exit: echo "pre-exit hook" diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..13b1504 --- /dev/null +++ b/changelog.md @@ -0,0 +1,25 @@ +# Changelog + +## 0.4.0 + +### Fixed + +- Allow steps + +### Removed + +- **Breaking change** Windows Batch (`.bat`, `.cmd`) support. + Sorry if this impacts you. + + To transition from a Windows Batch metahook like + + ```yaml + - label: my-step + command: echo "hello world" + plugins: + - improbable-eng/metahook: + post-checkout.bat: scripts/windows-setup.bat + post-artifact.bat: | + call some-batch-file.bat + echo "Step finished!" + ``` diff --git a/contributing.md b/contributing.md index 8a6e80f..525571f 100644 --- a/contributing.md +++ b/contributing.md @@ -36,72 +36,33 @@ pre-exit hook >>> Executing command step Test ~~~ Preparing plugins -> cd C:\metahook-buildkite-plugin +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin ~~~ Running plugin metahook environment hook > .buildkite\plugins\metahook\hooks\environment -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin -# BUILDKITE_METAHOOK_VARS changed +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin\c\Users\Pete\src\improbable\metahook-buildkite-plugin # BUILDKITE_METAHOOK_HOOKS_PATH changed ~~~ Running plugin metahook pre-command hook > .buildkite\plugins\metahook\hooks\pre-command -BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "pre-command hook" pre-command hook with two commands -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin\c\Users\Pete\src\improbable\metahook-buildkite-plugin ~~~ Running plugin metahook command hook > .buildkite\plugins\metahook\hooks\command -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin\c\Users\Pete\src\improbable\metahook-buildkite-plugin ~~~ Running plugin metahook post-command hook > .buildkite\plugins\metahook\hooks\post-command -BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command hook" post-command hook -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin\c\Users\Pete\src\improbable\metahook-buildkite-plugin ~~~ Running plugin metahook pre-exit hook > .buildkite\plugins\metahook\hooks\pre-exit -BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit hook" pre-exit hook -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin +> cd C:\Users\Pete\src\improbable\metahook-buildkite-plugin\c\Users\Pete\src\improbable\metahook-buildkite-plugin ->>> Command succeeded in 3.1585511s ->>> Executing command step Like we did last summer - -~~~ Preparing plugins -> cd C:\metahook-buildkite-plugin - -~~~ Running plugin metahook environment hook -> .buildkite\plugins\metahook\hooks\environment -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin -# BUILDKITE_METAHOOK_HOOKS_PATH changed -# BUILDKITE_METAHOOK_VARS changed - -~~~ Running plugin metahook pre-command hook -> .buildkite\plugins\metahook\hooks\pre-command -BUILDKITE_PLUGIN_METAHOOK_PRE_COMMAND=echo "plague hook" -plague hook -with two commands -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin - -~~~ Running plugin metahook command hook -> .buildkite\plugins\metahook\hooks\command -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin - -~~~ Running plugin metahook post-command hook -> .buildkite\plugins\metahook\hooks\post-command -BUILDKITE_PLUGIN_METAHOOK_POST_COMMAND=echo "post-command summer hook" -post-command summer hook -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin - -~~~ Running plugin metahook pre-exit hook -> .buildkite\plugins\metahook\hooks\pre-exit -BUILDKITE_PLUGIN_METAHOOK_PRE_EXIT=echo "pre-exit summer hook" -pre-exit summer hook -> cd C:\metahook-buildkite-plugin\c\metahook-buildkite-plugin - ->>> Command succeeded in 5.0973937s ->>> Build finished in 5.1013827s +>>> Command succeeded in 3.2662428s +>>> Build finished in 3.2672403s ``` diff --git a/hooks/checkout b/hooks/checkout index 9cac5c4..699c30f 100755 --- a/hooks/checkout +++ b/hooks/checkout @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/command b/hooks/command index 9cac5c4..699c30f 100755 --- a/hooks/command +++ b/hooks/command @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/environment b/hooks/environment index bea8e88..8542e59 100644 --- a/hooks/environment +++ b/hooks/environment @@ -1,6 +1,15 @@ #!/usr/bin/env bash +set -euo pipefail + BUILDKITE_METAHOOK_HOOKS_PATH="$(mktemp -d)" export BUILDKITE_METAHOOK_HOOKS_PATH env | sort | grep "BUILDKITE_PLUGIN_METAHOOK" | uniq >"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars" +if grep -E 'BUILDKITE_PLUGIN_METAHOOK_.+(\.BAT|\.CMD)=' "${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then + echo "Sorry, we had to remove Windows Batch file support in 0.4.0." + echo "Please refer to https://github.com/improbable-eng/metahook-buildkite-plugin/tree/master/changelog.md#0.4.0" + echo "" + exit 1 +fi + "${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-artifact b/hooks/post-artifact index 51a0717..699c30f 100755 --- a/hooks/post-artifact +++ b/hooks/post-artifact @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-checkout b/hooks/post-checkout index 51a0717..699c30f 100755 --- a/hooks/post-checkout +++ b/hooks/post-checkout @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/post-command b/hooks/post-command index 51a0717..699c30f 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/pre-artifact b/hooks/pre-artifact index 51a0717..699c30f 100755 --- a/hooks/pre-artifact +++ b/hooks/pre-artifact @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/pre-command b/hooks/pre-command index 51a0717..699c30f 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -1,2 +1,3 @@ #!/usr/bin/env bash -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" \ No newline at end of file +set -euo pipefail +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/pre-exit b/hooks/pre-exit index fa340c3..43d28cf 100755 --- a/hooks/pre-exit +++ b/hooks/pre-exit @@ -1,7 +1,9 @@ #!/usr/bin/env bash +set -euo pipefail + cleanup() { rm -rf "${BUILDKITE_METAHOOK_HOOKS_PATH}" } trap cleanup EXIT -"${BASH_SOURCE%/*}"/run-hook.sh "$(basename "${BASH_SOURCE[0]}")" +"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh index f525c3e..1951a82 100755 --- a/hooks/run-hook.sh +++ b/hooks/run-hook.sh @@ -1,26 +1,14 @@ #!/usr/bin/env bash - +set -euo pipefail hook_name="${1:?1st arg needs to be hook name}" -# The list of possible file extensions for hooks. -declare -a extensions=( - "" - ".bat" - ".cmd" -) +upperd="$(echo "${hook_name}" | tr "[:lower:]" "[:upper:]" | sed "s:-:_:")" +var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" -for ext in "${extensions[@]}"; do - upperd="$(echo "${hook_name}${ext}" | tr "[:lower:]" "[:upper:]" | tr .- _)" - var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" +if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then + hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" - if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then - hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}${ext}" - echo "${!var_name}" >"${hook_file}" - chmod +x "${hook_file}" - if [[ ${ext} == "" ]]; then - "${hook_file}" - else - cmd.exe /c "${hook_file}" # .bat or .cmd - fi - fi -done + echo "${!var_name}" >"${hook_file}" + chmod +x "${hook_file}" + "${hook_file}" +fi From 468fce0871f6ba4b1880c95d9901ef7c4492d423 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 12:30:51 +0100 Subject: [PATCH 11/15] changelog and porting guide --- changelog.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 13b1504..9f01f66 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,8 @@ ### Removed -- **Breaking change** Windows Batch (`.bat`, `.cmd`) support. +- **Breaking change** Removed Windows Batch (`.bat`, `.cmd`) support. + Sorry if this impacts you. To transition from a Windows Batch metahook like @@ -17,9 +18,24 @@ - label: my-step command: echo "hello world" plugins: - - improbable-eng/metahook: + - improbable-eng/metahook#0.3.0: post-checkout.bat: scripts/windows-setup.bat post-artifact.bat: | call some-batch-file.bat echo "Step finished!" ``` + + to + + ```yaml + - label: my-step + command: echo "hello world" + plugins: + - improbable-eng/metahook#0.4.0: + post-checkout: scripts/windows-setup.sh + post-artifact: | + some-bash-script-that-was-the-batch-file.sh + echo "Step finished!" + ``` + + To transition Windows Batch shell scripts to bash shell scripts [here is a porting guide](https://www.tldp.org/LDP/abs/html/dosbatch.html). From b68a4a46662f4df1b6f3298d7ebf929bbd7ea1e8 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 12:40:28 +0100 Subject: [PATCH 12/15] no longer needed --- .buildkite/local-pipeline.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.buildkite/local-pipeline.yml b/.buildkite/local-pipeline.yml index 10438e3..59385bc 100644 --- a/.buildkite/local-pipeline.yml +++ b/.buildkite/local-pipeline.yml @@ -6,7 +6,5 @@ steps: pre-command: | echo "pre-command hook" echo "with two commands" - # command.bat: echo "command hook .bat" post-command: echo "post-command hook" - # pre-artifact.cmd: echo "pre-artifact hook .cmd" pre-exit: echo "pre-exit hook" From 64db60ad043305c48e6a0f8712b2af575da0440e Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 12:42:23 +0100 Subject: [PATCH 13/15] restore comment --- hooks/run-hook.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh index 1951a82..2c9736d 100755 --- a/hooks/run-hook.sh +++ b/hooks/run-hook.sh @@ -8,6 +8,8 @@ var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" + # Exclamation syntax here is dynamic variable expansion. + # That is, use the var_name string to look up the value. echo "${!var_name}" >"${hook_file}" chmod +x "${hook_file}" "${hook_file}" From 25bcaa6471ed708391efc2271de99895a1543465 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 15:16:35 +0100 Subject: [PATCH 14/15] kill command --- changelog.md | 6 ++++++ hooks/command | 3 --- 2 files changed, 6 insertions(+), 3 deletions(-) delete mode 100755 hooks/command diff --git a/changelog.md b/changelog.md index 9f01f66..1b099a1 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,12 @@ ### Removed +- **Breaking change** Removed support for `command` metahook. + + Buildkite special-cases this which triggers edge-cases that a `command` metahook would need to work around. + + If you want to override behaviour here, you will need to modify the automation you're running rather than inject a difference via metahook. + - **Breaking change** Removed Windows Batch (`.bat`, `.cmd`) support. Sorry if this impacts you. diff --git a/hooks/command b/hooks/command deleted file mode 100755 index 699c30f..0000000 --- a/hooks/command +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -"${BASH_SOURCE%/*}/run-hook.sh" "$(basename "${BASH_SOURCE[0]}")" From bb69f704337ee4f42e31a6a244bfbc1903370933 Mon Sep 17 00:00:00 2001 From: Peter Mounce Date: Tue, 2 Jun 2020 15:36:45 +0100 Subject: [PATCH 15/15] bash strict mode + exec --- changelog.md | 10 +++++++++- hooks/run-hook.sh | 8 ++++++-- readme.md | 13 ++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index 1b099a1..77e3848 100644 --- a/changelog.md +++ b/changelog.md @@ -4,7 +4,15 @@ ### Fixed -- Allow steps +- Allow multiple steps to use metahooks for the same hook but with different content. +- Inject `bash` shebang and Bash Strict Mode (`set -euo pipefail`) so that if metahooks fail with errors the job halts rather than continuing. + + Bash Strict Mode is provided by + - `set -o errexit` - halt on error + - `set -o nounset` - halt if variable is unset + - `set -o pipefail` - halt if a command inside a pipe fails + + You can override those choices by setting different values (e.g. `set +o nounset`) within your metahook. ### Removed diff --git a/hooks/run-hook.sh b/hooks/run-hook.sh index 2c9736d..2857e8f 100755 --- a/hooks/run-hook.sh +++ b/hooks/run-hook.sh @@ -8,9 +8,13 @@ var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}" if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}" + echo "#\!/usr/bin/env bash" >"${hook_file}" + echo "set -o errexit" >>"${hook_file}" + echo "set -o nounset" >>"${hook_file}" + echo "set -o pipefail" >>"${hook_file}" # Exclamation syntax here is dynamic variable expansion. # That is, use the var_name string to look up the value. - echo "${!var_name}" >"${hook_file}" + echo "${!var_name}" >>"${hook_file}" chmod +x "${hook_file}" - "${hook_file}" + exec "${hook_file}" fi diff --git a/readme.md b/readme.md index eddf8cc..5188007 100644 --- a/readme.md +++ b/readme.md @@ -9,7 +9,6 @@ common: &common plugins: - improbable-eng/metahook: post-checkout: scripts/setup.sh - post-checkout.bat: scripts/windows-setup.bat pre-exit: | scripts/cleanup.sh echo "Step finished!" @@ -25,6 +24,18 @@ steps: <<: *common ``` +Metahooks execute via a `bash` script, searching your `PATH` for `bash`. + +Metahooks execute in Bash Strict Mode. + +Bash Strict Mode is provided by + +- `set -o errexit` - halt on error +- `set -o nounset` - halt if variable is unset +- `set -o pipefail` - halt if a command inside a pipe fails + +You can override those choices by setting different values (e.g. `set +o nounset`) within your metahook. + ## Contributing See [contributing](contributing.md)