diff --git a/hooks/post-command b/hooks/post-command index e405107..b125cf5 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -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 diff --git a/tests/post-command-tgz.bats b/tests/post-command-tgz.bats index 102bc65..1cfcc31 100644 --- a/tests/post-command-tgz.bats +++ b/tests/post-command-tgz.bats @@ -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" diff --git a/tests/post-command-zip.bats b/tests/post-command-zip.bats index 7964ea3..6421f7d 100644 --- a/tests/post-command-zip.bats +++ b/tests/post-command-zip.bats @@ -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" diff --git a/tests/post-command.bats b/tests/post-command.bats index 41e1b11..efcf4d9 100644 --- a/tests/post-command.bats +++ b/tests/post-command.bats @@ -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" @@ -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