forked from tongdun/td-redis-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.sh
executable file
·94 lines (77 loc) · 3 KB
/
version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
# copy from github.com/kubernetes/kubernetes/hack/lib/version.sh
set -o errexit
set -o nounset
set -o pipefail
ROOT=$(dirname "${BASH_SOURCE[0]}")/..
GO_PACKAGE="redis-priv-operator"
getVersion() {
if [[ '$Format:%%$' == "%" ]]; then
GIT_COMMIT='$Format:%H$'
GIT_TREE_STATE="archive"
# When a 'git archive' is exported, the '$Format:%D$' below will look
# something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
# can be extracted from it.
if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
GIT_VERSION="${BASH_REMATCH[1]}"
fi
fi
local git=(git --work-tree "${ROOT}")
if [[ -n ${GIT_COMMIT-} ]] || GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
if [[ -z ${GIT_TREE_STATE-} ]]; then
# Check if the tree is dirty. default to dirty
if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
GIT_TREE_STATE="clean"
else
GIT_TREE_STATE="dirty"
fi
fi
# Use git describe to find the version based on tags.
if [[ -n ${GIT_VERSION-} ]] || GIT_VERSION=$("${git[@]}" describe --tags --abbrev=14 "${GIT_COMMIT}^{commit}" 2>/dev/null); then
# This translates the "git describe" to an actual semver.org
# compatible semantic version that looks something like this:
# v1.1.0-alpha.0.6+84c76d1142ea4d
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then
# git describe --dirty only considers changes to existing files, but
# that is problematic since new untracked .go files affect the build,
# so use our idea of "dirty" from git status instead.
GIT_VERSION+="-dirty"
fi
# If GIT_VERSION is not a valid Semantic Version, then refuse to build.
if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}"
echo "Please see more details here: https://semver.org"
exit 1
fi
fi
fi
}
ldflag() {
local key=${1}
local val=${2}
echo "-X '${GO_PACKAGE}/pkg/version.${key}=${val}'"
}
# Prints the value that needs to be passed to the -ldflags parameter of go build
# in order to set the Kubernetes based on the git tree status.
ldflags() {
getVersion
local buildDate=
[[ -z ${SOURCE_DATE_EPOCH-} ]] || buildDate="--date=@${SOURCE_DATE_EPOCH}"
local -a ldflags=($(ldflag "buildDate" "$(date ${buildDate} -u +'%Y-%m-%dT%H:%M:%SZ')"))
if [[ -n ${KUBE_GIT_COMMIT-} ]]; then
ldflags+=($(ldflag "gitCommit" "${GIT_COMMIT}"))
ldflags+=($(ldflag "gitTreeState" "${GIT_TREE_STATE}"))
fi
if [[ -n ${GIT_VERSION-} ]]; then
ldflags+=($(ldflag "gitVersion" "${GIT_VERSION}"))
fi
# The -ldflags parameter takes a single string, so join the output.
echo "${ldflags[*]-}"
}
if [[ -z ${1:-} ]]; then
ldflags
else
getVersion
echo ${!1}
fi