Skip to content

Commit

Permalink
Do not upload an empty tarball (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
paymand authored Jun 2, 2023
1 parent 528cad0 commit 05de2f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ function s3Exists {
function s3Upload {
local s3_path
local localPaths
local tempFile
s3_path=$(s3Path "$1")
# shellcheck disable=SC2206
localPaths=($2)
tempFile=$(makeTempFile)
set +e
# shellcheck disable=SC2068
if ! (tar --ignore-failed-read -cz ${localPaths[@]} | aws "${aws_cli_args[@]}" s3 cp - "$s3_path"); then
if ! (tar -czf "$tempFile" ${localPaths[@]} && aws "${aws_cli_args[@]}" s3 cp "$tempFile" "$s3_path" --quiet); then
echo "false"
else
echo "true"
Expand Down
20 changes: 20 additions & 0 deletions tests/functions.bats
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,24 @@ setup() {
export BUILDKITE_PLUGIN_S3_CACHE_PIPELINE_NAME="another-pipeline"
run -0 s3ObjectKey file
assert_output "my-org/another-pipeline/file.tar.gz"
unset BUILDKITE_PLUGIN_S3_CACHE_PIPELINE_NAME
}

@test "s3Upload with existing and non-existing paths" {
makeTempFile() { echo /tmp/file.tar.gz; }
export -f makeTempFile
stub aws \
"s3 cp /tmp/file.tar.gz s3://bucket/my-org/my-pipeline/s3_path.tar.gz --quiet : :"
stub tar \
"-czf /tmp/file.tar.gz local_paths : :" \
"-czf /tmp/file.tar.gz local_paths : exit 1"

run -0 s3Upload s3_path local_paths
assert_output "true"
run -0 s3Upload s3_path local_paths
assert_output "false"

unset makeTempFile
unstub aws
unstub tar
}
4 changes: 2 additions & 2 deletions tests/post-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function cleanup {
trap cleanup EXIT

setup() {
bats_require_minimum_version 1.5.0
export BUILDKITE_BUILD_CHECKOUT_PATH=$tmp_dir
export BUILDKITE_BUILD_ID=1
export BUILDKITE_JOB_ID=0
Expand All @@ -21,6 +22,5 @@ setup() {

@test "Post-command succeeds" {
export BUILDKITE_PLUGIN_S3_CACHE_SAVE_0_KEY=v1-node-modules
run "$post_command_hook"
assert_success
run -0 "$post_command_hook"
}
5 changes: 2 additions & 3 deletions tests/pre-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function cleanup {
trap cleanup EXIT

setup() {
bats_require_minimum_version 1.5.0
export BUILDKITE_BUILD_CHECKOUT_PATH=$tmp_dir
export BUILDKITE_BUILD_ID=1
export BUILDKITE_JOB_ID=0
Expand All @@ -35,7 +36,6 @@ function teardown() {

run "$pre_command_hook"

assert_success
assert_output --partial "Successfully restored v1-cache-key"
}

Expand All @@ -50,9 +50,8 @@ function teardown() {
"-xz : echo true" \
"-xz : echo true"

run "$pre_command_hook"
run -0 "$pre_command_hook"

assert_success
assert_output --partial "Failed to restore cache-key-missing"
assert_output --partial "Successfully restored cache-key-exists"
}

0 comments on commit 05de2f9

Please sign in to comment.