sigs.k8s.io/cluster-api@v1.7.1/hack/version.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2020 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 set -o errexit 17 set -o nounset 18 set -o pipefail 19 20 if [[ "${TRACE-0}" == "1" ]]; then 21 set -o xtrace 22 fi 23 24 version::get_version_vars() { 25 # shellcheck disable=SC1083 26 GIT_COMMIT="$(git rev-parse HEAD^{commit})" 27 28 if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then 29 GIT_TREE_STATE="clean" 30 else 31 GIT_TREE_STATE="dirty" 32 fi 33 34 # stolen from k8s.io/hack/lib/version.sh 35 # Use git describe to find the version based on annotated tags. 36 if [[ -n ${GIT_VERSION-} ]] || GIT_VERSION=$(git describe --abbrev=14 --match "v[0-9]*" 2>/dev/null); then 37 # This translates the "git describe" to an actual semver.org 38 # compatible semantic version that looks something like this: 39 # v1.1.0-alpha.0.6+84c76d1142ea4d 40 # 41 # TODO: We continue calling this "git version" because so many 42 # downstream consumers are expecting it there. 43 # shellcheck disable=SC2001 44 DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g") 45 if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then 46 # We have distance to subversion (v1.1.0-subversion-1-gCommitHash) 47 # shellcheck disable=SC2001 48 GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\-\2/") 49 elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then 50 # We have distance to base tag (v1.1.0-1-gCommitHash) 51 # shellcheck disable=SC2001 52 GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/-\1/") 53 fi 54 if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then 55 # git describe --dirty only considers changes to existing files, but 56 # that is problematic since new untracked .go files affect the build, 57 # so use our idea of "dirty" from git status instead. 58 GIT_VERSION+="-dirty" 59 fi 60 61 62 # Try to match the "git describe" output to a regex to try to extract 63 # the "major" and "minor" versions and whether this is the exact tagged 64 # version or whether the tree is between two tagged versions. 65 if [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then 66 GIT_MAJOR=${BASH_REMATCH[1]} 67 GIT_MINOR=${BASH_REMATCH[2]} 68 fi 69 70 # If GIT_VERSION is not a valid Semantic Version, then refuse to build. 71 if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then 72 echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}" 73 echo "Please see more details here: https://semver.org" 74 exit 1 75 fi 76 fi 77 78 GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags) 79 GIT_RELEASE_COMMIT=$(git rev-list -n 1 "${GIT_RELEASE_TAG}") 80 } 81 82 # stolen from k8s.io/hack/lib/version.sh and modified 83 # Prints the value that needs to be passed to the -ldflags parameter of go build 84 version::ldflags() { 85 version::get_version_vars 86 87 local -a ldflags 88 function add_ldflag() { 89 local key=${1} 90 local val=${2} 91 ldflags+=( 92 "-X 'sigs.k8s.io/cluster-api/version.${key}=${val}'" 93 ) 94 } 95 96 add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')" 97 add_ldflag "gitCommit" "${GIT_COMMIT}" 98 add_ldflag "gitTreeState" "${GIT_TREE_STATE}" 99 add_ldflag "gitMajor" "${GIT_MAJOR}" 100 add_ldflag "gitMinor" "${GIT_MINOR}" 101 add_ldflag "gitVersion" "${GIT_VERSION}" 102 add_ldflag "gitReleaseCommit" "${GIT_RELEASE_COMMIT}" 103 104 # The -ldflags parameter takes a single string, so join the output. 105 echo "${ldflags[*]-}" 106 } 107 108 version::ldflags