Skip to content

Commit

Permalink
Merge pull request #84 from amancevice/abort-post-command
Browse files Browse the repository at this point in the history
abort post-command on non-zero exit status
  • Loading branch information
pzeballos authored Oct 22, 2024
2 parents b2e2172 + 5b65ca6 commit a540f5d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hooks/post-command
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/bin/bash
set -euo pipefail

# skip caching if command exited non-zero
if [ "${BUILDKITE_COMMAND_EXIT_STATUS}" -ne 0 ] ; then
echo "Aborting cache post-command hook because command exited with status $BUILDKITE_COMMAND_EXIT_STATUS"
exit 0
fi

DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"

# shellcheck source=lib/shared.bash
Expand Down
2 changes: 2 additions & 0 deletions tests/post-command-tgz.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
setup() {
load "${BATS_PLUGIN_PATH}/load.bash"

export BUILDKITE_COMMAND_EXIT_STATUS=0

mkdir -p tests/data/my_files
echo "all the llamas" > "tests/data/my_files/llamas.txt"
echo "no alpacas" > "tests/data/my_files/alpacas.txt"
Expand Down
2 changes: 2 additions & 0 deletions tests/post-command-zip.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
setup() {
load "${BATS_PLUGIN_PATH}/load.bash"

export BUILDKITE_COMMAND_EXIT_STATUS=0

mkdir -p tests/data/my_files
echo "all the llamas" > "tests/data/my_files/llamas.txt"
echo "no alpacas" > "tests/data/my_files/alpacas.txt"
Expand Down
11 changes: 11 additions & 0 deletions tests/post-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
setup() {
load "${BATS_PLUGIN_PATH}/load.bash"

export BUILDKITE_COMMAND_EXIT_STATUS=0

mkdir -p tests/data/my_files
echo "all the llamas" > "tests/data/my_files/llamas.txt"
echo "no alpacas" > "tests/data/my_files/alpacas.txt"
Expand Down Expand Up @@ -34,6 +36,15 @@ teardown() {
assert_output --partial 'Cache not setup for saving'
}

@test 'If command failed, do nothing' {
export BUILDKITE_COMMAND_EXIT_STATUS=127

run "$PWD/hooks/post-command"

assert_success
assert_output --partial 'Aborting cache post-command hook because command exited with status 127'
}

@test "Missing path fails" {
unset BUILDKITE_PLUGIN_CACHE_PATH

Expand Down

0 comments on commit a540f5d

Please sign in to comment.