diff --git a/Makefile b/Makefile index 314f7fdba..e4150ace5 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,18 @@ NETFLOW_GENERATOR=nflow-generator CMD_DIR=./cmd/ FLP_CONF_FILE ?= contrib/kubernetes/flowlogs-pipeline.conf.yaml +BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M) +TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1) +TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true) +COMMIT := $(shell git rev-parse --short HEAD) +BUILD_VERSION := $(TAG:v%=%) +ifneq ($(COMMIT), $(TAG_COMMIT)) + BUILD_VERSION := $(BUILD_VERSION)-$(COMMIT) +endif +ifneq ($(shell git status --porcelain),) + BUILD_VERSION := $(BUILD_VERSION)-dirty +endif + .DEFAULT_GOAL := help FORCE: ; @@ -59,7 +71,7 @@ lint: $(GOLANGCI_LINT) ## Lint the code .PHONY: build_code build_code: validate_go lint @go mod vendor - VERSION=$$(date); go build -ldflags "-X 'main.Version=$$VERSION'" "${CMD_DIR}${FLP_BIN_FILE}" + go build -ldflags "-X 'main.BuildVersion=$(BUILD_VERSION)' -X 'main.BuildDate=$(BUILD_DATE)'" "${CMD_DIR}${FLP_BIN_FILE}" .PHONY: build build: build_code docs ## Build flowlogs-pipeline executable and update the docs diff --git a/cmd/flowlogs-pipeline/main.go b/cmd/flowlogs-pipeline/main.go index 89b6d99f3..f904ad6f1 100644 --- a/cmd/flowlogs-pipeline/main.go +++ b/cmd/flowlogs-pipeline/main.go @@ -36,7 +36,8 @@ import ( ) var ( - Version string + BuildVersion string + BuildDate string cfgFile string logLevel string envPrefix = "FLOWLOGS-PIPILNE" @@ -163,7 +164,8 @@ func run() { ) // Initial log message - fmt.Printf("%s starting - version [%s]\n\n", filepath.Base(os.Args[0]), Version) + fmt.Printf("Starting %s:\n=====\nBuild Version: %s\nBuild Date: %s\n\n", + filepath.Base(os.Args[0]), BuildVersion, BuildDate) // Dump configuration dumpConfig() diff --git a/contrib/docker/Dockerfile b/contrib/docker/Dockerfile index 4b8874fa2..042623440 100644 --- a/contrib/docker/Dockerfile +++ b/contrib/docker/Dockerfile @@ -6,14 +6,17 @@ WORKDIR /app COPY go.mod . COPY go.sum . -# Download modules -RUN go mod download - COPY cmd/ cmd/ COPY pkg/ pkg/ +COPY .bingo/ .bingo/ +COPY .git/ .git/ +COPY Makefile Makefile + +# Download modules +RUN go mod download +RUN go mod vendor -# Build -RUN VERSION=$(date); CGO_ENABLED=0 GOOS=linux GO111MODULE=on GOARCH=amd64 go build -ldflags "-X 'main.Version=$VERSION'" ./cmd/flowlogs-pipeline +RUN make build_code # final stage FROM ubuntu