ソースを参照

Set LD_FLAG version from git tag. Displayed version will be (default behaviour of git describe —tags):
* release version : if tag `v0.1.2` is exactly on the built commit : `0.1.2` (strip v when displaying `docker version`)
* dev version : if tag `v0.1.2` is 3 commits before, and we are on commit `xyz` : `0.1.2-3-xyz`
* will only match tags starting with `v[0-9]`

Guillaume Tardif 5 年 前
コミット
0bcba8d1d2
2 ファイル変更10 行追加7 行削除
  1. 8 6
      builder.Makefile
  2. 2 1
      cli/cmd/version.go

+ 8 - 6
builder.Makefile

@@ -23,7 +23,9 @@ ifeq ($(GOOS),windows)
 endif
 
 STATIC_FLAGS=CGO_ENABLED=0
-LDFLAGS:="-s -w"
+GIT_TAG=$(shell git describe --tags --match "v[0-9]*")
+
+LDFLAGS="-s -w -X main.version=${GIT_TAG}"
 GO_BUILD=$(STATIC_FLAGS) go build -trimpath -ldflags=$(LDFLAGS)
 
 BINARY?=bin/docker
@@ -37,18 +39,18 @@ endif
 all: cli
 
 protos:
-	@protoc -I. --go_out=plugins=grpc,paths=source_relative:. ${PROTOS}
+	protoc -I. --go_out=plugins=grpc,paths=source_relative:. ${PROTOS}
 
 cli:
 	GOOS=${GOOS} GOARCH=${GOARCH} $(GO_BUILD) $(TAGS) -o $(BINARY_WITH_EXTENSION) ./cli
 
 cross:
-	@GOOS=linux   GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-linux-amd64 ./cli
-	@GOOS=darwin  GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-darwin-amd64 ./cli
-	@GOOS=windows GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-windows-amd64.exe ./cli
+	GOOS=linux   GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-linux-amd64 ./cli
+	GOOS=darwin  GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-darwin-amd64 ./cli
+	GOOS=windows GOARCH=amd64 $(GO_BUILD) $(TAGS) -o $(BINARY)-windows-amd64.exe ./cli
 
 test:
-	@go test $(TAGS) -cover $(shell go list ./... | grep -vE 'e2e')
+	go test $(TAGS) -cover $(shell go list ./... | grep -vE 'e2e')
 
 lint:
 	golangci-lint run --timeout 10m0s ./...

+ 2 - 1
cli/cmd/version.go

@@ -46,6 +46,7 @@ func VersionCommand(version string) *cobra.Command {
 }
 
 func runVersion(cmd *cobra.Command, version string) error {
+	displayedVersion := strings.TrimPrefix(version, "v")
 	versionResult, _ := mobycli.ExecSilent(cmd.Context())
 	// we don't want to fail on error, there is an error if the engine is not available but it displays client version info
 	// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
@@ -53,6 +54,6 @@ func runVersion(cmd *cobra.Command, version string) error {
 		return mobycli.ExecCmd(cmd)
 	}
 	var s string = string(versionResult)
-	fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration  "+version+"\n Version:", 1))
+	fmt.Print(strings.Replace(s, "\n Version:", "\n Azure integration  "+displayedVersion+"\n Version:", 1))
 	return nil
 }