-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.mk
148 lines (120 loc) · 4.08 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
137
138
139
140
141
142
143
144
145
146
147
148
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)
BUILD_TIME:=`date -u '+%Y-%m-%dT%H:%M:%S'`
CTIMEVAR=-X $(PKG)/version.GitCommit=$(GITSHA) -X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.BuildTime=$(BUILD_TIME)
GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)"
GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static"
PACKAGES:=$(shell go list ./... | grep -v vendor)
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) ./cmd/kube-gen
.PHONY: static
static:
@echo "+ $@"
CGO_ENABLED=$(CGO_ENABLED) $(GO) build \
-tags "$(BUILDTAGS),netgo,osusergo,static_build" ${GO_LDFLAGS_STATIC} \
-o $(NAME) ./cmd/kube-gen
.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 !darwin/386" \
-output="$(BUILDDIR)/$(NAME)-{{.OS}}-{{.Arch}}" \
-tags "$(BUILDTAGS),netgo,osusergo,static_build" \
$(GO_LDFLAGS_STATIC) \
$(PACKAGES)
define checksum
sha256sum $(1) > $(1).sha256;
endef
.PHONY: image
image: ## Builds a Docker image
@echo "+ $@"
@docker build --rm --force-rm -t $(NAME) .
.PHONY: tag-image
tag-image: image
@echo "+ $@"
@for reg in $(REGISTRIES); do \
docker tag $(NAME) "$${reg}/$(NAME):latest"; \
docker tag $(NAME) "$${reg}/$(NAME):$(GITSHA)"; \
docker tag $(NAME) "$${reg}/$(NAME):$(IMAGETAG)"; \
docker tag $(NAME) "$${reg}/$(NAME):$(VERSION)"; \
done
.PHONY: push-image
push-image: tag-image
@echo "+ $@"
@for reg in $(REGISTRIES); do \
docker push "$${reg}/$(NAME):latest"; \
docker push "$${reg}/$(NAME):$(GITSHA)"; \
docker push "$${reg}/$(NAME):$(IMAGETAG)"; \
docker push "$${reg}/$(NAME):$(VERSION)"; \
done
.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)" ]]; then \
echo "Please run go fmt on the following files:"; \
gofmt -l .; \
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: golangci-lint
golangci-lint:
@echo "+ $@"
@if [[ ! -z "$$(golangci-lint run --color=always --out-format=colored-line-number -E misspell | tee /dev/stderr)" ]]; then \
exit 1; \
fi
.PHONY: check
check: test fmt ## Runs test and fmt
.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 main $(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}'