blob: bdcfaf0698fe8e9970ba348c71136a12c03a276b [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001# adapted from the hack scripts in kubernetes/kubernetes
2
3GIT_COMMIT:=$(shell git rev-parse "HEAD^{commit}" 2>/dev/null)
4
5# the raw git version from `git describe` -- our starting point
6GIT_VERSION_RAW:=$(shell git describe --tags --abbrev=14 "$(GIT_COMMIT)^{commit}" 2>/dev/null)
7
8# use the number of dashes in the raw version to figure out what kind of
9# version this is, and turn it into a semver-compatible version
10DASHES_IN_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/[^-]//g")
11
12# just use the raw version by default
13GIT_VERSION:=$(GIT_VERSION_RAW)
14
15ifeq ($(DASHES_IN_VERSION), ---)
16# we have a distance to a subversion (v1.1.0-subversion-1-gCommitHash)
17GIT_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$$/.\1\+\2/")
18endif
19ifeq ($(DASHES_IN_VERSION), --)
20# we have distance to base tag (v1.1.0-1-gCommitHash)
21GIT_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/-g\([0-9a-f]\{14\}\)$$/+\1/")
22endif
23
24# figure out if we have new or changed files
25ifeq ($(shell git status --porcelain 2>/dev/null), "")
26GIT_TREE_STATE:=clean
27else
28# append the -dirty manually, since `git describe --dirty` only considers
29# changes to existing files
30GIT_TREE_STATE:=dirty
31GIT_VERSION:=$(GIT_VERSION)-dirty
32endif
33
34# construct a "shorter" version without the commit info, etc for use as container image tag, etc
35VERSION?=$(shell echo "$(GIT_VERSION)" | grep -E -o '^v[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+(-(alpha|beta)\.[[:digit:]]+)?')
36
37# construct the build date, taking into account SOURCE_DATE_EPOCH, which is
38# used for the purpose of reproducible builds
39ifdef SOURCE_DATE_EPOCH
40BUILD_DATE:=$(shell date --date=@${SOURCE_DATE_EPOCH} -u +'%Y-%m-%dT%H:%M:%SZ')
41else
42BUILD_DATE:=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
43endif
44
45# set the build information version ldflags (but not other ldflags)
46VERSION_LDFLAGS:=-X github.com/kubernetes-incubator/metrics-server/pkg/version.gitVersion=$(GIT_VERSION) -X github.com/kubernetes-incubator/metrics-server/pkg/version.gitCommit=$(GIT_COMMIT) -X github.com/kubernetes-incubator/metrics-server/pkg/version.gitTreeState=$(GIT_TREE_STATE) -X github.com/kubernetes-incubator/metrics-server/pkg/version.buildDate=$(BUILD_DATE)
47
48export VERSION
49export VERSION_LDFLAGS
50
51# print out a summary of the current version info
52version-info:
53 @echo "Version: $(GIT_VERSION) ($(VERSION))"
54 @echo " built from $(GIT_COMMIT) ($(GIT_TREE_STATE))"
55 @echo " built on $(BUILD_DATE)"
56.PHONY: version-info