diff --git a/.goreleaser.yaml b/.goreleaser.yaml index cbd1deb36f..c295ef6447 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -15,6 +15,8 @@ builds: - darwin ldflags: - -s -w -X main.version={{.Version}} + - -s -w -X main.commit={{.ShortCommit}} + - -s -w -X main.Date={{.CommitDate}} nfpms: - file_name_template: '{{ .ProjectName }}_{{ .Arch }}' diff --git a/Makefile b/Makefile index deae8e9f7e..9f0e013a0e 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ all: tidy add-copyright lint cover build build: @echo "$(shell go version)" @echo "===========> Building binary $(BUILDAPP) *[Git Info]: $(VERSION)-$(GIT_COMMIT)" - @export CGO_ENABLED=0 && go build -o $(BUILDAPP) -ldflags '-s -w' $(BUILDFILE) + @export CGO_ENABLED=0 && go build -o $(BUILDAPP) -ldflags "-s -w -X main.version=dev -X main.commit=$$(git rev-parse --short HEAD) -X main.date=$$(date +%FT%TZ)" $(BUILDFILE) ## tidy: tidy go.mod .PHONY: tidy diff --git a/cmd/root.go b/cmd/root.go index e9b5098f2b..fae9f05c05 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,7 +34,9 @@ var ( cfgFile string kubecontext string kubeconfig string - version string + Version string + Commit string + Date string ) // rootCmd represents the base command when called without any subcommands @@ -49,8 +51,10 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute(v string) { - version = v +func Execute(v string, c string, d string) { + Version = v + Commit = c + Date = d err := rootCmd.Execute() if err != nil { os.Exit(1) diff --git a/cmd/version.go b/cmd/version.go index f0b65f54ed..93a49940dc 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -14,6 +14,9 @@ limitations under the License. package cmd import ( + "fmt" + "runtime/debug" + "github.com/spf13/cobra" ) @@ -23,7 +26,21 @@ var versionCmd = &cobra.Command{ Short: "Print the version number of k8sgpt", Long: `All software has versions. This is k8sgpt's`, Run: func(cmd *cobra.Command, args []string) { - cmd.Printf("k8sgpt version %s\n", version) + if Version == "dev" { + details, ok := debug.ReadBuildInfo() + if ok && details.Main.Version != "" && details.Main.Version != "(devel)" { + Version = details.Main.Version + for _, i := range details.Settings { + if i.Key == "vcs.time" { + Date = i.Value + } + if i.Key == "vcs.revision" { + Commit = i.Value + } + } + } + } + fmt.Printf("ks8gpt: %s (%s), built at: %s\n", Version, Commit, Date) }, } diff --git a/container/Dockerfile b/container/Dockerfile index d805eb6f30..be91f9d7f9 100644 --- a/container/Dockerfile +++ b/container/Dockerfile @@ -12,7 +12,9 @@ FROM golang:1.20.4-alpine3.16 AS builder ENV CGO_ENABLED=0 - +ARG VERSION +ARG COMMIT +ARG DATE WORKDIR /workspace COPY go.mod go.sum ./ @@ -20,7 +22,7 @@ RUN go mod download COPY ./ ./ -RUN go build -o /workspace/k8sgpt ./ +RUN go build -o /workspace/k8sgpt -ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}" ./ FROM gcr.io/distroless/static AS production diff --git a/main.go b/main.go index 2517284e06..b177e9b15b 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,12 @@ package main import "github.com/k8sgpt-ai/k8sgpt/cmd" -var version = "dev" +var ( + version = "dev" + commit = "HEAD" + date = "unknown" +) func main() { - cmd.Execute(version) + cmd.Execute(version, commit, date) }