make build with version tags

This commit is contained in:
Luke K 2020-07-08 02:32:35 +00:00
parent cedda96896
commit a232165688
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
4 changed files with 70 additions and 7 deletions

33
Makefile Normal file
View File

@ -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

View File

@ -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()
}

View File

@ -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

View File

@ -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)
}