From 927a7980e7c6851e3abab314b5f2eeb98fca959d Mon Sep 17 00:00:00 2001 From: Lumian Zhang <84360903+Cirilla-zmh@users.noreply.github.com> Date: Tue, 3 Dec 2024 17:43:14 +0800 Subject: [PATCH] automatically write version info every time release (#245) --- Makefile | 39 +++++++++++++++++---------------- go.sum | 6 ++--- pkg/inst-api/version/version.go | 2 +- tool/config/config.go | 2 +- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 7fcb686e..e4988874 100644 --- a/Makefile +++ b/Makefile @@ -14,15 +14,17 @@ #------------------------------------------------------------------------------- # General build options +# Change this parameter while releasing +MAIN_VERSION := 0.4.0 + CURRENT_OS := $(shell uname -s | tr '[:upper:]' '[:lower:]') CURRENT_ARCH := $(shell uname -m) MOD_NAME := github.com/alibaba/opentelemetry-go-auto-instrumentation -VERSION := $(MAIN_VERSION)_$(COMMIT_ID) -XVERSION := -X=$(MOD_NAME)/tool/config.ToolVersion=$(VERSION) STRIP_DEBUG := -s -w LDFLAGS := $(XVERSION) $(STRIP_DEBUG) -BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -ldflags="$(LDFLAGS)" -o $(3) ./tool/cmd +# default build cmd without ldflags +BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -o $(3) ./tool/cmd OUTPUT_BASE = otel OUTPUT_DARWIN_AMD64 = $(OUTPUT_BASE)-darwin-amd64 @@ -31,6 +33,21 @@ OUTPUT_WINDOWS_AMD64 = $(OUTPUT_BASE)-windows-amd64.exe OUTPUT_DARWIN_ARM64 = $(OUTPUT_BASE)-darwin-arm64 OUTPUT_LINUX_ARM64 = $(OUTPUT_BASE)-linux-arm64 +#------------------------------------------------------------------------------- +# Prepare version +# Get the current Git commit ID +CHECK_GIT_DIRECTORY := $(if $(wildcard .git),true,false) +ifeq ($(CHECK_GIT_DIRECTORY),true) + COMMIT_ID := $(shell git rev-parse --short HEAD) +else + COMMIT_ID := default +endif + +VERSION := $(MAIN_VERSION)_$(COMMIT_ID) +XVERSION := -X=$(MOD_NAME)/tool/config.ToolVersion=$(VERSION) -X=$(MOD_NAME)/pkg/inst-api/version.Tag=v$(VERSION) +LDFLAGS := $(XVERSION) $(STRIP_DEBUG) +BUILD_CMD = CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -a -ldflags="$(LDFLAGS)" -o $(3) ./tool/cmd + #------------------------------------------------------------------------------- # Multiple OS and ARCH support ifeq ($(CURRENT_ARCH), x86_64) @@ -46,22 +63,6 @@ ifeq ($(findstring msys,$(CURRENT_OS)),msys) CURRENT_OS := windows endif -# Get the current Git commit ID -CHECK_GIT_DIRECTORY := $(if $(wildcard .git),true,false) -ifeq ($(CHECK_GIT_DIRECTORY),true) - BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD) - ifeq ($(findstring /,$(BRANCH_NAME)),/) - MAIN_VERSION := $(shell echo $(BRANCH_NAME) | cut -d'/' -f2-) - else - MAIN_VERSION := 1.0.0 - endif - COMMIT_ID := $(shell git rev-parse --short HEAD) -else - BRANCH_NAME := 1.0.0 - MAIN_VERSION := 1.0.0 - COMMIT_ID := default -endif - #------------------------------------------------------------------------------- # Build targets .PHONY: build diff --git a/go.sum b/go.sum index a6c544f5..7f90c76c 100644 --- a/go.sum +++ b/go.sum @@ -70,11 +70,9 @@ github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/elastic/elastic-transport-go/v8 v8.0.0-alpha h1:SW9xcMVxx4Nv9oRm5rQxzAMAatwiZV8xROP2a48y45Q= -github.com/elastic/elastic-transport-go/v8 v8.0.0-alpha/go.mod h1:87Tcz8IVNe6rVSLdBux1o/PEItLtyabHU3naC7IoqKI= +github.com/elastic/elastic-transport-go/v8 v8.1.0 h1:NeqEz1ty4RQz+TVbUrpSU7pZ48XkzGWQj02k5koahIE= github.com/elastic/elastic-transport-go/v8 v8.1.0/go.mod h1:87Tcz8IVNe6rVSLdBux1o/PEItLtyabHU3naC7IoqKI= -github.com/elastic/go-elasticsearch/v8 v8.0.0 h1:Hte+pgoEZI88j/sQx7u9vK9SqisvJYkYMmxDnQXiJyM= -github.com/elastic/go-elasticsearch/v8 v8.0.0/go.mod h1:8NCWP26meGbncX+R9sxo2JD8IqBjRTuS7yXMstHpd40= +github.com/elastic/go-elasticsearch/v8 v8.4.0 h1:Rn1mcqaIMcNT43hnx2H62cIFZ+B6mjWtzj85BDKrvCE= github.com/elastic/go-elasticsearch/v8 v8.4.0/go.mod h1:yY52i2Vj0unLz+N3Nwx1gM5LXwoj3h2dgptNGBYkMLA= github.com/envoyproxy/go-control-plane v0.13.0 h1:HzkeUz1Knt+3bK+8LG1bxOO/jzWZmdxpwC51i202les= github.com/envoyproxy/go-control-plane v0.13.0/go.mod h1:GRaKG3dwvFoTg4nj7aXdZnvMg4d7nvT/wl9WgVXn3Q8= diff --git a/pkg/inst-api/version/version.go b/pkg/inst-api/version/version.go index fad2d72e..887e840b 100644 --- a/pkg/inst-api/version/version.go +++ b/pkg/inst-api/version/version.go @@ -15,4 +15,4 @@ package version // Tag specifies the current release tag. It needs to be manually updated. -const Tag = "v0.4.0" +var Tag = "v0.4.0" diff --git a/tool/config/config.go b/tool/config/config.go index 7052d6a0..ee9f42a7 100644 --- a/tool/config/config.go +++ b/tool/config/config.go @@ -236,7 +236,7 @@ func InitConfig() (err error) { } func PrintVersion() error { - fmt.Printf("%s version %s", GetToolName(), ToolVersion) + fmt.Printf("%s version %s\n", GetToolName(), ToolVersion) os.Exit(0) return nil }