upload-to-gcs.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. # Copyright 2015 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script uploads metadata and test results to Google Cloud Storage, in the
  16. # location indicated by JENKINS_GCS_LOGS_PATH. By default, we use the Google
  17. # kubernetes-jenkins bucket.
  18. #
  19. # The script looks for one of two environment variables to be set:
  20. # JENKINS_BUILD_STARTED: set to a nonempty string to upload version
  21. # information to 'started.json'. The value of the variable is not
  22. # currently used.
  23. # JENKINS_BUILD_FINISHED: set to the Jenkins build result to upload the build
  24. # result to 'finished.json', any test artifacts, and update the
  25. # 'latest-build.txt' file pointer. Since this script uses gsutil directly,
  26. # it's a bit faster at uploading large numbers of files than the GCS Jenkins
  27. # plugin. It also makes use of gsutil's gzip functionality.
  28. #
  29. # Note: for magicfile support to work correctly, the "file" utility must be
  30. # installed.
  31. set -o errexit
  32. set -o nounset
  33. set -o pipefail
  34. if [[ -n "${JENKINS_BUILD_STARTED:-}" && -n "${JENKINS_BUILD_FINISHED:-}" ]]; then
  35. echo "Error: JENKINS_BUILD_STARTED and JENKINS_BUILD_FINISHED should not both be set!"
  36. exit 1
  37. fi
  38. if [[ ! ${JENKINS_UPLOAD_TO_GCS:-y} =~ ^[yY]$ ]]; then
  39. exit 0
  40. fi
  41. if [[ ${JOB_NAME} =~ -pull- ]]; then
  42. : ${JENKINS_GCS_LOGS_PATH:="gs://kubernetes-jenkins/pr-logs/pull/${ghprbPullId:-unknown}"}
  43. : ${JENKINS_GCS_LATEST_PATH:="gs://kubernetes-jenkins/pr-logs/directory"}
  44. : ${JENKINS_GCS_LOGS_INDIRECT:="gs://kubernetes-jenkins/pr-logs/directory/${JOB_NAME}"}
  45. else
  46. : ${JENKINS_GCS_LOGS_PATH:="gs://kubernetes-jenkins/logs"}
  47. : ${JENKINS_GCS_LATEST_PATH:="gs://kubernetes-jenkins/logs"}
  48. : ${JENKINS_GCS_LOGS_INDIRECT:=""}
  49. fi
  50. readonly artifacts_path="${WORKSPACE}/_artifacts"
  51. readonly gcs_job_path="${JENKINS_GCS_LOGS_PATH}/${JOB_NAME}"
  52. readonly gcs_build_path="${gcs_job_path}/${BUILD_NUMBER}"
  53. readonly gcs_latest_path="${JENKINS_GCS_LATEST_PATH}/${JOB_NAME}"
  54. readonly gcs_indirect_path="${JENKINS_GCS_LOGS_INDIRECT}"
  55. readonly gcs_acl="public-read"
  56. readonly results_url=${gcs_build_path//"gs:/"/"https://console.cloud.google.com/storage/browser"}
  57. readonly timestamp=$(date +%s)
  58. #########################################################################
  59. # $0 is called from different contexts so figure out where kubernetes is.
  60. # Sets non-exported global kubernetes_base_path and defaults to "."
  61. function set_kubernetes_base_path () {
  62. for kubernetes_base_path in kubernetes go/src/k8s.io/kubernetes .; do
  63. # Pick a canonical item to find in a kubernetes tree which could be a
  64. # raw source tree or an expanded tarball.
  65. [[ -f ${kubernetes_base_path}/cluster/common.sh ]] && break
  66. done
  67. }
  68. #########################################################################
  69. # Try to discover the kubernetes version.
  70. # prints version
  71. function find_version() {
  72. (
  73. # Where are we?
  74. # This could be set in the global scope at some point if we need to
  75. # discover the kubernetes path elsewhere.
  76. set_kubernetes_base_path
  77. cd ${kubernetes_base_path}
  78. if [[ -e "version" ]]; then
  79. cat version
  80. elif [[ -e "hack/lib/version.sh" ]]; then
  81. export KUBE_ROOT="."
  82. source "hack/lib/version.sh"
  83. kube::version::get_version_vars
  84. echo "${KUBE_GIT_VERSION-}"
  85. else
  86. # Last resort from the started.json
  87. gsutil cat ${gcs_build_path}/started.json 2>/dev/null |\
  88. sed -n 's/ *"version": *"\([^"]*\)",*/\1/p'
  89. fi
  90. )
  91. }
  92. function upload_version() {
  93. local -r version=$(find_version)
  94. local upload_attempt
  95. echo -n 'Run starting at '; date -d "@${timestamp}"
  96. if [[ -n "${version}" ]]; then
  97. echo "Found Kubernetes version: ${version}"
  98. else
  99. echo "Could not find Kubernetes version"
  100. fi
  101. local -r json_file="${gcs_build_path}/started.json"
  102. for upload_attempt in {1..3}; do
  103. echo "Uploading version to: ${json_file} (attempt ${upload_attempt})"
  104. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" <(
  105. echo "{"
  106. echo " \"version\": \"${version}\","
  107. echo " \"timestamp\": ${timestamp},"
  108. echo " \"jenkins-node\": \"${NODE_NAME:-}\""
  109. echo "}"
  110. ) "${json_file}" || continue
  111. break
  112. done
  113. }
  114. #########################################################################
  115. # Maintain a single file storing the full build version, Jenkins' job number
  116. # build state. Limit its size so it does not grow unbounded.
  117. # This is primarily used for and by the
  118. # github.com/kubernetes/release/find_green_build tool.
  119. # @param build_result - the state of the build
  120. #
  121. function update_job_result_cache() {
  122. local -r build_result=$1
  123. local -r version=$(find_version)
  124. local -r job_results=${gcs_job_path}/jobResultsCache.json
  125. local -r tmp_results="${WORKSPACE}/_tmp/jobResultsCache.tmp"
  126. local -r cache_size=200
  127. local upload_attempt
  128. if [[ -n "${version}" ]]; then
  129. echo "Found Kubernetes version: ${version}"
  130. else
  131. echo "Could not find Kubernetes version"
  132. fi
  133. mkdir -p ${tmp_results%/*}
  134. # Construct a valid json file
  135. echo "[" > ${tmp_results}
  136. for upload_attempt in $(seq 3); do
  137. echo "Copying ${job_results} to ${tmp_results} (attempt ${upload_attempt})"
  138. # The sed construct below is stripping out only the "version" lines
  139. # and then ensuring there's a single comma at the end of the line.
  140. gsutil -q cat ${job_results} 2>&- |\
  141. sed -n 's/^\({"version".*}\),*/\1,/p' |\
  142. tail -${cache_size} >> ${tmp_results} || continue
  143. break
  144. done
  145. echo "{\"version\": \"${version}\", \"buildnumber\": \"${BUILD_NUMBER}\"," \
  146. "\"result\": \"${build_result}\"}" >> ${tmp_results}
  147. echo "]" >> ${tmp_results}
  148. for upload_attempt in $(seq 3); do
  149. echo "Copying ${tmp_results} to ${job_results} (attempt ${upload_attempt})"
  150. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" \
  151. ${tmp_results} ${job_results} || continue
  152. break
  153. done
  154. rm -f ${tmp_results}
  155. }
  156. function upload_artifacts_and_build_result() {
  157. local -r build_result=$1
  158. local upload_attempt
  159. echo -n 'Run finished at '; date -d "@${timestamp}"
  160. for upload_attempt in {1..3}; do
  161. echo "Uploading to ${gcs_build_path} (attempt ${upload_attempt})"
  162. echo "Uploading build result: ${build_result}"
  163. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" <(
  164. echo "{"
  165. echo " \"result\": \"${build_result}\","
  166. echo " \"timestamp\": ${timestamp}"
  167. echo "}"
  168. ) "${gcs_build_path}/finished.json" || continue
  169. if [[ -d "${artifacts_path}" && -n $(ls -A "${artifacts_path}") ]]; then
  170. echo "Uploading artifacts"
  171. gsutil -m -q -o "GSUtil:use_magicfile=True" cp -a "${gcs_acl}" -r -c \
  172. -z log,txt,xml "${artifacts_path}" "${gcs_build_path}/artifacts" || continue
  173. fi
  174. if [[ -e "${WORKSPACE}/build-log.txt" ]]; then
  175. echo "Uploading build log"
  176. gsutil -q cp -Z -a "${gcs_acl}" "${WORKSPACE}/build-log.txt" "${gcs_build_path}"
  177. fi
  178. # For pull jobs, keep a canonical ordering for tools that want to examine
  179. # the output.
  180. if [[ "${gcs_indirect_path}" != "" ]]; then
  181. echo "Writing ${gcs_build_path} to ${gcs_indirect_path}/${BUILD_NUMBER}.txt"
  182. echo "${gcs_build_path}" | \
  183. gsutil -q -h "Content-Type:text/plain" \
  184. cp -a "${gcs_acl}" - "${gcs_indirect_path}/${BUILD_NUMBER}.txt" || continue
  185. echo "Marking build ${BUILD_NUMBER} as the latest completed build for this PR"
  186. echo "${BUILD_NUMBER}" | \
  187. gsutil -q -h "Content-Type:text/plain" -h "Cache-Control:private, max-age=0, no-transform" \
  188. cp -a "${gcs_acl}" - "${gcs_job_path}/latest-build.txt" || continue
  189. fi
  190. # Mark this build as the latest completed.
  191. echo "Marking build ${BUILD_NUMBER} as the latest completed build"
  192. echo "${BUILD_NUMBER}" | \
  193. gsutil -q -h "Content-Type:text/plain" -h "Cache-Control:private, max-age=0, no-transform" \
  194. cp -a "${gcs_acl}" - "${gcs_latest_path}/latest-build.txt" || continue
  195. break # all uploads succeeded if we hit this point
  196. done
  197. echo -e "\n\n\n*** View logs and artifacts at ${results_url} ***\n\n"
  198. }
  199. if [[ -n "${JENKINS_BUILD_STARTED:-}" ]]; then
  200. upload_version
  201. elif [[ -n "${JENKINS_BUILD_FINISHED:-}" ]]; then
  202. upload_artifacts_and_build_result ${JENKINS_BUILD_FINISHED}
  203. update_job_result_cache ${JENKINS_BUILD_FINISHED}
  204. else
  205. echo "Called without JENKINS_BUILD_STARTED or JENKINS_BUILD_FINISHED set."
  206. echo "Assuming a legacy invocation."
  207. upload_artifacts_and_build_result "[UNSET]"
  208. fi