From c8287ae9b802757b637d9384878d41f2f3aa2548 Mon Sep 17 00:00:00 2001 From: Alexander Mancevice Date: Tue, 15 Oct 2024 16:48:42 -0400 Subject: [PATCH 1/5] abort post-command on non-zero exit status resolves #80 if the command (eg, bundle install) fails partway through, the cache plugin could upload a poisoned cache --- hooks/post-command | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hooks/post-command b/hooks/post-command index 11a23cd..c8eec9c 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 1 +fi + DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" # shellcheck source=lib/shared.bash From 513c7956508ccaef023513541ff1e008e54d8d91 Mon Sep 17 00:00:00 2001 From: Alexander Mancevice Date: Thu, 17 Oct 2024 14:54:33 -0400 Subject: [PATCH 2/5] Update hooks/post-command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matías Bellone --- hooks/post-command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/post-command b/hooks/post-command index c8eec9c..869724b 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -2,7 +2,7 @@ set -euo pipefail # skip caching if command exited non-zero -if [ $BUILDKITE_COMMAND_EXIT_STATUS -ne 0 ] ; then +if [ "${BUILDKITE_COMMAND_EXIT_STATUS}" -ne 0 ] ; then echo "Aborting cache post-command hook because command exited with status $BUILDKITE_COMMAND_EXIT_STATUS" exit 1 fi From 5c6888f4f7c81c5cac28164b2bdabcc4248d481c Mon Sep 17 00:00:00 2001 From: Alexander Mancevice Date: Thu, 17 Oct 2024 14:55:51 -0400 Subject: [PATCH 3/5] exit 0 instead of 1 when aborting post-command --- hooks/post-command | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/post-command b/hooks/post-command index 869724b..8bfe19a 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -4,7 +4,7 @@ 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 1 + exit 0 fi DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" From 8932460026646016ccc86f538f0f2aea512e3f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 21 Oct 2024 15:56:41 -0300 Subject: [PATCH 4/5] Added missing variable to test setups --- tests/post-command-tgz.bats | 2 ++ tests/post-command-zip.bats | 2 ++ tests/post-command.bats | 2 ++ 3 files changed, 6 insertions(+) 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..a56f026 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" From 5b65ca63de11226fb9f18fa7cd74dacc099ccb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 21 Oct 2024 15:56:54 -0300 Subject: [PATCH 5/5] Added new test for when command fails --- tests/post-command.bats | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/post-command.bats b/tests/post-command.bats index a56f026..efcf4d9 100644 --- a/tests/post-command.bats +++ b/tests/post-command.bats @@ -36,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