Skip to content

Commit a826ef8

Browse files
authored
Makefile,version,cmd/operator-sdk/version: add git compile-time variables (#1431)
* Makefile,version,cmd/operator-sdk/version: add compile-time variables for git version and commit * release.sh: new format for Version in version.go
1 parent ff4dfb7 commit a826ef8

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ else
1010
endif
1111

1212
VERSION = $(shell git describe --dirty --tags --always)
13+
GIT_COMMIT = $(shell git rev-parse HEAD)
1314
REPO = github.com/operator-framework/operator-sdk
1415
BUILD_PATH = $(REPO)/cmd/operator-sdk
1516
PKGS = $(shell go list ./... | grep -v /vendor/)
@@ -42,7 +43,14 @@ clean:
4243
.PHONY: all test format dep clean
4344

4445
install:
45-
$(Q)go install -gcflags "all=-trimpath=${GOPATH}" -asmflags "all=-trimpath=${GOPATH}" $(BUILD_PATH)
46+
$(Q)go install \
47+
-gcflags "all=-trimpath=${GOPATH}" \
48+
-asmflags "all=-trimpath=${GOPATH}" \
49+
-ldflags " \
50+
-X '${REPO}/version.GitVersion=${VERSION}' \
51+
-X '${REPO}/version.GitCommit=${GIT_COMMIT}' \
52+
" \
53+
$(BUILD_PATH)
4654

4755
release_x86_64 := \
4856
build/operator-sdk-$(VERSION)-x86_64-linux-gnu \
@@ -54,7 +62,14 @@ build/operator-sdk-%-x86_64-linux-gnu: GOARGS = GOOS=linux GOARCH=amd64
5462
build/operator-sdk-%-x86_64-apple-darwin: GOARGS = GOOS=darwin GOARCH=amd64
5563

5664
build/%: $(SOURCES)
57-
$(Q)$(GOARGS) go build -gcflags "all=-trimpath=${GOPATH}" -asmflags "all=-trimpath=${GOPATH}" -o $@ $(BUILD_PATH)
65+
$(Q)$(GOARGS) go build \
66+
-gcflags "all=-trimpath=${GOPATH}" \
67+
-asmflags "all=-trimpath=${GOPATH}" \
68+
-ldflags " \
69+
-X '${REPO}/version.GitVersion=${VERSION}' \
70+
-X '${REPO}/version.GitCommit=${GIT_COMMIT}' \
71+
" \
72+
-o $@ $(BUILD_PATH)
5873

5974
build/%.asc:
6075
$(Q){ \

cmd/operator-sdk/version/cmd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ func NewCmd() *cobra.Command {
2727
Use: "version",
2828
Short: "Prints the version of operator-sdk",
2929
Run: func(cmd *cobra.Command, args []string) {
30-
fmt.Println("operator-sdk version:", ver.Version)
30+
version := ver.GitVersion
31+
if version == "unknown" {
32+
version = ver.Version
33+
}
34+
fmt.Printf("operator-sdk version: %s, commit: %s\n", version, ver.GitCommit)
3135
},
3236
}
3337
return versionCmd

release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fi
3131

3232
# Detect whether versions in code were updated.
3333
VER_FILE="version/version.go"
34-
CURR_VER="$(sed -nr 's|Version = "(.+)"|\1|p' "$VER_FILE" | tr -d ' \t\n')"
34+
CURR_VER="$(sed -nr 's|\s+Version\s+= "(.+)"|\1|p' "$VER_FILE" | tr -d ' \t\n')"
3535
if [[ "$VER" != "$CURR_VER" ]]; then
3636
echo "version is not set correctly in $VER_FILE"
3737
exit 1

version/version.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
package version
1616

1717
var (
18-
Version = "v0.7.0+git"
18+
Version = "v0.7.0+git"
19+
GitVersion = "unknown"
20+
GitCommit = "unknown"
1921
)

0 commit comments

Comments
 (0)