upload-to-gcs.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/env 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. # TODO(rmmh): rewrite this script in Python so we can actually test it!
  32. set -o errexit
  33. set -o nounset
  34. set -o pipefail
  35. if [[ -n "${JENKINS_BUILD_STARTED:-}" && -n "${JENKINS_BUILD_FINISHED:-}" ]]; then
  36. echo "Error: JENKINS_BUILD_STARTED and JENKINS_BUILD_FINISHED should not both be set!"
  37. exit 1
  38. fi
  39. if [[ ! ${JENKINS_UPLOAD_TO_GCS:-y} =~ ^[yY]$ ]]; then
  40. exit 0
  41. fi
  42. # Attempt to determine if we're running against a repo other than
  43. # kubernetes/kubernetes to determine whether to place PR logs in a different
  44. # location.
  45. #
  46. # In the current CI system, the tracked repo is named remote. This is not true
  47. # in general for most devs, where origin and upstream are more common.
  48. GCS_SUBDIR=""
  49. readonly remote_git_repo=$(git config --get remote.remote.url | sed 's:.*github.com/::' || true)
  50. if [[ -n "${remote_git_repo}" ]]; then
  51. case "${remote_git_repo}" in
  52. # main repo: nothing extra
  53. kubernetes/kubernetes) GCS_SUBDIR="" ;;
  54. # a different repo on the k8s org: just the repo name (strip kubernetes/)
  55. kubernetes/*) GCS_SUBDIR="${remote_git_repo#kubernetes/}/" ;;
  56. # any other repo: ${org}_${repo} (replace / with _)
  57. *) GCS_SUBDIR="${remote_git_repo/\//_}/" ;;
  58. esac
  59. if [[ "${remote_git_repo}" != "kubernetes/kubernetes" ]]; then
  60. # also store the repo in started.json, so Gubernator can link it properly.
  61. export BUILD_METADATA_REPO="${remote_git_repo}"
  62. fi
  63. fi
  64. if [[ ${JOB_NAME} =~ -pull- ]]; then
  65. : "${JENKINS_GCS_LOGS_PATH:="gs://kubernetes-jenkins/pr-logs/pull/${GCS_SUBDIR}${ghprbPullId:-unknown}"}"
  66. : "${JENKINS_GCS_LATEST_PATH:="gs://kubernetes-jenkins/pr-logs/directory"}"
  67. : "${JENKINS_GCS_LOGS_INDIRECT:="gs://kubernetes-jenkins/pr-logs/directory/${JOB_NAME}"}"
  68. else
  69. : "${JENKINS_GCS_LOGS_PATH:="gs://kubernetes-jenkins/logs"}"
  70. : "${JENKINS_GCS_LATEST_PATH:="gs://kubernetes-jenkins/logs"}"
  71. : "${JENKINS_GCS_LOGS_INDIRECT:=""}"
  72. fi
  73. readonly artifacts_path="${WORKSPACE}/_artifacts"
  74. readonly gcs_job_path="${JENKINS_GCS_LOGS_PATH}/${JOB_NAME}"
  75. readonly gcs_build_path="${gcs_job_path}/${BUILD_NUMBER}"
  76. readonly gcs_latest_path="${JENKINS_GCS_LATEST_PATH}/${JOB_NAME}"
  77. readonly gcs_indirect_path="${JENKINS_GCS_LOGS_INDIRECT}"
  78. readonly gcs_acl="public-read"
  79. readonly results_url=${gcs_build_path//"gs:/"/"https://console.cloud.google.com/storage/browser"}
  80. readonly timestamp=$(date +%s)
  81. #########################################################################
  82. # $0 is called from different contexts so figure out where kubernetes is.
  83. # Sets non-exported global kubernetes_base_path and defaults to "."
  84. function set_kubernetes_base_path () {
  85. for kubernetes_base_path in kubernetes go/src/k8s.io/kubernetes .; do
  86. # Pick a canonical item to find in a kubernetes tree which could be a
  87. # raw source tree or an expanded tarball.
  88. [[ -f ${kubernetes_base_path}/cluster/common.sh ]] && break
  89. done
  90. }
  91. #########################################################################
  92. # Try to discover the kubernetes version.
  93. # prints version
  94. function find_version() {
  95. (
  96. # Where are we?
  97. # This could be set in the global scope at some point if we need to
  98. # discover the kubernetes path elsewhere.
  99. set_kubernetes_base_path
  100. cd ${kubernetes_base_path}
  101. if [[ -e "version" ]]; then
  102. cat version
  103. elif [[ -e "hack/lib/version.sh" ]]; then
  104. export KUBE_ROOT="."
  105. source "hack/lib/version.sh"
  106. kube::version::get_version_vars
  107. echo "${KUBE_GIT_VERSION-}"
  108. else
  109. # Last resort from the started.json
  110. gsutil cat "${gcs_build_path}/started.json" 2>/dev/null |\
  111. sed -n 's/ *"version": *"\([^"]*\)",*/\1/p'
  112. fi
  113. )
  114. }
  115. # Output started.json. Use test function below!
  116. function print_started() {
  117. local metadata_keys
  118. metadata_keys=$(compgen -e | grep ^BUILD_METADATA_)
  119. echo "{"
  120. echo " \"version\": \"${version}\"," # TODO(fejta): retire
  121. echo " \"job-version\": \"${version}\","
  122. echo " \"timestamp\": ${timestamp},"
  123. if [[ -n "${metadata_keys}" ]]; then
  124. # Any exported variables of the form BUILD_METADATA_KEY=VALUE
  125. # will be available as started["metadata"][KEY.lower()].
  126. echo " \"metadata\": {"
  127. local sep="" # leading commas are easy to track
  128. for env_var in ${metadata_keys}; do
  129. local var_upper="${env_var#BUILD_METADATA_}"
  130. echo " ${sep}\"${var_upper,,}\": \"${!env_var}\""
  131. sep=","
  132. done
  133. echo " },"
  134. fi
  135. echo " \"jenkins-node\": \"${NODE_NAME:-}\""
  136. echo "}"
  137. }
  138. # Use this to test changes to print_started.
  139. if [[ -n "${TEST_STARTED_JSON:-}" ]]; then
  140. version=$(find_version)
  141. jq . <(print_started)
  142. exit
  143. fi
  144. function upload_version() {
  145. local -r version=$(find_version)
  146. local upload_attempt
  147. echo -n 'Run starting at '; date -d "@${timestamp}"
  148. if [[ -n "${version}" ]]; then
  149. echo "Found Kubernetes version: ${version}"
  150. else
  151. echo "Could not find Kubernetes version"
  152. fi
  153. local -r json_file="${gcs_build_path}/started.json"
  154. for upload_attempt in {1..3}; do
  155. echo "Uploading version to: ${json_file} (attempt ${upload_attempt})"
  156. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" <(print_started) "${json_file}" || continue
  157. break
  158. done
  159. }
  160. #########################################################################
  161. # Maintain a single file storing the full build version, Jenkins' job number
  162. # build state. Limit its size so it does not grow unbounded.
  163. # This is primarily used for and by the
  164. # github.com/kubernetes/release/find_green_build tool.
  165. # @param build_result - the state of the build
  166. #
  167. function update_job_result_cache() {
  168. local -r build_result=$1
  169. local -r version=$(find_version)
  170. local -r job_results=${gcs_job_path}/jobResultsCache.json
  171. local -r tmp_results="${WORKSPACE}/_tmp/jobResultsCache.tmp"
  172. # TODO: This constraint is insufficient. The boundary for secondary
  173. # job cache should be date based on the last primary build.
  174. # The issue is we are trying to find a matched green set of results
  175. # at a given hash, but all of the jobs run at wildly different lengths.
  176. local -r cache_size=300
  177. local upload_attempt
  178. if [[ -n "${version}" ]]; then
  179. echo "Found Kubernetes version: ${version}"
  180. else
  181. echo "Could not find Kubernetes version"
  182. fi
  183. mkdir -p "${tmp_results%/*}"
  184. # Construct a valid json file
  185. echo "[" > "${tmp_results}"
  186. for upload_attempt in $(seq 3); do
  187. echo "Copying ${job_results} to ${tmp_results} (attempt ${upload_attempt})"
  188. # The sed construct below is stripping out only the "version" lines
  189. # and then ensuring there's a single comma at the end of the line.
  190. gsutil -q cat "${job_results}" 2>&- |\
  191. sed -n 's/^\({"version".*}\),*/\1,/p' |\
  192. tail -${cache_size} >> "${tmp_results}" || continue
  193. break
  194. done
  195. echo "{\"version\": \"${version}\", \"buildnumber\": \"${BUILD_NUMBER}\"," \
  196. "\"result\": \"${build_result}\"}" >> "${tmp_results}"
  197. echo "]" >> "${tmp_results}"
  198. for upload_attempt in $(seq 3); do
  199. echo "Copying ${tmp_results} to ${job_results} (attempt ${upload_attempt})"
  200. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" \
  201. "${tmp_results}" "${job_results}" || continue
  202. break
  203. done
  204. rm -f "${tmp_results}"
  205. }
  206. function upload_artifacts_and_build_result() {
  207. local -r build_result=$1
  208. local upload_attempt
  209. echo -n 'Run finished at '; date -d "@${timestamp}"
  210. for upload_attempt in {1..3}; do
  211. echo "Uploading to ${gcs_build_path} (attempt ${upload_attempt})"
  212. echo "Uploading build result: ${build_result}"
  213. gsutil -q -h "Content-Type:application/json" cp -a "${gcs_acl}" <(
  214. echo "{"
  215. echo " \"result\": \"${build_result}\","
  216. echo " \"timestamp\": ${timestamp}"
  217. echo "}"
  218. ) "${gcs_build_path}/finished.json" || continue
  219. if [[ -d "${artifacts_path}" && -n $(ls -A "${artifacts_path}") ]]; then
  220. echo "Uploading artifacts"
  221. gsutil -m -q -o "GSUtil:use_magicfile=True" cp -a "${gcs_acl}" -r -c \
  222. -z log,txt,xml "${artifacts_path}" "${gcs_build_path}/artifacts" || continue
  223. fi
  224. if [[ -e "${WORKSPACE}/build-log.txt" ]]; then
  225. echo "Uploading build log"
  226. gsutil -q cp -Z -a "${gcs_acl}" "${WORKSPACE}/build-log.txt" "${gcs_build_path}"
  227. fi
  228. # For pull jobs, keep a canonical ordering for tools that want to examine
  229. # the output.
  230. if [[ "${gcs_indirect_path}" != "" ]]; then
  231. echo "Writing ${gcs_build_path} to ${gcs_indirect_path}/${BUILD_NUMBER}.txt"
  232. echo "${gcs_build_path}" | \
  233. gsutil -q -h "Content-Type:text/plain" \
  234. cp -a "${gcs_acl}" - "${gcs_indirect_path}/${BUILD_NUMBER}.txt" || continue
  235. echo "Marking build ${BUILD_NUMBER} as the latest completed build for this PR"
  236. echo "${BUILD_NUMBER}" | \
  237. gsutil -q -h "Content-Type:text/plain" -h "Cache-Control:private, max-age=0, no-transform" \
  238. cp -a "${gcs_acl}" - "${gcs_job_path}/latest-build.txt" || continue
  239. fi
  240. # Mark this build as the latest completed.
  241. echo "Marking build ${BUILD_NUMBER} as the latest completed build"
  242. echo "${BUILD_NUMBER}" | \
  243. gsutil -q -h "Content-Type:text/plain" -h "Cache-Control:private, max-age=0, no-transform" \
  244. cp -a "${gcs_acl}" - "${gcs_latest_path}/latest-build.txt" || continue
  245. break # all uploads succeeded if we hit this point
  246. done
  247. echo -e "\n\n\n*** View logs and artifacts at ${results_url} ***\n\n"
  248. }
  249. if [[ -z "${BOOTSTRAP_MIGRATION:-}" ]]; then
  250. if [[ -n "${JENKINS_BUILD_STARTED:-}" ]]; then
  251. upload_version
  252. elif [[ -n "${JENKINS_BUILD_FINISHED:-}" ]]; then
  253. upload_artifacts_and_build_result "${JENKINS_BUILD_FINISHED}"
  254. update_job_result_cache "${JENKINS_BUILD_FINISHED}"
  255. else
  256. echo "ERROR: Called without JENKINS_BUILD_STARTED or JENKINS_BUILD_FINISHED set."
  257. echo "ERROR: this should not happen"
  258. exit 1
  259. fi
  260. fi