From 90f9c83d984ad7972bb41e7c6534ef006456f412 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Wed, 14 Sep 2016 16:05:07 -0700 Subject: [PATCH] Make upload-to-gcs.sh include BUILD_METADATA_* env vars in started.json. This is initially intended for storing GCI image versions. Example output: $ TEST_STARTED_JSON=1 BUILD_METADATA_GCE_IMAGE=m53 \ BUILD_METADATA_VARIANT=reliableforreal BUILD_NUMBER=123 \ WORKSPACE="blaga" JOB_NAME="test-e2e" hack/jenkins/upload-to-gcs.sh { "version": "v1.5.0-alpha.0.806+fa943089125df4-dirty", "timestamp": 1473894301, "metadata": { "gce_image": "m53", "variant": "reliableforreal" }, "jenkins-node": "" } --- hack/jenkins/upload-to-gcs.sh | 39 ++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) 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 }