diff --git a/hack/jenkins/upload-to-gcs.sh b/hack/jenkins/upload-to-gcs.sh index 13c5fafb39b..5c099dbde28 100755 --- a/hack/jenkins/upload-to-gcs.sh +++ b/hack/jenkins/upload-to-gcs.sh @@ -31,6 +31,8 @@ # Note: for magicfile support to work correctly, the "file" utility must be # installed. +# TODO(rmmh): rewrite this script in Python so we can actually test it! + set -o errexit set -o nounset set -o pipefail @@ -102,6 +104,35 @@ function find_version() { ) } +# Output started.json. Use test function below! +function print_started() { + local metadata_keys=$(compgen -e | grep ^BUILD_METADATA_) + echo "{" + echo " \"version\": \"${version}\"," + echo " \"timestamp\": ${timestamp}," + if [[ -n "${metadata_keys}" ]]; then + # Any exported variables of the form BUILD_METADATA_KEY=VALUE + # will be available as started["metadata"][KEY.lower()]. + echo " \"metadata\": {" + local sep="" # leading commas are easy to track + for env_var in $metadata_keys; do + local var_upper="${env_var#BUILD_METADATA_}" + echo " $sep\"${var_upper,,}\": \"${!env_var}\"" + sep="," + done + echo " }," + fi + echo " \"jenkins-node\": \"${NODE_NAME:-}\"" + echo "}" +} + +# Use this to test changes to print_started. +if [[ -n "${TEST_STARTED_JSON:-}" ]]; then + version=$(find_version) + cat <(print_started) | jq . + exit +fi + function upload_version() { local -r version=$(find_version) local upload_attempt @@ -117,13 +148,7 @@ function upload_version() { local -r json_file="${gcs_build_path}/started.json" for upload_attempt in {1..3}; do echo "Uploading version to: ${json_file} (attempt ${upload_attempt})" - gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" <( - echo "{" - echo " \"version\": \"${version}\"," - echo " \"timestamp\": ${timestamp}," - echo " \"jenkins-node\": \"${NODE_NAME:-}\"" - echo "}" - ) "${json_file}" || continue + gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" <(print_started) "${json_file}" || continue break done }