Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | # adapted from the hack scripts in kubernetes/kubernetes |
| 2 | |
| 3 | GIT_COMMIT:=$(shell git rev-parse "HEAD^{commit}" 2>/dev/null) |
| 4 | |
| 5 | # the raw git version from `git describe` -- our starting point |
| 6 | GIT_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 |
| 10 | DASHES_IN_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/[^-]//g") |
| 11 | |
| 12 | # just use the raw version by default |
| 13 | GIT_VERSION:=$(GIT_VERSION_RAW) |
| 14 | |
| 15 | ifeq ($(DASHES_IN_VERSION), ---) |
| 16 | # we have a distance to a subversion (v1.1.0-subversion-1-gCommitHash) |
| 17 | GIT_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$$/.\1\+\2/") |
| 18 | endif |
| 19 | ifeq ($(DASHES_IN_VERSION), --) |
| 20 | # we have distance to base tag (v1.1.0-1-gCommitHash) |
| 21 | GIT_VERSION:=$(shell echo "$(GIT_VERSION_RAW)" | sed "s/-g\([0-9a-f]\{14\}\)$$/+\1/") |
| 22 | endif |
| 23 | |
| 24 | # figure out if we have new or changed files |
| 25 | ifeq ($(shell git status --porcelain 2>/dev/null), "") |
| 26 | GIT_TREE_STATE:=clean |
| 27 | else |
| 28 | # append the -dirty manually, since `git describe --dirty` only considers |
| 29 | # changes to existing files |
| 30 | GIT_TREE_STATE:=dirty |
| 31 | GIT_VERSION:=$(GIT_VERSION)-dirty |
| 32 | endif |
| 33 | |
| 34 | # construct a "shorter" version without the commit info, etc for use as container image tag, etc |
| 35 | VERSION?=$(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 |
| 39 | ifdef SOURCE_DATE_EPOCH |
| 40 | BUILD_DATE:=$(shell date --date=@${SOURCE_DATE_EPOCH} -u +'%Y-%m-%dT%H:%M:%SZ') |
| 41 | else |
| 42 | BUILD_DATE:=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') |
| 43 | endif |
| 44 | |
| 45 | # set the build information version ldflags (but not other ldflags) |
| 46 | VERSION_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 | |
| 48 | export VERSION |
| 49 | export VERSION_LDFLAGS |
| 50 | |
| 51 | # print out a summary of the current version info |
| 52 | version-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 |