Skip to content

Commit

Permalink
Shard vars file between multiple plugin configurations
Browse files Browse the repository at this point in the history
By hashing the `BUILDKITE_PLUGIN_CONFIGURATION` envvar, we can
effectively disambiguate multiple instances of the `metahook` plugin
within a single pipeline.  This allows us to seprate the `vars` file, as
it otherwise can get confused if you have multiple invocations of the
plugin that each use different hooks.
  • Loading branch information
staticfloat committed Jun 14, 2021
1 parent d3dc683 commit d6b8e0e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions hooks/environment
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
set -euo pipefail

# If users add multiple `metahook` instances, we'll just store our `vars` in the same directory.
# We store the `vars` in a file that is namespaced by a hash of the plugin configuration, which
# allows us to avoid having multiple metahook instances clobber eachother.
if [[ ! -v "BUILDKITE_METAHOOK_HOOKS_PATH" ]]; then
export BUILDKITE_METAHOOK_HOOKS_PATH="$(mktemp -d)"
env | sort | grep "BUILDKITE_PLUGIN_METAHOOK" | uniq >"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"
fi

if grep -E 'BUILDKITE_PLUGIN_METAHOOK_.+(\.BAT|\.CMD)=' "${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then
PLUGIN_CONFIG_HASH=$(cksum <<<"${BUILDKITE_PLUGIN_CONFIGURATION}" | awk '{ print $1 }')
VARS_FILENAME="vars-${PLUGIN_CONFIG_HASH}"
env | sort | grep "BUILDKITE_PLUGIN_METAHOOK" | uniq >"${BUILDKITE_METAHOOK_HOOKS_PATH}/${VARS_FILENAME}"

if grep -E 'BUILDKITE_PLUGIN_METAHOOK_.+(\.BAT|\.CMD)=' "${BUILDKITE_METAHOOK_HOOKS_PATH}/${VARS_FILENAME}"; 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 ""
Expand Down
6 changes: 5 additions & 1 deletion hooks/pre-exit
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail

PLUGIN_CONFIG_HASH=$(cksum <<<"${BUILDKITE_PLUGIN_CONFIGURATION}" | awk '{ print $1 }')
VARS_FILENAME="vars-${PLUGIN_CONFIG_HASH}"
cleanup() {
rm -rf "${BUILDKITE_METAHOOK_HOOKS_PATH}"
# Delete our `vars` directory, then if the directory itself is empty, delete that too
rm -rf "${BUILDKITE_METAHOOK_HOOKS_PATH}/${VARS_FILENAME}"
rmdir "${BUILDKITE_METAHOOK_HOOKS_PATH}" 2>/dev/null || true
}
trap cleanup EXIT

Expand Down
2 changes: 2 additions & 0 deletions hooks/run-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ hook_name="${1:?1st arg needs to be hook name}"
upperd="$(echo "${hook_name}" | tr "[:lower:]" "[:upper:]" | sed "s:-:_:")"
var_name="BUILDKITE_PLUGIN_METAHOOK_${upperd}"

PLUGIN_CONFIG_HASH=$(cksum <<<"${BUILDKITE_PLUGIN_CONFIGURATION}" | awk '{ print $1 }')
VARS_FILENAME="vars-${PLUGIN_CONFIG_HASH}"
if grep -q "${var_name}" <"${BUILDKITE_METAHOOK_HOOKS_PATH}/vars"; then
hook_file="${BUILDKITE_METAHOOK_HOOKS_PATH}/${hook_name}"

Expand Down

0 comments on commit d6b8e0e

Please sign in to comment.