Skip to content

Commit

Permalink
bash strict mode + exec
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Mounce committed Jun 2, 2020
1 parent 25bcaa6 commit bb69f70
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
10 changes: 9 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions hooks/run-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand All @@ -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)

0 comments on commit bb69f70

Please sign in to comment.