From a232165688c95cb40115724b4bd768ac1d2b1a8d Mon Sep 17 00:00:00 2001 From: Luke K Date: Wed, 8 Jul 2020 02:32:35 +0000 Subject: [PATCH] make build with version tags --- Makefile | 33 +++++++++++++++++++++++++++++++++ cmd/faas/main.go | 9 ++++++++- cmd/root.go | 2 +- cmd/version.go | 33 ++++++++++++++++++++++++++++----- 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..6eaa76833 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +REPO := boson-project/faas +BIN := faas + +CODE := $(shell find . -name '*.go') +DATE := $(shell date -u +"%Y%m%dT%H%M%SZ") +HASH := $(shell git rev-parse --short HEAD 2>/dev/null) +BRCH := $(shell git symbolic-ref --short -q HEAD | sed 's/\//-/g') +VTAG := $(shell git tag --points-at HEAD) +VERS := $(shell [ -z $(VTAG) ] && echo 'tip' || echo $(VTAG) ) + +all: $(BIN) + +$(BIN): $(CODE) + go build -ldflags "-X main.brch=$(BRCH) -X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)" ./cmd/$(BIN) + +test: + go test -cover -coverprofile=coverage.out ./... + +build: Dockerfile + docker build -t $(REPO):$(BRCH) \ + -t $(REPO):$(VERS) \ + -t $(REPO):$(HASH) \ + -t $(REPO):$(BRCH)-$(DATE)-$(VERS)-$(HASH) . + +push: build + docker push $(REPO):$(BRCH) + docker push $(REPO):$(VERS) + docker push $(REPO):$(HASH) + docker push $(REPO):$(BRCH)-$(DATE)-$(VERS)-$(HASH) + +clean: + -@rm -f $(BIN) + -@rm -f coverage.out diff --git a/cmd/faas/main.go b/cmd/faas/main.go index 4d0b4936d..33e8038c0 100644 --- a/cmd/faas/main.go +++ b/cmd/faas/main.go @@ -1,7 +1,14 @@ package main -import "github.com/boson-project/faas/cmd" +import ( + "github.com/boson-project/faas/cmd" +) + +// Statically-populated build metadata set +// by `make build`. +var brch, date, vers, hash string func main() { + cmd.SetMeta(brch, date, vers, hash) cmd.Execute() } diff --git a/cmd/root.go b/cmd/root.go index 5b11a562f..efa4931ad 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,7 +19,7 @@ var ( var root = &cobra.Command{ Use: "faas", Short: "Function as a Service", - Version: Version, + Version: verboseVersion(), SilenceErrors: true, // we explicitly handle errors in Execute() SilenceUsage: true, // no usage dump on error Long: `Function as a Service diff --git a/cmd/version.go b/cmd/version.go index a96ad26da..bbd9c6a21 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -6,10 +6,6 @@ import ( "github.com/spf13/cobra" ) -// Version -// Printed on subcommand `version` or flag `--version` -const Version = "v0.2.1" - func init() { root.AddCommand(versionCmd) } @@ -21,5 +17,32 @@ var versionCmd = &cobra.Command{ } func version(cmd *cobra.Command, args []string) { - fmt.Println(Version) + fmt.Println(verboseVersion()) +} + +// Populated at build time by `make build`, plumbed through +// main using SetMeta() +var ( + brch string // the branch built from + date string // datestamp + vers string // verstionof git commit or `tip` + hash string // git hash built from +) + +// SetMeta from the build process, used for verbose version tagging. +func SetMeta(buildBranch, buildTimestamp, commitVersionTag, commitHash string) { + brch = buildBranch + date = buildTimestamp + vers = commitVersionTag + hash = commitHash +} + +func verboseVersion() string { + // If building from source (i.e. from `go install` or `go build` directly, + // simply print 'v0.0.0-source`, a semver-valid version indicating no version + // number. Otherwise print the verbose version populated during `make build`. + if vers == "" { // not statically populatd + return "v0.0.0-source" + } + return fmt.Sprintf("%s-%s-%s-%s", brch, date, vers, hash) }