Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not upload an empty tarball #11

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}