version.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/bin/bash
  2. # Copyright 2014 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. # -----------------------------------------------------------------------------
  16. # Version management helpers. These functions help to set, save and load the
  17. # following variables:
  18. #
  19. # KRATOS_GIT_COMMIT - The git commit id corresponding to this
  20. # source code.
  21. # KRATOS_GIT_TREE_STATE - "clean" indicates no changes since the git commit id
  22. # "dirty" indicates source code changes after the git commit id
  23. # "archive" indicates the tree was produced by 'git archive'
  24. # KRATOS_GIT_VERSION - "vX.Y" used to indicate the last release version.
  25. # KRATOS_GIT_MAJOR - The major part of the version
  26. # KRATOS_GIT_MINOR - The minor component of the version
  27. # Grovels through git to set a set of env variables.
  28. #
  29. # If KRATOS_GIT_VERSION_FILE, this function will load from that file instead of
  30. # querying git.
  31. kratos::version::get_version_vars() {
  32. KRATOS_BUILD_TIME=`date +%Y.%m.%d-%H:%M:%S%Z`
  33. if [[ -n ${KRATOS_GIT_VERSION_FILE-} ]]; then
  34. kratos::version::load_version_vars "${KRATOS_GIT_VERSION_FILE}"
  35. return
  36. fi
  37. # If the kratosrnetes source was exported through git archive, then
  38. # we likely don't have a git tree, but these magic values may be filled in.
  39. if [[ '$Format:%%$' == "%" ]]; then
  40. KRATOS_GIT_COMMIT='$Format:%H$'
  41. KRATOS_GIT_TREE_STATE="archive"
  42. # When a 'git archive' is exported, the '$Format:%D$' below will look
  43. # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
  44. # can be extracted from it.
  45. if [[ '$Format:%D$' =~ tag:\ (v[^ ]+) ]]; then
  46. KRATOS_GIT_VERSION="${BASH_REMATCH[1]}"
  47. fi
  48. fi
  49. local git=(git --work-tree "${KRATOS_ROOT}")
  50. if [[ -n ${KRATOS_GIT_COMMIT-} ]] || KRATOS_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
  51. if [[ -z ${KRATOS_GIT_TREE_STATE-} ]]; then
  52. # Check if the tree is dirty. default to dirty
  53. if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
  54. KRATOS_GIT_TREE_STATE="clean"
  55. else
  56. KRATOS_GIT_TREE_STATE="dirty"
  57. fi
  58. fi
  59. # Use git describe to find the version based on tags.
  60. if [[ -n ${KRATOS_GIT_VERSION-} ]] || KRATOS_GIT_VERSION=$("${git[@]}" describe --tags --abbrev=14 "${KRATOS_GIT_COMMIT}^{commit}" 2>/dev/null); then
  61. # This translates the "git describe" to an actual semver.org
  62. # compatible semantic version that looks something like this:
  63. # v1.1.0-alpha.0.6+84c76d1142ea4d
  64. #
  65. # TODO: We continue calling this "git version" because so many
  66. # downstream consumers are expecting it there.
  67. DASHES_IN_VERSION=$(echo "${KRATOS_GIT_VERSION}" | sed "s/[^-]//g")
  68. if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
  69. # We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
  70. KRATOS_GIT_VERSION=$(echo "${KRATOS_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
  71. elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
  72. # We have distance to base tag (v1.1.0-1-gCommitHash)
  73. KRATOS_GIT_VERSION=$(echo "${KRATOS_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
  74. fi
  75. if [[ "${KRATOS_GIT_TREE_STATE}" == "dirty" ]]; then
  76. # git describe --dirty only considers changes to existing files, but
  77. # that is problematic since new untracked .go files affect the build,
  78. # so use our idea of "dirty" from git status instead.
  79. KRATOS_GIT_VERSION+="-dirty"
  80. fi
  81. # Try to match the "git describe" output to a regex to try to extract
  82. # the "major" and "minor" versions and whether this is the exact tagged
  83. # version or whether the tree is between two tagged versions.
  84. if [[ "${KRATOS_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
  85. KRATOS_GIT_MAJOR=${BASH_REMATCH[1]}
  86. KRATOS_GIT_MINOR=${BASH_REMATCH[2]}
  87. if [[ -n "${BASH_REMATCH[4]}" ]]; then
  88. KRATOS_GIT_MINOR+="+"
  89. fi
  90. fi
  91. # If KRATOS_GIT_VERSION is not a valid Semantic Version, then refuse to build.
  92. #if ! [[ "${KRATOS_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  93. # echo "KRATOS_GIT_VERSION should be a valid Semantic Version"
  94. # echo "Please see more details here: https://semver.org"
  95. # exit 1
  96. #fi
  97. fi
  98. fi
  99. }
  100. # Saves the environment flags to $1
  101. kratos::version::save_version_vars() {
  102. local version_file=${1-}
  103. [[ -n ${version_file} ]] || {
  104. echo "!!! Internal error. No file specified in kratos::version::save_version_vars"
  105. return 1
  106. }
  107. cat <<EOF >"${version_file}"
  108. KRATOS_GIT_COMMIT='${KRATOS_GIT_COMMIT-}'
  109. KRATOS_GIT_TREE_STATE='${KRATOS_GIT_TREE_STATE-}'
  110. KRATOS_GIT_VERSION='${KRATOS_GIT_VERSION-}'
  111. KRATOS_GIT_MAJOR='${KRATOS_GIT_MAJOR-}'
  112. KRATOS_GIT_MINOR='${KRATOS_GIT_MINOR-}'
  113. EOF
  114. }
  115. # Loads up the version variables from file $1
  116. kratos::version::load_version_vars() {
  117. local version_file=${1-}
  118. [[ -n ${version_file} ]] || {
  119. echo "!!! Internal error. No file specified in kratos::version::load_version_vars"
  120. return 1
  121. }
  122. source "${version_file}"
  123. }
  124. kratos::version::ldflag() {
  125. local key=${1}
  126. local val=${2}
  127. # If you update these, also update the list pkg/version/def.bzl.
  128. echo "-X '${KRATOS_GO_PACKAGE}/pkg/version.${key}=${val}'"
  129. echo "-X '${KRATOS_GO_PACKAGE}/vendor/k8s.io/client-go/pkg/version.${key}=${val}'"
  130. }
  131. # Prints the value that needs to be passed to the -ldflags parameter of go build
  132. # in order to set the Kubernetes based on the git tree status.
  133. # IMPORTANT: if you update any of these, also update the lists in
  134. # pkg/version/def.bzl and .build/print-workspace-status.sh.
  135. kratos::version::ldflags() {
  136. kratos::version::get_version_vars
  137. local buildDate=
  138. [[ -z ${SOURCE_DATE_EPOCH-} ]] || buildDate="--date=@${SOURCE_DATE_EPOCH}"
  139. local -a ldflags=($(kratos::version::ldflag "buildDate" "$(date ${buildDate} -u +'%Y-%m-%dT%H:%M:%SZ')"))
  140. if [[ -n ${KRATOS_GIT_COMMIT-} ]]; then
  141. ldflags+=($(kratos::version::ldflag "gitCommit" "${KRATOS_GIT_COMMIT}"))
  142. ldflags+=($(kratos::version::ldflag "gitTreeState" "${KRATOS_GIT_TREE_STATE}"))
  143. fi
  144. if [[ -n ${KRATOS_GIT_VERSION-} ]]; then
  145. ldflags+=($(kratos::version::ldflag "gitVersion" "${KRATOS_GIT_VERSION}"))
  146. fi
  147. if [[ -n ${KRATOS_GIT_MAJOR-} && -n ${KRATOS_GIT_MINOR-} ]]; then
  148. ldflags+=(
  149. $(kratos::version::ldflag "gitMajor" "${KRATOS_GIT_MAJOR}")
  150. $(kratos::version::ldflag "gitMinor" "${KRATOS_GIT_MINOR}")
  151. )
  152. fi
  153. # The -ldflags parameter takes a single string, so join the output.
  154. echo "${ldflags[*]-}"
  155. }