From 942c967e35f4f53270e80bf84cfd6f39cf461614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 13:03:07 -0300 Subject: [PATCH 01/12] Added ignore-missing option to plugin.yml --- plugin.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin.yml b/plugin.yml index 3d50c75..d14edf7 100644 --- a/plugin.yml +++ b/plugin.yml @@ -47,6 +47,8 @@ configuration: compressed: type: string pattern: '.*(.zip|.tgz)$' + ignore-missing: + type: boolean oneOf: - required: - upload From bc0f965d952ffd546ff64f45ec72affb6459839c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 13:03:28 -0300 Subject: [PATCH 02/12] Added implementation of ignore-missing --- hooks/post-command | 16 +++++++++++++--- hooks/pre-command | 18 ++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/hooks/post-command b/hooks/post-command index fb5628e..64e0055 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -60,6 +60,16 @@ workdir=${BUILDKITE_PLUGIN_ARTIFACTS_WORKDIR:-.} pushd "${workdir}" trap popd EXIT +bk_agent() { + if ! buildkite-agent artifact "${@}"; then + if [[ "${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"}" != "false" ]]; then + echo "Ignoring error in upload" + else + echo "Error in upload" + exit 1 + fi + fi +} # Set user-provided object ACL for AWS S3 if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_S3_UPLOAD_ACL:-}" ]] ; then @@ -97,7 +107,7 @@ if [[ "${SINGULAR_UPLOAD_OBJECT}" == "true" ]]; then fi echo "~~~ Uploading artifacts ${EXTRA_MESSAGE}" - buildkite-agent artifact "${args[@]}" "${path}" + bk_agent "${args[@]}" "${path}" elif [[ "${COMPRESSED}" == "true" ]]; then final_paths=() index=0 @@ -120,7 +130,7 @@ elif [[ "${COMPRESSED}" == "true" ]]; then "${compress[@]}" "${final_paths[@]}" echo "~~~ Uploading artifacts ${EXTRA_MESSAGE}" - buildkite-agent artifact "${args[@]}" "${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED}" + bk_agent "${args[@]}" "${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED}" elif [[ "${MULTIPLE_UPLOADS}" == "true" ]]; then index=0 echo "~~~ Uploading artifacts ${EXTRA_MESSAGE}" @@ -136,7 +146,7 @@ elif [[ "${MULTIPLE_UPLOADS}" == "true" ]]; then path="${!dest_env_var}" fi - buildkite-agent artifact "${args[@]}" "$path" + bk_agent "${args[@]}" "$path" ((index+=1)) done fi diff --git a/hooks/pre-command b/hooks/pre-command index 8abaa31..97c71b5 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -46,6 +46,17 @@ if [[ "${#paths[@]}" -le 0 ]]; then exit 0 fi +bk_agent() { + if ! buildkite-agent artifact "${@}"; then + if [[ "${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"}" != "false" ]]; then + echo "Ignoring error in download" + else + echo "Error in download" + exit 1 + fi + fi +} + if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED:-}" ]]; then COMPRESSED="true" if [[ "${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED}" =~ .*\.zip ]]; then @@ -68,8 +79,7 @@ workdir="${BUILDKITE_PLUGIN_ARTIFACTS_WORKDIR:-.}" if [[ "${COMPRESSED}" == "true" ]]; then echo "~~~ Downloading artifacts ${EXTRA_MESSAGE}" - buildkite-agent artifact "${args[@]}" "${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED}" "${workdir}" - + bk_agent "${args[@]}" "${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED}" "${workdir}" echo "~~~ Uncompressing ${paths[*]} from ${BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED}" "${compress[@]}" "${paths[@]}" @@ -107,7 +117,7 @@ elif [[ "${SINGULAR_DOWNLOAD_OBJECT}" == "true" ]]; then fi echo "~~~ Downloading artifacts ${EXTRA_MESSAGE}" - buildkite-agent artifact "${args[@]}" "${source}" "${workdir}" + bk_agent "${args[@]}" "${source}" "${workdir}" if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO:-}" ]]; then if ! [[ -d $(dirname "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}") ]]; then @@ -128,7 +138,7 @@ elif [[ "${MULTIPLE_DOWNLOADS}" == "true" ]]; then else source="${path}" fi - buildkite-agent artifact "${args[@]}" "${source}" "${workdir}" + bk_agent "${args[@]}" "${source}" "${workdir}" if [[ -n "${!dest_env_var:-}" ]]; then if ! [[ -d $(dirname "${!dest_env_var}") ]]; then mkdir -p "$(dirname "${!dest_env_var}")" From 7f9d480d59b7e6da035450a1ef8d96ffe52546ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 13:55:24 -0300 Subject: [PATCH 03/12] Added ignore-missing option to readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index af611db..8809f83 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,9 @@ steps: compressed: logs.tgz ``` +### `ignore-missing` (optional, boolean) + +If set to `true`, it will ignore errors caused when calling `buildkite-agent artifact` to prevent failures if you expect artifacts not to be present in some situations. ### Relocation From a6cfe47bcd8b6bfcf1d3b72d13bd4bd8b4e0853b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 13:56:08 -0300 Subject: [PATCH 04/12] Added better handling of relocations in downloads --- hooks/pre-command | 59 +++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/hooks/pre-command b/hooks/pre-command index 97c71b5..d5787a7 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -23,6 +23,7 @@ COMPRESSED="false" SINGULAR_DOWNLOAD_OBJECT="false" RELOCATION="false" MULTIPLE_DOWNLOADS="false" +IGNORE_ERRORS="${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"}" if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD:-}" ]] || { [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM:-}" ]] && [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO:-}" ]]; }; then SINGULAR_DOWNLOAD_OBJECT="true" @@ -52,7 +53,32 @@ bk_agent() { echo "Ignoring error in download" else echo "Error in download" - exit 1 + return 1 + fi + fi + + return 0 +} + +handle_relocation() { + local index="$1" + local var_string="" + if [[ -n "${index}" ]]; then + var_string="${index}_" + fi + + source_env_var="BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_${var_string}FROM" + dest_env_var="BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_${var_string}TO" + if [[ -n "${!dest_env_var:-}" ]]; then + if ! [[ -d $(dirname "${!dest_env_var}") ]]; then + mkdir -p "$(dirname "${!dest_env_var}")" + fi + + if [[ ! -e "${!source_env_var}" ]] && [[ ${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"} != "false" ]]; then + echo "Ignoring missing file ${!source_env_var} for relocation" + else + echo "~~~ Moving [${!source_env_var}] to [${!dest_env_var}]..." + mv "${!source_env_var}" "${!dest_env_var}" fi fi } @@ -86,26 +112,13 @@ if [[ "${COMPRESSED}" == "true" ]]; then # single relocation if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO:-}" ]]; then - if ! [[ -d $(dirname "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}") ]]; then - mkdir -p "$(dirname "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}")" - fi - echo "~~~ Moving [${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM}] to [${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}]..." - mv "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM}" "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}" + handle_relocation "" fi # multiple relocations index=0 for path in "${paths[@]}"; do - source_env_var="BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_${index}_FROM" - dest_env_var="BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_${index}_TO" - if [[ -n "${!dest_env_var:-}" ]]; then - if ! [[ -d $(dirname "${!dest_env_var}") ]]; then - mkdir -p "$(dirname "${!dest_env_var}")" - fi - echo "~~~ Moving [${!source_env_var}] to [${!dest_env_var}]..." - mv "${!source_env_var}" "${!dest_env_var}" - fi - + handle_relocation "${index}" ((index+=1)) done @@ -120,11 +133,7 @@ elif [[ "${SINGULAR_DOWNLOAD_OBJECT}" == "true" ]]; then bk_agent "${args[@]}" "${source}" "${workdir}" if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO:-}" ]]; then - if ! [[ -d $(dirname "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}") ]]; then - mkdir -p "$(dirname "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}")" - fi - echo "~~~ Moving [${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM}] to [${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}]..." - mv "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM}" "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO}" + handle_relocation "" fi elif [[ "${MULTIPLE_DOWNLOADS}" == "true" ]]; then index=0 @@ -139,13 +148,7 @@ elif [[ "${MULTIPLE_DOWNLOADS}" == "true" ]]; then source="${path}" fi bk_agent "${args[@]}" "${source}" "${workdir}" - if [[ -n "${!dest_env_var:-}" ]]; then - if ! [[ -d $(dirname "${!dest_env_var}") ]]; then - mkdir -p "$(dirname "${!dest_env_var}")" - fi - echo "~~~ Moving [${source}] to [${!dest_env_var}]..." - mv "${source}" "${!dest_env_var}" - fi + handle_relocation "${index}" ((index+=1)) done fi From de43d0d4f64d5063275930e65ba2e2ee77b3e7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 13:56:25 -0300 Subject: [PATCH 05/12] Added tests for ignore-missing in downloads --- tests/download-ignore-missing.bats | 147 +++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tests/download-ignore-missing.bats diff --git a/tests/download-ignore-missing.bats b/tests/download-ignore-missing.bats new file mode 100644 index 0000000..d4e0b0e --- /dev/null +++ b/tests/download-ignore-missing.bats @@ -0,0 +1,147 @@ +#!/usr/bin/env bats + +load '/usr/local/lib/bats/load.bash' + +# Uncomment to enable stub debug output: +# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty + +setup() { + export BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING="true" +} + +@test "Pre-command downloads doesn't fail on agent failure" { + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="*.log" + + stub buildkite-agent \ + "artifact download \* \* : exit 1" + + run "$PWD/hooks/pre-command" + + assert_success + assert_output --partial "Downloading artifacts" + assert_output --partial "Ignoring error in download" + + unstub buildkite-agent +} + +@test "Pre-command downloads artifacts with relocation doesn't fail" { + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO="/tmp/foo2.log" + + stub buildkite-agent \ + "artifact download \* \* : exit 1" + + run "$PWD/hooks/pre-command" + + assert_success + assert_output --partial "Downloading artifacts" + assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring missing file /tmp/foo.log for relocation" + + unstub buildkite-agent +} + +@test "Pre-command downloads multiple artifacts with a failure" { + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0="foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact download \* \* : echo downloaded artifact \$3 to \$4" \ + "artifact download \* \* : exit 1" \ + "artifact download \* \* : echo downloaded artifact \$3 to \$4" + + run "$PWD/hooks/pre-command" + + assert_success + assert_output --partial "Downloading artifacts" + assert_output --partial "Ignoring error in download" + refute_output --partial "download artifact bar.log" + + unstub buildkite-agent +} + +@test "Pre-command downloads multiple artifacts with some relocation and failure" { + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_TO="/tmp/foo2.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact download \* \* : touch /tmp/foo.log; echo downloaded artifact \$3 to \$4" \ + "artifact download \* \* : exit 1" \ + "artifact download \* \* : echo downloaded artifact \$3 to \$4" + + run "$PWD/hooks/pre-command" + + assert_success + assert_output --partial "Downloading artifacts" + assert_output --partial "Ignoring error in download" + refute_output --partial "download artifact bar.log" + + assert [ -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + + rm /tmp/foo2.log + unstub buildkite-agent +} + +@test "Pre-command downloads multiple artifacts with some relocation and failure on relocation" { + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_TO="/tmp/foo2.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact download \* \* : exit 1" \ + "artifact download \* \* : echo downloaded artifact \$3 to \$4" \ + "artifact download \* \* : echo downloaded artifact \$3 to \$4" + + run "$PWD/hooks/pre-command" + + assert_success + assert_output --partial "Downloading artifacts" + assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring missing file /tmp/foo.log for relocation" + refute_output --partial "download artifact /tmp/foo.log" + + assert [ ! -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + + unstub buildkite-agent +} + + +@test "Pre-command downloads multiple > 10 artifacts with build and relocation and some failures" { + stub_calls=() + for i in $(seq 0 10); do + export "BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_${i}_FROM=/tmp/foo-${i}.log" + export "BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_${i}_TO=/tmp/foo-r-${i}.log" + if [ $((i%2)) -eq 0 ]; then + stub_calls+=( "artifact download \* \* : touch /tmp/foo-$i.log; echo downloaded artifact \$3 to \$4" ) + else + stub_calls+=( "artifact download \* \* : exit 1" ) + fi + done + stub buildkite-agent "${stub_calls[@]}" + + run "$PWD/hooks/pre-command" + + assert_success + assert_output --partial "Downloading artifacts" + + for i in $(seq 0 10); do + if [ $((i%2)) -eq 0 ]; then + assert [ -e /tmp/foo-r-"${i}".log ] + assert [ ! -e /tmp/foo-"${i}".log ] + rm /tmp/foo-r-"${i}".log + else + assert [ ! -e /tmp/foo-r-"${i}".log ] + assert [ ! -e /tmp/foo-"${i}".log ] + assert_output --partial "Ignoring missing file /tmp/foo-${i}.log" + refute_output --partial "downloaded artifact /tmp/foo-${i}.log" + fi + done + + unstub buildkite-agent +} \ No newline at end of file From ed96538aa32660aa4192c83a93aa0c291978987b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 13:56:55 -0300 Subject: [PATCH 06/12] Remove unnecessary variable --- hooks/pre-command | 1 - 1 file changed, 1 deletion(-) diff --git a/hooks/pre-command b/hooks/pre-command index d5787a7..9046492 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -23,7 +23,6 @@ COMPRESSED="false" SINGULAR_DOWNLOAD_OBJECT="false" RELOCATION="false" MULTIPLE_DOWNLOADS="false" -IGNORE_ERRORS="${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"}" if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD:-}" ]] || { [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM:-}" ]] && [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO:-}" ]]; }; then SINGULAR_DOWNLOAD_OBJECT="true" From 774a34620f028f21096e30c9690d884c17f0a8b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 14:06:16 -0300 Subject: [PATCH 07/12] Refactored upload relocation for easier missing handling --- hooks/post-command | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/hooks/post-command b/hooks/post-command index 64e0055..96160c8 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -71,6 +71,26 @@ bk_agent() { fi } +handle_relocation() { + local index="$1" + local var_string="" + if [[ -n "${index}" ]]; then + var_string="${index}_" + fi + + source_env_var="BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${var_string}FROM" + dest_env_var="BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${var_string}TO" + + if [[ -n "${!source_env_var:-}" ]] && [[ -n "${!dest_env_var:-}" ]]; then + if [[ -e ${!source_env_var} ]]; then + echo "~~~ Moving [${!source_env_var}] to [${!dest_env_var}]..." + mv ${!source_env_var} ${!dest_env_var} + elif [[ ${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"} == "true" ]]; then + echo "Ignoring missing file ${!source_env_var} for relocation" + fi + fi +} + # Set user-provided object ACL for AWS S3 if [[ -n "${BUILDKITE_PLUGIN_ARTIFACTS_S3_UPLOAD_ACL:-}" ]] ; then echo "~~~ Setting S3 object upload ACL: ${BUILDKITE_PLUGIN_ARTIFACTS_S3_UPLOAD_ACL}" @@ -91,10 +111,7 @@ fi if [[ "${SINGULAR_UPLOAD_OBJECT}" == "true" ]]; then if [[ "${RELOCATION}" == "true" ]]; then - if [[ -e "${BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_FROM:-}" ]]; then - echo "~~~ Moving [${BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_FROM}] to [${BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO}]..." - mv "${BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_FROM}" "${BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO}" - fi + handle_relocation "" path="${BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO}" else path="${paths[0]}" @@ -112,13 +129,9 @@ elif [[ "${COMPRESSED}" == "true" ]]; then final_paths=() index=0 for path in "${paths[@]}"; do - source_env_var="BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${index}_FROM" + handle_relocation "${index}" dest_env_var="BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${index}_TO" - if [[ -n "${!source_env_var:-}" ]] && [[ -n "${!dest_env_var:-}" ]]; then - if [[ -e ${!source_env_var} ]]; then - echo "~~~ Moving [${!source_env_var}] to [${!dest_env_var}]..." - mv ${!source_env_var} ${!dest_env_var} - fi + if [[ -n "${!dest_env_var:-}" ]]; then path="${!dest_env_var}" fi @@ -136,13 +149,9 @@ elif [[ "${MULTIPLE_UPLOADS}" == "true" ]]; then echo "~~~ Uploading artifacts ${EXTRA_MESSAGE}" for path in "${paths[@]}"; do - source_env_var="BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${index}_FROM" + handle_relocation "${index}" dest_env_var="BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${index}_TO" - if [[ -n "${!source_env_var:-}" ]] && [[ -n "${!dest_env_var:-}" ]]; then - if [[ -e ${!source_env_var} ]]; then - echo "~~~ Moving [${!source_env_var}] to [${!dest_env_var}]..." - mv ${!source_env_var} ${!dest_env_var} - fi + if [[ -n "${!dest_env_var:-}" ]]; then path="${!dest_env_var}" fi From 0f76ba01953778062bbf5cfa852343a15116be68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 14:43:49 -0300 Subject: [PATCH 08/12] Better errors --- hooks/post-command | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hooks/post-command b/hooks/post-command index 96160c8..8200cf5 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -63,9 +63,9 @@ trap popd EXIT bk_agent() { if ! buildkite-agent artifact "${@}"; then if [[ "${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"}" != "false" ]]; then - echo "Ignoring error in upload" + echo "Ignoring error in upload of" "${@: -1}" else - echo "Error in upload" + echo "Error in upload of" "${@:-1}" exit 1 fi fi @@ -84,7 +84,7 @@ handle_relocation() { if [[ -n "${!source_env_var:-}" ]] && [[ -n "${!dest_env_var:-}" ]]; then if [[ -e ${!source_env_var} ]]; then echo "~~~ Moving [${!source_env_var}] to [${!dest_env_var}]..." - mv ${!source_env_var} ${!dest_env_var} + mv "${!source_env_var}" "${!dest_env_var}" elif [[ ${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"} == "true" ]]; then echo "Ignoring missing file ${!source_env_var} for relocation" fi From 23e2331092f29ea7da43c3f43505a319195786ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 14:44:00 -0300 Subject: [PATCH 09/12] Added tests for uploads with ignore missing --- tests/upload-ignore-missing.bats | 213 +++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 tests/upload-ignore-missing.bats diff --git a/tests/upload-ignore-missing.bats b/tests/upload-ignore-missing.bats new file mode 100644 index 0000000..4a3005f --- /dev/null +++ b/tests/upload-ignore-missing.bats @@ -0,0 +1,213 @@ +#!/usr/bin/env bats + +load '/usr/local/lib/bats/load.bash' + +# Uncomment to enable stub debug output: +# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty + +setup() { + export BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING="true" +} + +@test "Post-command does not fail with ignore missing" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="*.log" + + stub buildkite-agent \ + "artifact upload \* : exit 1" + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + assert_output --partial "Ignoring error in upload of *.log" + + unstub buildkite-agent +} + +@test "Post-command with relocation does not fail" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO="/tmp/foo2.log" + + stub buildkite-agent \ + "artifact upload \* : exit 1" + + touch /tmp/foo.log + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Moving [/tmp/foo.log]" + assert_output --partial "Uploading artifacts" + assert_output --partial "Ignoring error in upload of /tmp/foo2.log" + refute_output --partial "uploaded /tmp/foo2.log" + refute_output --partial "uploaded /tmp/foo.log" + assert [ -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + rm -f /tmp/foo2.log + + unstub buildkite-agent +} + +@test "Post-command with relocation does not fail if file does not exist" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO="/tmp/foo2.log" + + stub buildkite-agent \ + "artifact upload \* : exit 1" + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + assert_output --partial "Ignoring missing file /tmp/foo.log" + assert_output --partial "Ignoring error in upload of /tmp/foo2.log" + refute_output --partial "Moving [/tmp/foo.log]" + refute_output --partial "uploaded /tmp/foo2.log" + refute_output --partial "uploaded /tmp/foo.log" + assert [ ! -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + + unstub buildkite-agent +} + +@test "Post-command does not file with multiple artifacts and some failures" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact upload \* : echo uploaded \$3" \ + "artifact upload \* : exit 1" \ + "artifact upload \* : echo uploaded \$3" + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + assert_output --partial "Ignoring error in upload of bar.log" + refute_output --partial "uploaded bar.log" + + unstub buildkite-agent +} + +@test "Post-command does not fail with some relocation and failure" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_TO="/tmp/foo2.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact upload \* : echo uploaded \$3" \ + "artifact upload \* : exit 1" \ + "artifact upload \* : echo uploaded \$3" + + touch /tmp/foo.log + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + refute_output --partial "uploaded /tmp/foo.log" + assert_output --partial "Ignoring error in upload of bar.log" + refute_output --partial "uploaded bar.log" + assert [ -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + + rm /tmp/foo2.log + + unstub buildkite-agent +} + +@test "Post-command does not fail with some relocation that fails" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_TO="/tmp/foo2.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact upload \* : exit 1" \ + "artifact upload \* : echo uploaded \$3" \ + "artifact upload \* : echo uploaded \$3" + + touch /tmp/foo.log + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + assert_output --partial "Moving [/tmp/foo.log]" + refute_output --partial "uploaded /tmp/foo.log" + refute_output --partial "uploaded /tmp/foo2.log" + assert_output --partial "Ignoring error in upload of /tmp/foo2.log" + + assert [ -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + + rm /tmp/foo2.log + + unstub buildkite-agent +} + + +@test "Post-command does not fail with some relocation and missing file" { + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_FROM="/tmp/foo.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_TO="/tmp/foo2.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1="bar.log" + export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_2="baz.log" + + stub buildkite-agent \ + "artifact upload \* : exit 1" \ + "artifact upload \* : echo uploaded \$3" \ + "artifact upload \* : echo uploaded \$3" + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + refute_output --partial "Moving [/tmp/foo.log]" + refute_output --partial "uploaded /tmp/foo.log" + refute_output --partial "uploaded /tmp/foo2.log" + assert_output --partial "Ignoring missing file /tmp/foo.log" + assert_output --partial "Ignoring error in upload of /tmp/foo2.log" + + assert [ ! -e /tmp/foo2.log ] + assert [ ! -e /tmp/foo.log ] + + unstub buildkite-agent +} + +@test "Post-command uploads multiple > 10 artifacts with relocation and some failures" { + stub_calls=() + for i in $(seq 0 10); do + export "BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${i}_FROM=/tmp/foo-${i}.log" + export "BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_${i}_TO=/tmp/foo-r-${i}.log" + touch "/tmp/foo-${i}.log" + + if [ $((i%2)) -eq 0 ]; then + stub_calls+=( "artifact upload \* : echo uploaded \$3" ) + else + stub_calls+=( "artifact upload \* : exit 1" ) + fi + done + + stub buildkite-agent "${stub_calls[@]}" + + run "$PWD/hooks/post-command" + + assert_success + assert_output --partial "Uploading artifacts" + + for i in $(seq 0 10); do + assert [ -e /tmp/foo-r-"${i}".log ] + assert [ ! -e /tmp/foo-"${i}".log ] + rm /tmp/foo-r-"${i}".log + + if [ $((i%2)) -eq 0 ]; then + refute_output --partial "uploaded /tmp/foo-${i}.log" + else + assert_output --partial "Ignoring error in upload of /tmp/foo-r-${i}.log" + fi + done + + unstub buildkite-agent +} \ No newline at end of file From 1ed8b7524f2696d426d178635e7e8f34e9c02756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 14:46:37 -0300 Subject: [PATCH 10/12] Made errors better when ignoring failures --- hooks/pre-command | 4 ++-- tests/download-ignore-missing.bats | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/hooks/pre-command b/hooks/pre-command index 9046492..e1b8318 100755 --- a/hooks/pre-command +++ b/hooks/pre-command @@ -49,9 +49,9 @@ fi bk_agent() { if ! buildkite-agent artifact "${@}"; then if [[ "${BUILDKITE_PLUGIN_ARTIFACTS_IGNORE_MISSING:-"false"}" != "false" ]]; then - echo "Ignoring error in download" + echo "Ignoring error in download of" "${@: -2}" else - echo "Error in download" + echo "Error in download of" "${@: -2}" return 1 fi fi diff --git a/tests/download-ignore-missing.bats b/tests/download-ignore-missing.bats index d4e0b0e..e6ba3a6 100644 --- a/tests/download-ignore-missing.bats +++ b/tests/download-ignore-missing.bats @@ -19,7 +19,7 @@ setup() { assert_success assert_output --partial "Downloading artifacts" - assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring error in download of *.log" unstub buildkite-agent } @@ -35,7 +35,7 @@ setup() { assert_success assert_output --partial "Downloading artifacts" - assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring error in download of /tmp/foo.log" assert_output --partial "Ignoring missing file /tmp/foo.log for relocation" unstub buildkite-agent @@ -55,7 +55,7 @@ setup() { assert_success assert_output --partial "Downloading artifacts" - assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring error in download of bar.log" refute_output --partial "download artifact bar.log" unstub buildkite-agent @@ -76,7 +76,7 @@ setup() { assert_success assert_output --partial "Downloading artifacts" - assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring error in download of bar.log" refute_output --partial "download artifact bar.log" assert [ -e /tmp/foo2.log ] @@ -101,7 +101,7 @@ setup() { assert_success assert_output --partial "Downloading artifacts" - assert_output --partial "Ignoring error in download" + assert_output --partial "Ignoring error in download of /tmp/foo.log" assert_output --partial "Ignoring missing file /tmp/foo.log for relocation" refute_output --partial "download artifact /tmp/foo.log" @@ -138,6 +138,7 @@ setup() { else assert [ ! -e /tmp/foo-r-"${i}".log ] assert [ ! -e /tmp/foo-"${i}".log ] + assert_output --partial "Ignoring error in download of /tmp/foo-${i}.log" assert_output --partial "Ignoring missing file /tmp/foo-${i}.log" refute_output --partial "downloaded artifact /tmp/foo-${i}.log" fi From c2bab44d45210c701296fb90192c40667ffda1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 14:47:27 -0300 Subject: [PATCH 11/12] Updated plugin version for next release --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8809f83..6e11357 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This functionality duplicates the [artifact_paths](https://buildkite.com/docs/pi steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: "log/**/*.log" ``` @@ -20,7 +20,7 @@ or steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: [ "log/**/*.log", "debug/*.error" ] ``` @@ -30,7 +30,7 @@ or steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: from: log1.log to: log2.log @@ -42,7 +42,7 @@ or steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: - from: log1.log to: log2.log @@ -58,7 +58,7 @@ eg: uploading a public file when using S3 steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: "coverage-report/**/*" s3-upload-acl: public-read ``` @@ -68,7 +68,7 @@ eg: uploading a private file when using GS steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: "coverage-report/**/*" gs-upload-acl: private ``` @@ -81,7 +81,7 @@ This downloads artifacts matching globs to the local filesystem. See [downloadin steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: download: "log/**/*.log" ``` @@ -91,7 +91,7 @@ or steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: download: [ "log/**/*.log", "debug/*.error" ] ``` @@ -101,7 +101,7 @@ or steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: download: from: log1.log to: log2.log @@ -113,7 +113,7 @@ or steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: download: - from: log1.log to: log2.log @@ -149,7 +149,7 @@ When uploading, globs specified in the `upload` option will be compressed in a s steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: upload: "log/*.log" compressed: logs.zip ``` @@ -160,7 +160,7 @@ When downloading, this option states the actual name of the artifact to be downl steps: - command: ... plugins: - - artifacts#v1.5.0: + - artifacts#v1.7.0: download: "log/*.log" compressed: logs.tgz ``` From 3cfbd160a9c96d8bd4418c4efed0e8000b42a45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 24 Oct 2022 18:46:42 -0300 Subject: [PATCH 12/12] Updated docker compose plugin to latest version --- .buildkite/pipeline.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.buildkite/pipeline.yml b/.buildkite/pipeline.yml index d2c40b4..022226e 100644 --- a/.buildkite/pipeline.yml +++ b/.buildkite/pipeline.yml @@ -1,7 +1,7 @@ steps: - label: "🤡 :hammer:" plugins: - - docker-compose#v4.1.1: + - docker-compose#v4.5.0: run: tests - label: ":sparkles: lint" plugins: