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