From 4d73d8ff4799fc3e3b3fcaf41bc3471de168305e Mon Sep 17 00:00:00 2001 From: Ramkumar Chinchani Date: Mon, 16 Sep 2019 11:01:59 -0700 Subject: [PATCH] version: add commit id to binary during build --- Makefile | 6 ++++-- pkg/api/config.go | 5 +++++ pkg/cli/root.go | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7028a77e..ab8c3388 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ export GO111MODULE=on TOP_LEVEL=$(shell git rev-parse --show-toplevel) +COMMIT_HASH=$(shell git describe --always --tags --long) +COMMIT=$(if $(shell git status --porcelain --untracked-files=no),$(COMMIT_HASH)-dirty,$(COMMIT_HASH)) CONTAINER_RUNTIME := $(shell command -v podman 2> /dev/null || echo docker) PATH := bin:$(PATH) @@ -8,11 +10,11 @@ all: doc binary debug test check .PHONY: binary binary: doc - go build -v -o bin/zot -tags=jsoniter ./cmd/zot + go build -v -ldflags "-X github.com/anuvu/zot/pkg/api.Commit=${COMMIT}" -o bin/zot -tags=jsoniter ./cmd/zot .PHONY: debug debug: doc - go build -v -gcflags all='-N -l' -o bin/zot-debug -tags=jsoniter ./cmd/zot + go build -v -gcflags all='-N -l' -ldflags "-X github.com/anuvu/zot/pkg/api.Commit=${COMMIT}" -o bin/zot-debug -tags=jsoniter ./cmd/zot .PHONY: test test: diff --git a/pkg/api/config.go b/pkg/api/config.go index cbb6ffd3..3889ea6c 100644 --- a/pkg/api/config.go +++ b/pkg/api/config.go @@ -4,6 +4,9 @@ import ( dspec "github.com/opencontainers/distribution-spec" ) +//nolint (gochecknoglobals) +var Commit string + type StorageConfig struct { RootDirectory string } @@ -39,6 +42,7 @@ type LogConfig struct { type Config struct { Version string + Commit string Storage StorageConfig HTTP HTTPConfig Log LogConfig `mapstructure:",omitempty"` @@ -47,6 +51,7 @@ type Config struct { func NewConfig() *Config { return &Config{ Version: dspec.Version, + Commit: Commit, HTTP: HTTPConfig{Address: "127.0.0.1", Port: "8080"}, Log: LogConfig{Level: "debug"}, } diff --git a/pkg/cli/root.go b/pkg/cli/root.go index f871d173..f6579a6f 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -85,7 +85,7 @@ func NewRootCmd() *cobra.Command { Long: "`zot`", Run: func(cmd *cobra.Command, args []string) { if showVersion { - log.Info().Str("version", dspec.Version).Msg("distribution-spec") + log.Info().Str("distribution-spec", dspec.Version).Str("commit", api.Commit).Msg("version") } _ = cmd.Usage() },