-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.mk
136 lines (108 loc) · 3.66 KB
/
build.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
SHELL := /bin/bash
GO := go
BUILDDIR := dist
# capture version information
GITSHA := $(shell git rev-parse --short HEAD)
VERSION := $(shell cat version.txt)
IMAGETAG := $(shell git describe --tags --exact-match 2>/dev/null || git symbolic-ref --short HEAD)
CTIMEVAR=-X $(PKG)/version.GitCommit=$(GITSHA) -X $(PKG)/version.Version=$(VERSION)
GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)"
GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static"
all: clean build check install
.PHONY: clean
clean: ## Clean up any binaries and build artifacts
@echo "+ $@"
$(RM) $(NAME)
$(RM) -r $(BUILDDIR)
.PHONY: build
build: $(NAME)
$(NAME): $(wildcard *.go) $(wildcard */*.go)
@echo "+ $@"
$(GO) build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) .
.PHONY: static
static:
@echo "+ $@"
CGO_ENABLED=$(CGO_ENABLED) $(GO) build \
-tags "$(BUILDTAGS),netgo,osusergo,static_build" ${GO_LDFLAGS_STATIC} \
-o $(NAME) .
.PHONY: release
release: build-release calculate-checksums ## Creates release artifacts
.PHONY: build-release
build-release: *.go ## Builds release binaries
@echo "+ $@"
CGO_ENABLED=$(CGO_ENABLED) gox \
-os="darwin freebsd linux solaris windows" \
-arch="amd64 arm arm64 386" \
-osarch="!darwin/arm !darwin/arm64" \
-output="$(BUILDDIR)/$(NAME)-{{.OS}}-{{.Arch}}" \
-tags "$(BUILDTAGS),netgo,osusergo,static_build" \
$(GO_LDFLAGS_STATIC)
define checksum
sha256sum $(1) > $(1).sha256;
endef
.PHONY: image
image: ## Builds a Docker image
@echo "+ $@"
@docker build --rm --force-rm -t $(REGISTRY)/$(NAME) .
.PHONY: tag-image
tag-image: image
@echo "+ $@"
@docker tag $(REGISTRY)/$(NAME) $(REGISTRY)/$(NAME):$(GITSHA)
@docker tag $(REGISTRY)/$(NAME) $(REGISTRY)/$(NAME):$(IMAGETAG)
.PHONY: push-image
push-image: tag-image
@echo "+ $@"
@docker push $(REGISTRY)/$(NAME)
@docker push $(REGISTRY)/$(NAME):$(GITSHA)
@docker push $(REGISTRY)/$(NAME):$(IMAGETAG)
.PHONY: calculate-checksums
calculate-checksums: $(wildcard BUILDDIR)/* ## Calculates checksums for release artifacts
$(RM) $(BUILDDIR)/*.sha256
$(foreach bin,$(wildcard $(BUILDDIR)/*), $(call checksum,$(bin)))
.PHONY: fmt
fmt: ## Makes sure go source files are formatted in the canonical format
@echo "+ $@"
@if [[ ! -z "$(shell gofmt -l -s . | grep -v vendor | tee /dev/stderr)" ]]; then \
exit 1; \
fi
.PHONY: lint
lint: ## Makes sure `golint`
@echo "+ $@"
@if [[ ! -z "$(shell golint ./... | grep -v vendor | tee /dev/stderr)" ]]; then \
exit 1; \
fi
.PHONY: staticcheck
staticcheck: ## Makes sure `staticcheck` passes
@echo "+ $@"
@if [[ ! -z "$(shell staticcheck $(shell $(GO) list ./... | grep -v vendor) | tee /dev/stderr)" ]]; then \
exit 1; \
fi
.PHONY: vet
vet: ## Makes sure `go vet` passes
@echo "+ $@"
@if [[ ! -z "$(shell $(GO) vet $(shell $(GO) list ./... | grep -v vendor) | tee /dev/stderr)" ]]; then \
exit 1; \
fi
.PHONY: gosec
gosec: ## Makes sure `gosec` passes
@echo "+ $@"
@if [[ ! -z "$(shell gosec -quiet -fmt golint -confidence medium -severity medium ./... | tee /dev/stderr)" ]]; then \
exit 1; \
fi
.PHONY: test
test: ## Runs `go test` and makes sure the tests pass
@echo "+ $@"
@$(GO) test -v -tags "$(BUILDTAGS) cgo" $(shell $(GO) list ./... | grep -v vendor)
.PHONY: check
check: test fmt lint staticcheck vet ## Runs test, fmt, lint, staticcheck, and vet
.PHONY: install
install:
@echo "+ $@"
@$(GO) install -a -tags "$(BUILDTAGS)" ${GO_LDFLAGS}
.PHONY: tag
tag: ## Creates a new git tag for the current version
git tag -sa $(VERSION) -m "$(VERSION)"
git push origin master $(VERSION)
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | cut -d ':' -f2- | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'